简体   繁体   English

在Keras中,如何使用无尺寸的Reshape图层?

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

In my model, a layer has a shape of [None, None, 40, 64] . 在我的模型中,层的形状为[None, None, 40, 64] I want to reshape this into [None, None, 40*64] . 我想将其重塑为[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 . 引发错误,提示None values not supported

(Just to be clear, this is not tf.keras , this is just Keras). (请注意,这不是tf.keras ,这只是tf.keras )。

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. 首先,传递给“ Reshape形状”层的参数是批次中一个样本的所需形状,而不是整个批次的样本。 So since each of the samples in the batch is a 3D tensor, the argument must also consider only that 3D tensor (ie excluding the batch axis). 因此,由于批次中的每个样本都是3D张量,因此该参数还必须仅考虑该3D张量(即,不包括批次轴)。

Second, you can use -1 as the shape of only one axis. 其次,您可以将-1用作仅一个轴的形状。 It tells to the Reshape layer to automatically infer the shape of that axis based on the shape of other axes you provide. 它告知Reshape图层根据您提供的其他轴的形状自动推断该轴的形状。 So considering these two points, it would be: 因此,考虑到这两点,将是:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM