![](/img/trans.png)
[英]TensorFlow Recommenders - ValueError: Shape must be rank 2 but is rank 3
[英]TensorFlow - Slicing tensor results in: ValueError: Shape (16491,) must have rank 3
我想对张量进行切片以按索引列表获取特定张量,例如:
word_weight = tf.get_variable("word_weight", [20])
a= word_weight[ [1,6,5] ]
(我想得到word_weight[1], word_weight[6], word_weight[5]
)
但是当我运行代码时出现以下错误:
ValueError: Shape (16491,) must have rank 3
首先,首先评估张量。 然后,您可以索引它们:
import tensorflow as tf
word_weight = tf.get_variable("word_weight", [20])
with tf.Session() as sess:
tf.initialize_all_variables().run()
x = sess.run(word_weight)
print(x[[1,6,5]])
# Or evaluete like this
print(sess.run([word_weight[1],word_weight[6],word_weight[5]]))
这输出:
[ 1.61491954 0.66727936 -0.73491937]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.