gpt4 book ai didi

python-3.x - 如何在 while 循环中打印张量的值?

转载 作者:行者123 更新时间:2023-12-04 18:41:10 24 4
gpt4 key购买 nike

我对 tensorflow 很陌生,我搞不懂这个。

我有这个 while 循环:

def process_tree_tf(n_child, reprs, weights, bias, embed_dim, activation = tf.nn.relu):
n_child, reprs = n_child, reprs
parent_idxs = generate_parents_numpy(n_child)
loop_idx = reprs.shape[0] - 1
loop_vars = loop_idx, reprs, parent_idxs, weights, embed_dim

def loop_condition(loop_ind, *_):
return tf.greater(0, loop_idx)

def loop_body(loop_ind, reprs, parent_idxs, weights, embed_dim):
x = reprs[loop_ind]
x_expanded = tf.expand_dims(x, axis=-1)
w = weights
out = tf.squeeze(tf.add(tf.matmul(x_expanded,w,transpose_a=True), bias))
activated = activation(out)
par_idx = parent_idxs[loop_ind]
reprs = update_parent(reprs, par_idx, embed_dim, activated)
reprs = tf.Print(reprs, [reprs]) #This doesn't work
loop_ind = loop_ind-1
return loop_ind, reprs, parent_idxs, weights, embed_dim

return tf.while_loop(loop_condition, loop_body, loop_vars)

我是这样评价的:

embed_dim = 2
hidden_dim = 2
n_nodes = 4
batch = 2
reprs = np.ones((n_nodes, embed_dim+hidden_dim))
n_child = np.array([1, 1, 1, 0])
weights = np.ones((embed_dim+hidden_dim, hidden_dim))
bias = np.ones(hidden_dim)
with tf.Session() as sess:
_, r, *_ = process_tree_tf(n_child, reprs, weights, bias, embed_dim, activation=tf.nn.relu)
print(r.eval())

我想在 while 循环中检查 reprs 的值,但是 tf.Print 似乎不起作用,并且 print只是告诉我它是一个张量并给我它的形状。我该怎么做?

非常感谢!

最佳答案

看看这个网页:https://www.tensorflow.org/api_docs/python/tf/Print

您可以看到 tf.Print 是一个身份运算符,在评估时具有打印数据的副作用。因此,您应该使用这一行来打印:

reprs = tf.Print(reprs, [reprs])

希望这对您有所帮助,祝您好运!

关于python-3.x - 如何在 while 循环中打印张量的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42443467/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com