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
model = tf.keras.models.Sequential([
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(128, activation='relu'),
tf.keras.layers.Dense(10, activation='softmax')
])
model.compile(optimizer='adam',
loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=False),
metrics=['sparse_categorical_accuracy'])
model.fit(x_train, y_train, batch_size=32, epochs=5, validation_data=(x_test, y_test), validation_freq=1)
model.summary()
Epoch 1/5
1875/1875 [==============================] - 1s 569us/step - loss: 0.2590 - sparse_categorical_accuracy: 0.9269 - val_loss: 0.1354 - val_sparse_categorical_accuracy: 0.9600
Epoch 2/5
1875/1875 [==============================] - 1s 530us/step - loss: 0.1137 - sparse_categorical_accuracy: 0.9664 - val_loss: 0.0936 - val_sparse_categorical_accuracy: 0.9725
Epoch 3/5
1875/1875 [==============================] - 1s 524us/step - loss: 0.0788 - sparse_categorical_accuracy: 0.9758 - val_loss: 0.0857 - val_sparse_categorical_accuracy: 0.9756
Epoch 4/5
1875/1875 [==============================] - 1s 526us/step - loss: 0.0588 - sparse_categorical_accuracy: 0.9821 - val_loss: 0.0800 - val_sparse_categorical_accuracy: 0.9750
Epoch 5/5
1875/1875 [==============================] - 1s 529us/step - loss: 0.0442 - sparse_categorical_accuracy: 0.9867 - val_loss: 0.0697 - val_sparse_categorical_accuracy: 0.9803
Model: "sequential"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
flatten (Flatten) (None, 784) 0
dense (Dense) (None, 128) 100480
dense_1 (Dense) (None, 10) 1290
=================================================================
Total params: 101,770
Trainable params: 101,770
Non-trainable params: 0
_________________________________________________________________
Process finished with exit code 0