Keras callback

import tensorflow as tf
class myCallback(tf.keras.callbacks.Callback):
    def on_epoch_end(self,epoch,logs={}):
        if logs.get('acc')>0.99:
            print('\nReaching 99% accuracy so cancelling training')
            self.model.stop_training = True
            
def train():
    callback = myCallback()
    model = tf.keras.Sequential([tf.keras.layers.Flatten(input_shape = (28,28)),
                                 tf.keras.layers.Dense(512,activation = tf.nn.relu),               
                                  ])
    model.compile(optimizer='adam',loss='',metrics='')
    history = model.fit(x,y,epochs=10,callbacks=[callback])
    
posted @ 2019-08-20 12:53  FromZeroToOne  阅读(395)  评论(0编辑  收藏  举报