Reputation: 2621
I encountered a problem to reshape an intermediate 4D tensorflow tensor X
to a 3D tensor Y
, where
X
is of shape ( batch_size, nb_rows, nb_cols, nb_filters )
Y
is of shape ( batch_size, nb_rows*nb_cols, nb_filters )
batch_size = None
Of course, when nb_rows
and nb_cols
are known integers, I can reshape X
without any problem. However, in my application I need to deal with the case
nb_rows = nb_cols = None
What should I do? I tried Y = tf.reshape( X, (-1, -1, nb_filters))
but it clearly fails to work.
For me, this operation is deterministic because it always squeezes the two middle axes into a single one while keeping the first axis and the last axis unchanged. Can anyone help me?
Upvotes: 8
Views: 10963
Reputation: 758
In this case you can access to the dynamic shape of X
through tf.shape(X)
:
shape = [tf.shape(X)[k] for k in range(4)]
Y = tf.reshape(X, [shape[0], shape[1]*shape[2], shape[3]])
Upvotes: 3