Last active
October 1, 2019 20:27
-
-
Save vibhatha/60843deecb76edf03718e5dc78ea81bf to your computer and use it in GitHub Desktop.
MNIST Basic Example with Cloudmesh
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# replace the # with ! to run them in the Python Notebook | |
# pip install cloudmesh-installer | |
# pip install cloudmesh-common | |
import time | |
import numpy as np | |
from keras.models import Sequential | |
from keras.layers import Dense, Activation, Dropout | |
from keras.utils import to_categorical, plot_model | |
from keras.datasets import mnist | |
from cloudmesh.common.StopWatch import StopWatch | |
import matplotlib.pyplot as plt | |
import matplotlib.image as mpimg | |
StopWatch.start("data-load") | |
(x_train, y_train), (x_test, y_test) = mnist.load_data() | |
StopWatch.stop("data-load") | |
x1 = x_train[0] | |
y1 = y_train[0] | |
plt.imshow(x1) | |
y1 | |
num_labels = len(np.unique(y_train)) | |
y_train = to_categorical(y_train) | |
y_test = to_categorical(y_test) | |
image_size = x_train.shape[1] | |
input_size = image_size * image_size | |
x_train = np.reshape(x_train, [-1, input_size]) | |
x_train = x_train.astype('float32') / 255 | |
x_test = np.reshape(x_test, [-1, input_size]) | |
x_test = x_test.astype('float32') / 255 | |
batch_size = 4 | |
hidden_units = 64 | |
model = Sequential() | |
model.add(Dense(hidden_units, input_dim=input_size)) | |
model.add(Dense(num_labels)) | |
model.add(Activation('softmax')) | |
model.summary() | |
plot_model(model, to_file='mnist_v1.png', show_shapes=True) | |
StopWatch.start("compile") | |
model.compile(loss='categorical_crossentropy', | |
optimizer='adam', | |
metrics=['accuracy']) | |
StopWatch.stop("compile") | |
StopWatch.start("train") | |
model.fit(x_train, y_train, epochs=1, batch_size=batch_size) | |
StopWatch.stop("train") | |
StopWatch.start("test") | |
loss, acc = model.evaluate(x_test, y_test, batch_size=batch_size) | |
print("\nTest accuracy: %.1f%%" % (100.0 * acc)) | |
StopWatch.stop("test") | |
StopWatch.benchmark() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment