yac
yac

Reputation: 63

Tensors transformation

Am trying to use tf.gather_nd(params, indices, name=None) to retrieve elements from a feature map tensor

Is there anyway to transform this tensor [[0,2,2]] to [[0,0],[1,2],[2,2]] As I need it to use it as indices in the function

I only have [[0,2,2]]

it should be this structure

indices = [[0,0],[1,2],[2,2]]
params = [['3', '1','2','-1'], ['0.3', '1.4','5','0'],['5', '6','7','8']]

t=tf.gather_nd(params, indices, name=None)

with tf.Session() as sess:

    sess.run(tf.initialize_all_variables())
    print(sess.run(t)) # outputs 3 5 7

Upvotes: 0

Views: 52

Answers (1)

rlys
rlys

Reputation: 480

Assuming you're trying to transform the tensor t0 = [[x0, x1, x2, ... xn]] into the tensor [[0, x0], [1, x1], [2, x2], ..., [n, xn]], you can concatenate it with a range tensor as follows:

t0 = ...
N = tf.shape(t0)[1]                   # number of indices
t0 = tf.concat([tf.range(N), t0], 0)  # [[0, 1, 2], [0, 2, 2]]
indices = tf.transpose(t0)            # [[0, 0], [1, 2], [2, 2]]

This should give you the indices you want.

Upvotes: 1

Related Questions