chaosink
chaosink

Reputation: 1483

How to reshape a tensor to the shape of a placeholder?

How to reshape a tensor to the shape of a placeholder?

a = tf.placeholder(shape=[None, None, 48])
h, w, c = a.shape
b = tf.reshape(a, shape=[-1, 4, 4, 3]) # flatten the first two dimension and expand the last dimension
# do something
c = tf.reshape(b, shape=[h, w, 3]) # reshape back to the shape of a, but error occurs

I want to reshpae tensor b back to the shape of placeholder a.

Upvotes: 1

Views: 1032

Answers (1)

P-Gn
P-Gn

Reputation: 24581

a has dynamic shapes that are not known at graph construction time, so you need to turn to tensorflow operations, and more specifically to tf.shape that returns (dynamically) the shape of a tensor.

So in your example you could use e.g.

s = tf.shape(a)
c = tf.reshape(b, shape=[s[0], s[1], 3])

Upvotes: 3

Related Questions