【问题标题】:ValueError: total size of new array must be unchanged (Reshape from keras)ValueError:新数组的总大小必须保持不变(从 keras 重塑)
【发布时间】:2019-12-16 01:41:48
【问题描述】:

对于这个玩具模型:

from keras.layers import Input, Dense, Reshape
from keras.models import Model

# this is the size of our encoded representations
compression = 10  

input_img = Input(shape=(28,28, ), name= "28x28")
encoded = Dense(int(np.floor(28*28/compression)), activation='relu', 
                name= "encoder_" + str(compression))(input_img)
decoded = Dense(units = 28*28, activation='sigmoid',
                name = "28.28_decoder")(encoded)
reshape = Reshape(target_shape = (28,28), name = "28x28_reshape")(decoded)
autoencoder = Model(input_img, reshape)

我得到错误:

ValueError                                Traceback (most recent call last)
<ipython-input-27-04b835543369> in <module>
     12 decoded = Dense(units = 28*28, activation='sigmoid',
     13                 name = "28.28_decoder")(encoded)
---> 14 reshape = Reshape(target_shape = (28,28), name = "28x28_reshape")(decoded)
     15 autoencoder = Model(input_img, reshape)
     16 

~/.local/lib/python3.6/site-packages/keras/engine/base_layer.py in __call__(self, inputs, **kwargs)
    472             if all([s is not None
    473                     for s in to_list(input_shape)]):
--> 474                 output_shape = self.compute_output_shape(input_shape)
    475             else:
    476                 if isinstance(input_shape, list):

~/.local/lib/python3.6/site-packages/keras/layers/core.py in compute_output_shape(self, input_shape)
    396             # input shape known? then we can compute the output shape
    397             return (input_shape[0],) + self._fix_unknown_dimension(
--> 398                 input_shape[1:], self.target_shape)
    399 
    400     def call(self, inputs):

~/.local/lib/python3.6/site-packages/keras/layers/core.py in _fix_unknown_dimension(self, input_shape, output_shape)
    384             output_shape[unknown] = original // known
    385         elif original != known:
--> 386             raise ValueError(msg)
    387 
    388         return tuple(output_shape)

ValueError: total size of new array must be unchanged

我一直试图找出原因,但我不明白。看看help page,它相当简单,因为前一层是执行重塑的足够形状。

【问题讨论】:

  • Dense 的文档中写着Output shape: nD tensor with shape: (batch_size, ..., units). For instance, for a 2D input with shape (batch_size, input_dim), the output would have shape (batch_size, units).
  • @Ardweaden 但是文档说 target_shape:目标形状。整数元组,不包括样本维度(批量大小)。
  • The Dense layer is applied on the last dimension。因此,encoded 的形状为 (None, 28, 78)decoded 的形状为 (None, 28, 784)。因此,您不能将形状为 (28,784) 的张量调整为 (28,28)。解决这个问题的一种方法是在输入层之后使用Flatten 层。

标签: python numpy tensorflow keras python-3.6


【解决方案1】:

您解码的密集层有 28*28 单位,这将使其输出暗淡为 (,28,784),不会重塑为 28*28。

【讨论】:

  • 例如,对于形状为 (batch_size, input_dim) 的 2D 输入,输出将具有形状 (batch_size, units) - 来自密集层帮助页面。我不明白。你能详细说明一下吗?
  • 只有最后一个维度会影响权重的大小,在您的情况下为 28 (github.com/keras-team/keras/blob/…)。您的解码层输入形状是 (None, 28, 78),将最后一个 dim 替换为 28*28,您的解码层输出 dim 原来是 (None, 28, 784)。
【解决方案2】:

根据您想要做的事情,您应该事先展平阵列。这应该有效:

flatten = Flatten()(input_img)
encoded = Dense(int(np.floor(28*28/compression)), activation='relu',  name= "encoder_" + str(compression))(flatten)
decoded = Dense(units = 28*28, activation='sigmoid', name = "28.28_decoder" (encoded)
reshape = Reshape(target_shape = (28,28), name = "28x28_reshape")(decoded)
autoencoder = Model(input_img, reshape)

我假设初始重塑不起作用,因为您正在通过密集层传递 3D 张量,并且初始尺寸被弄乱了。因此,您可以将其恢复为初始形状。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-18
    • 1970-01-01
    • 1970-01-01
    • 2016-06-09
    • 2019-01-18
    • 1970-01-01
    相关资源
    最近更新 更多