【问题标题】:How to convert an input_shape to 3 dim "input to have 3 dimensions, but got array with shape (35, 33297)"如何将 input_shape 转换为 3 dim “输入具有 3 个维度,但得到的数组具有形状 (35, 33297)”
【发布时间】:2019-08-07 01:51:50
【问题描述】:

无法为我的模型找到确切的输入形状,它具有 (35,33297) 形状

我已经用 np.expand dims 试过了,但它也不起作用

model=Sequential()
model.add(LSTM(50,return_sequences=True, input_shape=(X_train.shape)))
model.add(LSTM(32, return_sequences=True ))
model.add(Dense(2, activation='softmax'))
model.compile(loss='binary_crossentropy',
              optimizer='adam',
              metrics=['accuracy'])```
```print("Train...")
model.fit(X_train,y_train,batch_size=5,epochs=10,verbose=1)```


```Train...
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-123-a64af5eee5e8> in <module>()
      1 print("Train...")
----> 2 model.fit(X_train,y_train,batch_size=5,epochs=10,verbose=1)

~\Anaconda3\lib\site-packages\keras\engine\training.py in fit(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, **kwargs)
    950             sample_weight=sample_weight,
    951             class_weight=class_weight,
--> 952             batch_size=batch_size)
    953         # Prepare validation data.
    954         do_validation = False

ValueError: Error when checking input: expected lstm_37_input to have 3 dimensions, but got array with shape (35, 33297)```

【问题讨论】:

  • 你给了 LSTM 一个大小为(35, 33297) 的数组。但是 LSTM 需要一个 [batch_size, seq length, num features] 数组。你在使用np.expand_dims时遇到了什么错误?
  • 我在执行时确实遇到了任何错误
  • 但如果我使用的是批量大小,则表明 dim 大于 3

标签: tensorflow keras deep-learning lstm


【解决方案1】:
model.add(LSTM(50,return_sequences=True, input_shape=(timesteps, inputs)))

你必须像这样定义你的 LSTM。输入形状必须是(时间步长,输入)。 timesteps 是一个输入序列的大小,并输入每个时间步您给 LSTM 的变量数。而且您的训练数据必须是 (样本、时间步长、输入)。

【讨论】:

  • 我已经创建了这些模型,输入尺寸为 3,但它也显示错误 model=Sequential() model.add(LSTM(2,return_sequences=True, input_shape=(X_train.shape))) model.add(LSTM(32, return_sequences=True )) model.add(Dense(2, activation='softmax')) model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
  • ValueError: Input 0 is incompatible with layer lstm_6: expected ndim=3, found ndim=4
  • 你能打印吗(X_train.shape)。我很确定当你定义你的第一个 LSTM 层时你有 3 个暗淡,而不是 2 个
  • 是的,它是 3 暗我通过使用 np.expand_dim 方法扩展了尺寸,然后我打印它然后显示 ``` (1,35,33297)```
  • 正如我在回答中提到的那样,您已经将其定义为 model.add(LSTM(50,return_sequences=True, input_shape=(X_train.shape[1], X_train.shape[2]) ))
猜你喜欢
  • 1970-01-01
  • 2018-07-25
  • 2018-01-27
  • 2018-08-03
  • 2019-09-04
  • 2017-11-26
  • 2019-04-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多