import numpy as np
import matplotlib.pyplot as plt
import cv2 as cv
import tensorflow as tf
from tensorflow.keras import layers
from tensorflow.keras import utils
from tensorflow.keras import optimizers
# load data
mnist = tf.keras.datasets.mnist
(x_train, y_train),(x_test, y_test) = mnist.load_data()
# prepare data
x_train, x_test = x_train / 255.0, x_test / 255.0 # normalize
y_train_oh = utils.to_categorical(y_train, num_classes=10) # one-hot encoding
y_test_oh = utils.to_categorical(y_test, num_classes=10) # one-hot encoding
# plot one instance
plt.title("The label is '{label}'".format(label=y_train[888]))
plt.imshow(x_train[888], cmap='Greys')
plt.show()
model = tf.keras.models.Sequential([
layers.Flatten(input_shape=(28, 28)),
layers.Dense(256, activation='relu'),
layers.Dense(128, activation='relu'),
layers.Dense(10, activation='softmax')
])
# If we want to define the hyperparameters of optimizer
adam = optimizers.Adam(lr=0.001, beta_1=0.9, beta_2=0.999,
epsilon=None, decay=0.0, amsgrad=False) # I just copy the default hyperparameters here
model.compile(optimizer='adam',
loss='categorical_crossentropy',
metrics=['accuracy'])
model.fit(x_train, y_train_oh, epochs=5, batch_size=None) # If unspecified, batch_size will default to 32.
model.evaluate(x_test, y_test_oh)