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
probability_model = tf.keras.Sequential([model, tf.keras.layers.Softmax()]) | |
predictions = probability_model.predict(test_images) | |
# 'predictions' will contain the prediction for each image in the training set. Lets check the first prediction | |
predictions[0] |
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
test_loss, test_acc = model.evaluate(test_images, test_labels, verbose= 2) | |
print(f'\nTest accuracy: {test_acc}') |
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
%%timeit -n1 -r1 # time required toexecute this cell once | |
# To view in TensorBoard | |
logdir = os.path.join("logs/adam", datetime.datetime.now().strftime("%Y%m%d-%H%M%S")) | |
tensorboard_callback = tf.keras.callbacks.TensorBoard(logdir, histogram_freq=1) | |
model.fit(train_images, train_labels, epochs= 10, callbacks = [tensorboard_callback]) |
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
# The from_logits=True attribute inform the loss function that the output values generated by the model are not normalized, a.k.a. logits. | |
# Since softmax layer is not being added at the last layer, hence we need to have the from_logits=True to indicate the probabilities are not normalized. | |
model.compile(optimizer= 'adam', | |
loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True), | |
metrics = ['accuracy']) |
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
model = tf.keras.Sequential([ | |
tf.keras.layers.Flatten(input_shape=(28,28)), | |
tf.keras.layers.Dense(128, activation= 'relu'), | |
tf.keras.layers.Dense(10) # linear activation function | |
]) |
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
train_images = train_images / 255.0 | |
test_images = test_images / 255.0 |
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
# Display the first 25 images from the training set and display the class name below each image. | |
plt.figure(figsize=(10,10)) | |
for i in range(25): | |
plt.subplot(5,5,i+1) | |
plt.xticks([]) | |
plt.yticks([]) | |
plt.grid(False) | |
plt.imshow(train_images[i], cmap=plt.cm.binary) | |
plt.xlabel(class_names[train_labels[i]]) | |
plt.show() |
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
# Images labels(classes) possible values from 0 to 9 | |
train_labels |
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
plt.figure() | |
plt.imshow(train_images[0]) | |
plt.colorbar() | |
plt.grid(False) | |
plt.show() |
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
# The images are 28x28 NumPy arrays, with pixel values ranging from 0 to 255 | |
train_images |