def loss_fn(y_true, y_pred):
return y_true # or y_pred
...
import keras.callbacks as cbks
class CustomMetrics(cbks.Callback):
def on_epoch_end(self, epoch, logs=None):
for k in logs:
if k.endswith('loss_fn'):
print logs[k]
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.
如果使用TensorFlow的keras,则可以启用Eager Execution:
然后你可以在损失函数中打印张量。
如果您收到错误消息“ValueError:Only TF native optimizer s are supported In Eager mode.”并且您使用了“adam”作为优化器,例如,您可以将模型的编译参数更改为
通常,
y_true
你事先知道-在准备你的火车语料库。。。但是,有一个技巧可以看到
y_true
和/或y_pred
中的值。Keras给了你一个机会写下相应的callback来打印神经网络的输出。 它看起来像这样:这里的
loss_fn
是在模型编译期间将loss函数传递到model.compile(...,metrics=[loss_fn],)
函数时的函数名。因此,最后,必须将这个
CustomMetrics
回调作为参数传递到model.fit()
:注:如果你在Keras中使用类似于这里的Theano(或TensorFlow),你可以编写一个python程序,然后编译并执行它。因此,在您的示例中
y_true
-只是一个张量变量,用于进一步编译和损失函数计数。这意味着无法看到里面的值。例如,在ano中,您可以在执行相应的
eval()
函数之后查看唯一所谓的共享变量。有关详细信息,请参见this question。Keras的后端有
print_tensor
,这使您能够做到这一点。你可以这样使用它:函数返回一个相同的张量。当对该张量求值时,它将打印其内容,前面是
message
。 从Keras docs:所以,以后一定要用张量。
相关问题 更多 >
编程相关推荐