在Keras中,如何使用无尺寸的Reshape图层? [英] In Keras, how to use Reshape layer with None dimension?
问题描述
在我的模型中,层的形状为[None, None, 40, 64]
.我想将其重塑为[None, None, 40*64]
.但是,如果我只是执行以下操作:
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)
它抛出一个错误,抱怨None values not supported
.
It throws an error complaining that None values not supported
.
(请注意,这不是tf.keras
,这只是Keras).
(Just to be clear, this is not tf.keras
, this is just Keras).
推荐答案
首先,传递给Reshape
层的参数是批次中一个样本的期望形状,而不是整个批次的样本.因此,由于批次中的每个样本都是3D张量,因此该参数还必须仅考虑该3D张量(即,不包括批次轴).
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).
第二,可以将-1
用作仅一个轴的形状.它告诉Reshape
层根据您提供的其他轴的形状自动推断该轴的形状.因此,考虑到这两点,应该是:
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)
这篇关于在Keras中,如何使用无尺寸的Reshape图层?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!