如何打印Keras张量的值? [英] How can I print the values of Keras tensors?
问题描述
我正在实现自己的Keras损失功能.如何访问张量值?
I am implementing own Keras loss function. How can I access tensor values?
我尝试过的
def loss_fn(y_true, y_pred):
print y_true
它打印
Tensor("target:0", shape=(?, ?), dtype=float32)
是否有Keras函数可以访问y_true
值?
Is there any Keras function to access y_true
values?
推荐答案
Keras的后端具有print_tensor
,使您可以执行此操作.您可以通过以下方式使用它:
Keras' backend has print_tensor
which enables you to do this. You can use it this way:
import keras.backend as K
def loss_fn(y_true, y_pred):
y_true = K.print_tensor(y_true, message='y_true = ')
y_pred = K.print_tensor(y_pred, message='y_pred = ')
...
该函数返回相同的张量.当评估该张量时,它将打印其内容,并以message
开头.
从 Keras文档:
The function returns an identical tensor. When that tensor is evaluated, it will print its content, preceded by message
.
From the Keras docs:
请注意,print_tensor返回的新张量与x相同,应在以下代码中使用.否则,评估过程中将不考虑打印操作.
Note that print_tensor returns a new tensor identical to x which should be used in the following code. Otherwise the print operation is not taken into account during evaluation.
因此,请确保随后使用张量.
So, make sure to use the tensor afterwards.
这篇关于如何打印Keras张量的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!