mnist 数据集物体分类实例
import tensorflow as tf
class myCallback(tf.keras.callbacks.Callback):
def on_epoch_end(self, epoch, logs={}):
if(logs.get('accuracy')>0.9):
print('\n Reached 99% accuracy so cancelling training!')
self.model.stop_training = True
minst = tf.keras.datasets.mnist
(train_images,train_labels),(test_images,test_labels) = minst.load_data()
train_images = train_images/255
test_images = test_images/255
callback= myCallback()
model = tf.keras.Sequential([
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(512,activation=tf.nn.relu),
tf.keras.layers.Dense(10,activation=tf.nn.softmax)
])
model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
model.fit(train_images,train_labels,callbacks=[callback])