简体   繁体   English

Keras LSTM 层值错误:尺寸必须相等,但为 17 和 2

[英]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.

相关问题 ValueError:尺寸必须相等(keras) - ValueError: Dimensions must be equal (keras) ValueError:尺寸必须相等,输入形状 - ValueError: Dimensions must be equal, with input shapes tensorflow Keras:维度必须相等 ValueError - tensorflow Keras: Dimenions must be equal ValueError LSTM Keras ValueError - LSTM Keras ValueError keras LSTM模型中的尺寸不匹配 - Dimensions not matching in keras LSTM model Keras Layer LSTM中的输入不兼容 - Incompatible input in Keras Layer LSTM ValueError:尺寸必须相等,但输入形状为 100 和 19:[?,100], [?,100,19] - ValueError: Dimensions must be equal, but are 100 and 19 with input shapes: [?,100], [?,100,19] ValueError:尺寸必须相等,但对于'MatMul_1'(op:'MatMul'),输入形状为784和500:[?,784],[500,500] - ValueError: Dimensions must be equal, but are 784 and 500 for 'MatMul_1' (op: 'MatMul') with input shapes: [?,784], [500,500] ValueError:尺寸必须相等,但对于具有输入形状的“mul_18”(操作:“Mul”)为 2 和 80:[?,?,?,5,2], [?,?,?,5,80] - ValueError: Dimensions must be equal, but are 2 and 80 for 'mul_18' (op: 'Mul') with input shapes: [?,?,?,5,2], [?,?,?,5,80] Keras LSTM和嵌入连接数组维度问题 - Keras LSTM and Embedding Concatenate Array Dimensions Issue
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM