Nils Cao
Nils Cao

Reputation: 1419

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

Upvotes: 1

Views: 473

Answers (1)

Sung Kim
Sung Kim

Reputation: 8536

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]

Upvotes: 1

Related Questions