问题
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).
回答1:
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)
来源:https://stackoverflow.com/questions/53439765/in-keras-how-to-use-reshape-layer-with-none-dimension