我在keras中有一个模型,在其中我将自定义指标用作:
class MyMetrics(keras.callbacks.Callback):
def __init__(self):
initial_value = 0
def on_train_begin(self, logs={}):
...
def on_epoch_end(self, batch, logs={}):
here I calculate my important values
现在,可以在Tensorboard中可视化它们吗?例如,如果我的指标类似于:
def mymetric(y_true,y_pred):
return myImportantValues
我可以通过Tensorboard将它们可视化mymodel.compile(..., metrics = mymetric)
指标回调有类似之处吗?我试图在类MyMetric中创建一个函数,并将其传递给mymodel.compile
,但它不会更新值。
您可以使用自定义指标创建事件文件,并在tensorboard中直接对其进行可视化。
这适用于Tensorflow 2.0。在此示例中,从训练历史记录了准确性/度量。对于您的情况,可以从on_epoch_end
回调中完成。
import datetime
current_time = datetime.datetime.now().strftime("%Y%m%d-%H%M%S")
train_log_dir = 'logs/train/' + current_time
train_summary_writer = tf.summary.create_file_writer(train_log_dir)
history = model.fit(x=X, y=y, epochs=100, verbose=1)
for epoch in range(len(history.history['accuracy'])):
with train_summary_writer.as_default():
tf.summary.scalar('loss', history.history['loss'][epoch], step=epoch)
tf.summary.scalar('accuracy', history.history['accuracy'][epoch], step=epoch)
执行脚本后,
tensorboard --logdir日志/火车
https://www.tensorflow.org/tensorboard/r2/get_started#using_tensorboard_with_other_methods