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

倾然丶 夕夏残阳落幕 提交于 2019-12-13 01:16:19

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!