pitfall
pitfall

Reputation: 2621

How to reshape a tensor with multiple `None` dimensions?

I encountered a problem to reshape an intermediate 4D tensorflow tensor X to a 3D tensor Y, where

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

Answers (1)

Anthony D'Amato
Anthony D'Amato

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

Related Questions