我在本教程中实现了 cycleGAN:https://keras.io/examples/generative/cyclegan/
现在我想为两个生成器和两个鉴别器的损失创建一个 Tensorboard 回调。
我试过这个:
# Callbacks
lr_callback = tf.keras.callbacks.ReduceLROnPlateau(monitor='G_loss', factor=0.2,
patience=5, min_lr=0)
log_dir = "logs/fit/" + log_name
tensorboard_callback = tf.keras.callbacks.TensorBoard(log_dir=log_dir, histogram_freq=1)
并将其添加到
.fit
函数中:
cycle_gan_model.fit(
data_gen_training,
epochs=50,
callbacks=[plotter, model_checkpoint_callback, lr_callback, tensorboard_callback],
)
但是我得到这个错误:
ValueError: Expected scalar shape, saw shape: (1, 30, 30).
没有回调我不会收到任何错误。
所以我的问题是:如何将每个时期的损失写入 tensorboard 日志文件?
提前致谢
我在尝试为
cycle_gan_model.fit()
创建 TensorBoard 回调时遇到了同样的问题。为了解决这个问题,我必须首先使用 train_step
在急切模式下运行我的 tf.config.run_functions_eagerly(True)
并通过将 tf.reduce_mean
应用于每个输出损失(以及可选的 .numpy()
)将损失作为标量返回。
例如,
return {
"G_loss": tf.reduce_mean(total_loss_G).numpy(),
"F_loss": tf.reduce_mean(total_loss_F).numpy(),
"D_X_loss": tf.reduce_mean(disc_X_loss).numpy(),
"D_Y_loss": tf.reduce_mean(disc_Y_loss).numpy(),
}
我不确定应用
reduce_mean
是否会影响训练的结果,但我能够让 TensorBoard 工作以可视化生成器和鉴别器的损失。