import tensorflow as tf
import numpy as np
mnist = tf.keras.datasets.mnist
(xtrain, ytrain), (xtest, ytest) = mnist.load_data()
print(xtrain.shape, ytrain.shape, xtest.shape,ytest.shape)
xtrain, xtest = xtrain/255.0, xtest/255.0
model = tf.keras.Sequential([
tf.keras.layers.Flatten(input_shape=(28,28)),
tf.keras.layers.Dense(128,activation='relu'),
tf.keras.layers.Dropout(0.2),
tf.keras.layers.Dense(10,activation='softmax')
])
model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
check_path = 'my_mnist_checkpoint/ckpt/cp-{epoch:04d}.ckpt'
save_mode_cp = tf.keras.callbacks.ModelCheckpoint(check_path, verbose=1, save_weights_only=True, period=2)
model.fit(xtest,ytest,epochs=5,callbacks=[save_mode_cp])