【问题标题】:LSTM Dimension are IncompatibleLSTM 维度不兼容
【发布时间】:2021-12-04 17:46:18
【问题描述】:

我正在使用 LSTM 架构处理多分类问题。它似乎有一个不兼容的形状错误。请帮我调试模型。提前致谢。

这里我提供了模型:

# build the network
model = Sequential()
model=models.Sequential()
model.add(layers.LSTM(1024,activation='tanh',input_shape=x_train.shape[1:], return_sequences=True))
model.add(layers.LSTM(512,activation='tanh',return_sequences=True))
model.add(layers.Flatten())
model.add(layers.Dense(3,activation='sigmoid'))
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
model.summary()

这是这个 LSTM 模型的总结:

finished loading 7740 subjects from 3 classes
train / test split: 6192, 1548
training data shape:  (6192, 16000, 1)
training labels shape:  (6192, 3)
Model: "sequential_1"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
lstm (LSTM)                  (None, 16000, 1024)       4202496   
_________________________________________________________________
lstm_1 (LSTM)                (None, 16000, 512)        3147776   
_________________________________________________________________
flatten (Flatten)            (None, 8192000)           0         
_________________________________________________________________
dense (Dense)                (None, 3)                 24576003  
=================================================================
Total params: 31,926,275
Trainable params: 31,926,275
Non-trainable params: 0

这是训练/拟合:

results = model.fit(x_train, y_train, epochs = 500, batch_size=16,validation_data= (x_test, y_test))

我得到的错误:

ValueError: Input 0 of layer lstm is incompatible with the layer: expected ndim=3, found ndim=4. Full shape received: (None, 800, 20, 1)

【问题讨论】:

  • 请避免使用粗体,重点除外(已编辑)。

标签: python deep-learning conv-neural-network lstm multiclass-classification


【解决方案1】:

乍一看,我认为您的问题来自这一行:

results = model.fit(x_train, x_train, epochs = 500, batch_size=16,validation_data= (x_test, x_test))

通常我们将训练数据拟合到标签上。在上一行中,您试图将数据拟合到自身。但是你做的模型架构不是为了那个。

所以尝试如下更改:

results = model.fit(x_train, y_train, epochs = 500, batch_size=16,validation_data= (x_test, y_test)).

这可能只是你的一个错字。更改这些行,看看它是否有效。

【讨论】:

  • 谢谢。我改变了那个。而且我收到与尺寸相关的错误。这里是: ValueError: Input 0 of layer lstm is in compatible with the layer: expected ndim=3, found ndim=4.收到的完整形状:(None, 800, 20, 1)
  • 输入形状通常应为(None,16000, 1)。该错误表示输入为(None, 800, 20, 1)。您可能需要重塑您的输入数据。例如np.reshape(x, (16000,1)).
猜你喜欢
  • 2019-06-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-01-18
  • 1970-01-01
  • 1970-01-01
  • 2019-10-23
  • 1970-01-01
相关资源
最近更新 更多