[英]Keras LSTM Layer ValueError: Dimensions must be equal, but are 17 and 2
I'm working on a basic RNN model for a multiclass task and I'm facing some issues with output dimensions.我正在为一个多类任务开发一个基本的 RNN model,我在 output 尺寸方面遇到了一些问题。
This is my input/output shapes:这是我的输入/输出形状:
input.shape = (50000, 2, 5) # (samples, features, feature_len)
output.shape = (50000, 17, 185) # (samples, features, feature_len) <-- one hot encoded
input[0].shape = (2, 5)
output[0].shape = (17, 185)
This is my model, using Keras functional API:这是我的 model,使用 Keras 功能 API:
inp = tf.keras.Input(shape=(2, 5,))
x = tf.keras.layers.LSTM(128, input_shape=(2, 5,), return_sequences=True, activation='relu')(inp)
out = tf.keras.layers.Dense(185, activation='softmax')(x)
model = tf.keras.models.Model(inputs=inp, outputs=out)
This is my model.summary()
:这是我的
model.summary()
:
Layer (type) Output Shape Param #
=================================================================
input_1 (InputLayer) [(None, 2, 5)] 0
_________________________________________________________________
lstm (LSTM) (None, 2, 128) 68608
_________________________________________________________________
dense (Dense) (None, 2, 185) 23865
=================================================================
Total params: 92,473
Trainable params: 92,473
Non-trainable params: 0
_________________________________________________________________
Then I compile the model and run fit()
:然后我编译 model 并运行
fit()
:
model.compile(optimizer='adam',
loss=tf.nn.softmax_cross_entropy_with_logits,
metrics='accuracy')
model.fit(x=input, y=output, epochs=5)
And I'm getting a dimension error:我得到一个尺寸错误:
ValueError: Dimensions must be equal, but are 17 and 2 for '{{node Equal}} = Equal[T=DT_INT64, incompatible_shape_error=true](ArgMax, ArgMax_1)' with input shapes: [?,17], [?,2].
The error is clear, the model output a dimension 2
and my output has dimension 17
, although I understand the issue, I can't find a way of fixing it, any ideas?错误很明显,model output 尺寸为
2
而我的 output 尺寸为17
,虽然我理解这个问题,但我找不到解决方法,有什么想法吗?
I think your output shape is not "output[0].shape = (17, 185)" but "dense (Dense) (None, 2, 185) ".我认为您的 output 形状不是“输出 [0].shape = (17, 185)”,而是“密集 (Dense) (None, 2, 185)”。
You need to change your output shape or change your layer structure.您需要更改 output 形状或更改图层结构。
LSTM output is a list of encoder_outputs
, when you specify return_sequences=True
.当您指定
return_sequences=True
时,LSTM output 是encoder_outputs
的列表。 hence;因此; I suggest just using the last item of
encoder_outputs
as the input of your Dense layer.我建议只使用最后一项
encoder_outputs
作为密集层的输入。 you can see the example section of this link to the documentation .您可以查看此文档链接的示例部分。 It may help you.
它可能会帮助你。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.