Keras Reshape 层添加一个额外的维度?

Keras Reshape layer adding an extra dimension?

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

>>> 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(target_shape=(1,))(x)

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

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

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