在keras的model.fit中没有tf.Print的结果 [英] No result of tf.Print in keras's model.fit

查看:494
本文介绍了在keras的model.fit中没有tf.Print的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了这个损失(用于测试喀拉拉邦的自定义损失):

I wrote that loss (for testing custom loss in keras):

def loss(y_true, y_pred):
  loss = -tf.reduce_sum(y_true * tf.log(y_pred))
  loss = tf.Print(loss, [loss], 'loss = ')
return loss

然后:

model.compile(loss=loss, 
              optimizer=keras.optimizers.Adadelta(),
              metrics=['accuracy'])
model.fit(x_train, y_train)

并且没有tf.Print结果:

and there's no tf.Print results:

Epoch 1/12 
60000/60000 [==============================] - 12s 198us/step - loss: 25.3197 - acc: 0.9384 - val_loss: 5.6927 - val_acc: 0.9857
Epoch 2/12
60000/60000 [==============================] - 11s 187us/step - loss: 8.7803 - acc: 0.9798 - val_loss: 4.6938 - val_acc: 0.9888

为什么?

推荐答案

我想您正在Jupyter Notebook中运行它. tf.Print() 打印到终端,从该终端调用Jupyter Notebook .看看那里,看看是否有输出.

I suppose you're running this in Jupyter Notebook. tf.Print() prints to the terminal where the Jupyter Notebook was called from. Take a look there and see if there's output.

请参见 tf.Print() 手册页上的注释.

See blue note at the tf.Print() manual page.

从Evgeniya的以下评论中:您可以编写自己的tf.Print() 打印所需的数据(通过 Vihari Piratla 进行编码):

From Evgeniya's comment below: you can write your own version of tf.Print() to print the data you desire (code by Vihari Piratla):

"""
The default tf.Print op goes to STDERR
Use the function below to direct the output to stdout instead
Usage: 
> x=tf.ones([1, 2])
> y=tf.zeros([1, 3])
> p = x*x
> p = tf_print(p, [x, y], "hello")
> p.eval()
hello [[ 0.  0.]]
hello [[ 1.  1.]]
"""
def tf_print(op, tensors, message=None):
    def print_message(x):
        sys.stdout.write(message + " %s\n" % x)
        return x

    prints = [tf.py_func(print_message, [tensor], tensor.dtype) for tensor in tensors]
    with tf.control_dependencies(prints):
        op = tf.identity(op)
    return op

这篇关于在keras的model.fit中没有tf.Print的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆