【问题标题】:Keras Reshape layer adding an extra dimension?Keras Reshape 图层添加了额外的维度?
【发布时间】:2019-01-17 11:54:58
【问题描述】:

Reshape 层没有按我的预期工作。在下面的示例中,我认为最后一行应该返回一个形状为[5,1] 的张量对象。但是会抛出一个错误,指出形状 [5] tensor 不能重新整形为大小 [5,5,1] tensor。

>>> from keras.layers import Reshape
>>> from keras import backend as K
>>> import numpy as np
>>> x = K.constant(np.array([1,2,3,4,5]))
>>> K.eval(x)
array([1., 2., 3., 4., 5.], dtype=float32)
>>> Reshape(target_shape=(5,1))(x)
...
ValueError: Cannot reshape a tensor with 5 elements to
shape [5,5,1] (25 elements) for 'reshape_3/Reshape' (op: 
'Reshape') with input shapes: [5], [3] and with input 
tensors computed as partial shapes: input[1] = [5,5,1].

有人可以解释一下 Reshape 层是如何工作的(即为什么要添加额外的暗淡)以及如何将向量重塑为矩阵的过程?

谢谢

【问题讨论】:

    标签: keras reshape tensor


    【解决方案1】:

    用户Reshape(target_shape=(1,))(x)

    batch_size 隐含在整个模型中,从头到尾都被忽略。

    如果您确实想访问批量大小,请使用 K.reshape(x,(5,1))

    如果不创建完全由层组成的模型,则不应使用 Keras。

    【讨论】:

      猜你喜欢
      • 2019-04-25
      • 2021-02-13
      • 2019-11-19
      • 2022-01-16
      • 2023-03-07
      • 2015-04-07
      • 1970-01-01
      • 2019-06-18
      • 1970-01-01
      相关资源
      最近更新 更多