Q:

Reshaping a tensor with more than one uknown diemnsions

I need to be able to reshape a tensor only on its last axis:
(None, 4) --> (None, 2, 2),

which at time of execution can have instances like these:
(128, 10, 4) --> (128, 10, 2, 2)
(128, 4) --> (128, 2, 2)

Is there a straight forward solution or I need to iterate on the first axes (by excluding the last one), and considering the case that it can be None?

A:

You can do that like this:
my_tensor = ...
new_shape = tf.concat([tf.shape(my_tensor)[:-1], [2, 2]], axis=0)
my_tensor_reshaped = tf.reshape(my_tensor, new_shape)

