我有这段代码:
import tensorflow as tf
import numpy as np
# batch x time x events x dim
batch = 2
time = 3
events = 4
tensor = np.random.rand(batch, time, events)
tensor[0][0][2] = 0
tensor[0][0][3] = 0
tensor[0][1][3] = 0
tensor[0][2][1] = 0
tensor[0][2][2] = 0
tensor[0][2][3] = 0
tensor[1][0][3] = 0
def cum_sum(prev, cur):
non_zeros = tf.equal(cur, 0.)
tf.Print(non_zeros, [non_zeros], "message ")
tf.Print(cur, [cur])
return cur
elems = tf.constant([1,2,3],dtype=tf.int64)
#alternates = tf.map_fn(lambda x: (x, 2*x, -x), elems, dtype=(tf.int64, tf.int64, tf.int64))
cum_sum_ = tf.scan(cum_sum, tensor)
s = tf.Session()
s.run(cum_sum_)
我在传递给 tf.scan
的函数中有两个 tf.Print
语句,但是当我运行累计和时,我没有得到任何打印声明。我做错了什么吗?
最佳答案
tf.Print 不是那样工作的。打印节点需要进入图形才能执行。我强烈建议您查看 this教程以了解如何使用它。
如果您有任何问题,请随时提出。
关于python - 为什么 tf.Print() 不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51422833/