简体   繁体   中英

TensorFlow - Slicing tensor results in: ValueError: Shape (16491,) must have rank 3

I want to slice tensor to get specific tensor by list of index, for example:

word_weight   = tf.get_variable("word_weight", [20])
a= word_weight[ [1,6,5] ]

(I want to get word_weight[1], word_weight[6], word_weight[5] )

But I get the following error when I run the code:

ValueError: Shape (16491,) must have rank 3

First, evaluate the tensor first. Then, you can index them:

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]]))

This outputs:

[ 1.61491954  0.66727936 -0.73491937]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM