In [121]:
import tensorflow as tf
mnist = tf.keras.datasets.mnist
(x_train, y_train),(x_test, y_test) = mnist.load_data()
x_train, x_test = x_train / 255.0, x_test / 255.0 # Keep the values the same but convert to dtype('float64')
# x_train = x_train / 255.0
In [122]:
model = tf.keras.models.Sequential([
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(512, activation=tf.nn.relu),
tf.keras.layers.Dropout(0.2),
tf.keras.layers.Dense(10, activation=tf.nn.softmax)
])
In [123]:
model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
In [124]:
model.fit(x_train, y_train, epochs=25, validation_data=(x_test, y_test))
Out[124]:
In [126]:
model.evaluate(x_test, y_test)
Out[126]:
In [127]:
y_test.shape
Out[127]:
In [103]:
import numpy as np
prediction = model.predict(x_test[:50])
In [102]:
np.argmax(prediction, 1)
Out[102]:
In [71]:
tf.keras.models.save_model(model, 'test_tf_model')
In [74]:
new_model = tf.keras.models.load_model('test_tf_model', compile=False)
In [85]:
all(np.argmax(new_model.predict(x_test[:50]), 1) == np.argmax(prediction, 1))
Out[85]:
In [97]:
(x_train, y_train),(x_test, y_test) = mnist.load_data()
In [98]:
x_train[0].dtype
Out[98]:
In [99]:
x_train, x_test = x_train / 255.0, x_test / 255.0
x_train[0].dtype
Out[99]: