foobar
foobar

Reputation: 11374

In Keras, how to use Reshape layer with None dimension?

In my model, a layer has a shape of [None, None, 40, 64]. I want to reshape this into [None, None, 40*64]. However, if I simply do the following:

reshaped_layer = Reshape((None, None, 40*64))(my_layer)

It throws an error complaining that None values not supported.

(Just to be clear, this is not tf.keras, this is just Keras).

Upvotes: 6

Views: 5668

Answers (1)

today
today

Reputation: 33410

First of all, the argument you pass to Reshape layer is the desired shape of one sample in the batch and not the whole batch of samples. So since each of the samples in the batch is a 3D tensor, the argument must also consider only that 3D tensor (i.e. excluding the batch axis).

Second, you can use -1 as the shape of only one axis. It tells to the Reshape layer to automatically infer the shape of that axis based on the shape of other axes you provide. So considering these two points, it would be:

reshaped_out = Reshape((-1, 40*64))(layer_out)

Upvotes: 6

Related Questions