[英]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.