简体   繁体   English

在 Keras 训练期间如何在损失函数内打印?

[英]How do I print inside the loss function during training in Keras?

I am trying to create a loss function in Keras (Tensorflow Backend) but I am a little stuck to check the inside of the custom loss function.我正在尝试在 Keras(Tensorflow 后端)中创建一个损失函数,但我有点难以检查自定义损失函数的内部。 In fact, the print appears on the console only when I compile the model, after that there is no print.实际上,只有在我编译模型时才会在控制台上显示打印,之后就没有打印了。 (I am just testing very simple custom function, I will create the true function when I solved this problem). (我只是在测试非常简单的自定义函数,当我解决这个问题时我会创建真正的函数)。 I train the model using the train_on_batch function.我使用train_on_batch函数训练模型。 How can I solve this problem?我该如何解决这个问题?

def loss_yolo(self, y_true, y_pred):  
    print('inside loss function')
    loss = K.mean(y_true - y_pred)
    return loss

model.compile(optimizer='sgd', loss=loss_yolo)

print('train on batch')
print(model.train_on_batch(x, y))

And the output is输出是

inside loss function内部损失函数

train on batch批量训练

-0.481604 -0.481604

The only thing you can do is not use python's print function, but for example, tensorflow's tf.Print function that is part of the computational graph.你唯一能做的就是不使用 python 的打印函数,但例如,tensorflow 的tf.Print函数是计算图的一部分。 The documentation says the operation does nothing but each time it is evaluated it prints a message that you can specify.该文档说该操作什么都不做,但每次评估它时都会打印一条您可以指定的消息。

You just need to be careful to place it correctly in the graph, something like:您只需要小心将其正确放置在图表中,例如:

def loss(y_true, y_pred):
    d = y_true - y_pred
    d = tf.Print(d, [d], "Inside loss function")
    return tf.reduce_mean(tf.square(d))

A better option to look inside what is going on internally is to use the tensorflow debugger .查看内部发生的事情的更好选择是使用tensorflow debugger

I added the output_stream argument and tried this code in TensorFlow v2.4.1.我添加了output_stream参数并在 TensorFlow v2.4.1 中尝试了这段代码。 Worked fine:工作正常:

def loss_custom(y_true, y_pred):
    d = y_true - y_pred
    tf.print("\n y_true:", type(y_true), output_stream=sys.stdout)
    return tf.reduce_mean(tf.square(d))

Output during training:训练期间的输出:

Epoch 1/10

 y_true: <class 'tensorflow.python.framework.ops.Tensor'>
 1/72 [..............................] - ETA: 0s - loss: 0.2328 - accuracy: 0.3319
 y_true: <class 'tensorflow.python.framework.ops.Tensor'>
 2/72 [..............................] - ETA: 9s - loss: 0.2087 - accuracy: 0.5250
 y_true: <class 'tensorflow.python.framework.ops.Tensor'>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在训练期间替换损失函数 tensorflow.keras - How to replace loss function during training tensorflow.keras 在Keras中,如何在训练期间访问Word2Vec(嵌入)矢量以实现自定义损失功能 - In Keras, how can I access Word2Vec (embedding) vectors for custom loss function during training 如何在训练期间修改每个时期的损失函数内的变量? - How to modify a variable inside the loss function in each epoch during training? 在训练过程中如何在Tensorflow Python中打印训练损失 - How to print training loss in tensorflow python during training process 如何在Keras实施的卷积神经网络的训练过程中修复我的骰子损失计算? - How can I fix my dice loss calculation during the training process of a convolutional neural network implemented in Keras? 获取 keras 自定义损失 function 内部的训练数据形状 - Get training data shape inside keras custom loss function Keras 训练期间损失的移动平均 - Moving averaging of Loss during Training in Keras 每次使用 Keras 开始新的训练 session 时,如何防止丢失和准确性重新开始? - How do I prevent loss and accuracy from restarting every time I start a new training session with Keras? 如何在 Keras 中使用自定义损失函数加快模型的训练速度? - How can I speed the training up for a model with a custom loss function in Keras? 如何在 keras 中为损失函数实现 ssim? - how do I implement ssim for loss function in keras?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM