TensorFlow - Slicing tensor results in: ValueError: Shape (16491,) must have 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]