tensorflow使用keras后端,保存每一步运行的loss(tensorflow、keras通用)

参考博客:https://blog.csdn.net/yang_daxia/article/details/84571658

 

class LossHistory(tf.keras.callbacks.Callback):#如果是直接keras就把tf去了
    def on_train_begin(self, logs={}):
        self.losses = []
 
    def on_batch_end(self, batch, logs={}):
        self.losses.append(logs.get('loss'))
from tensorflow.python.keras.callbacks import ModelCheckpoint

history = LossHistory()
checkpoint = ModelCheckpoint(filepath, monitor='val_loss', verbose=1, save_best_only=True,mode='auto')

callbacks_list = [checkpoint,history]




with open('models/new/log_loss.txt','a', encoding='utf-8') as f:
    f.write(history.losses)

 

你可能感兴趣的:(keras,tensorflow)