网上大多都是 sess.run(),且也是 tensorflow1 中的版本
tf.keras, tf2.0以后采用如下方式。
import tensorflow.keras.backend as K
K.get_value(my_variable)
如下也可更改为logs["learning_rate"] = k.get_value(self.model.optimizer.lr)
不同版本测试之后,就知道了。
'''
参考stackoverflow:
https://stackoverflow.com/questions/49127214/keras-how-to-output-learning-rate-onto-tensorboard
'''
############# 添加部分 ###############
class LearningRateLogger(tf.keras.callbacks.Callback):
def __init__(self):
super().__init__()
self._supports_tf_logs = True
def on_epoch_end(self, epoch, logs=None):
if logs is None or "learning_rate" in logs:
return
logs["learning_rate"] = self.model.optimizer.lr
callbacks = [
tf.keras.callbacks.ModelCheckpoint(
"densenet_crnn_qunt_{epoch}.h5",
monitor='val_accuracy',
save_best_only=False,
verbose=2,
save_weights_only=False,
period=1),
tf.keras.callbacks.EarlyStopping(monitor='val_loss',
patience=5,
mode='auto',
restore_best_weights=True),
######### 添加部分###########
LearningRateLogger(),
tf.keras.callbacks.TensorBoard(log_dir='logs/',histogram_freq=0),
tf.keras.callbacks.ReduceLROnPlateau(monitor='val_loss', factor=0.2, patience=5, min_lr=0.00000001,verbose=2),
]
quant_model.fit(train_ds,
epochs=args.epochs,
validation_data=val_ds,
callbacks=callbacks,)