【问题标题】:Print loss values every epoch in TF 2.x在 TF 2.x 中每个 epoch 打印损失值
【发布时间】:2021-01-31 07:41:13
【问题描述】:

我在 TF 2.3 中编写了自定义损失函数,其中损失由几个子损失组成,因为我想跟踪我使用 tf.print 打印它们的子损失:

def custom_loss_envelop(model_inputs,  model, num_bound,model_outputs,lambda_, ener):
    def custom_loss(y_true,y_pred):
     l1 = ....
     l2 = ....
     l3 = ....
     tf.print("l1:",tf.math.round(l1 * 100)/100,", l2:", tf.math.round(l2 * 100) / 100,
        ", l3:", tf.math.round(l3 * 100) / 100,
        ", l4:", tf.math.round(l4 * 100) / 100)
        loss = l1 + l2 + l3 + l4
        return loss
    return custom_loss

问题是这段代码每批都打印损失,而我只希望每个时期都打印。 有什么办法吗?

【问题讨论】:

    标签: tensorflow tensorflow2.0


    【解决方案1】:

    您可以将keras.callbacks.Callback() 类与此定义的函数一起使用:

    def on_epoch_end(self, epoch, logs=None):
        keys = list(logs.keys()) 
        # you could do more here ...            
        print("End epoch {} of training; got log keys: {}".format(epoch, keys))
    

    请注意,epoch 的结束仅在训练期间有效。 查看文档中的其他一些选项 https://www.tensorflow.org/guide/keras/custom_callback?hl=en

    【讨论】:

    • 我不认为这会保存子损失,只保存最终损失
    • on_epoch_end 将为您提供做任何您想做的事情的便利,因此您可以简单地在其中添加您的代码。您可以查看更新的示例
    猜你喜欢
    • 2022-06-27
    • 1970-01-01
    • 1970-01-01
    • 2022-01-21
    • 2018-11-12
    • 2021-11-28
    • 2016-07-02
    • 2019-08-13
    相关资源
    最近更新 更多