bountrisv
bountrisv

Reputation: 55

How to replace a dimension of a tensor in tensorflow?

Say I have a tensor with shape (?, 5, 1, 20)

For each occurence of the last dimension I do some computation (getting the k max values) on the last dimension that produces a smaller tensor b. What do I do if want to replace the last dimension of my original tensor with b?

What (preferably pure tensorflow) path should I take?

Upvotes: 0

Views: 1652

Answers (1)

nmiculinic
nmiculinic

Reputation: 2474

You're doing some computation on last dimension...That is you want to go from (?, 5, 1, 20) -> (?, 5, 1, b) if I understood correctly?

What kind of computation?

You could reshape your tensor, do the computation (such as matrix multiplication) and reshape back.

a = tf.reshape(X, [-1, 20])
a = tf.matmul(a, X)
a = tf.reshape(a, [-1, b])

Or you could use tf.einsum() to achieve similar feat. For non-linear computation depends what you want to do.

EDIT: Also you could hack it with Conv2D and using filter of size [1,1, 20, b]. Does the same thing and more efficiently

Upvotes: 2

Related Questions