【问题标题】:LSTM keras - Value error how to resolve input dimensionLSTM keras - 值错误如何解决输入维度
【发布时间】:2018-10-23 03:55:48
【问题描述】:

我已经为训练数据创建了一个函数,它可以选择正数或负数的随机文件。这是一个二分类问题。

    model=Sequential()
InputBatch = np.expand_dims(InputBatch, 0)
print(InputBatch.shape)
model.add(LSTM(100,input_shape=(1,6,30),return_sequences=True))
model.compile(loss='mean_absolute_error',optimizer='adam',metrics=['accuracy'])
model.fit(InputBatch,PositiveOrNegativeLabel,batch_size=6,nb_epoch=10,verbose=1,validation_split=0.05)

InputBatch 变量的形状为 (1,6,30)

例如我的输入数据是

[[   nan  1520.  1295.    nan  8396.  9322. 12715.    nan  5172.  7232.
  11266.    nan 11266.  2757.  4416. 12020. 12111.     0.     0.     0.
      0.     0.     0.     0.     0.     0.     0.     0.     0.     0.]
 [   nan  3045. 11480.   900.  5842. 11496.  4463.    nan 11956.   900.
  10400.  8022.  2504. 12106.     0.     0.     0.     0.     0.     0.
      0.     0.     0.     0.     0.     0.     0.     0.     0.     0.]
 [   nan  9307. 12003.  2879.  6398.  9372.  4614.  5222.    nan    nan
   2879. 10364.  6923.  4709.  4860. 11871.     0.     0.     0.     0.
      0.     0.     0.     0.     0.     0.     0.     0.     0.     0.]
 [   nan  6689.  2818. 12003.  6480.    nan     0.     0.     0.     0.
      0.     0.     0.     0.     0.     0.     0.     0.     0.     0.
      0.     0.     0.     0.     0.     0.     0.     0.     0.     0.]
 [   nan  3395.  1087. 11904.  7232.  8840. 10115.  4494. 11516.  7441.
   8535. 12106.     0.     0.     0.     0.     0.     0.     0.     0.
      0.     0.     0.     0.     0.     0.     0.     0.     0.     0.]
 [   nan  1287.   420.  4070. 11087.  7410. 12186.  2387. 12111.     0.
      0.     0.     0.     0.     0.     0.     0.     0.     0.     0.
      0.     0.     0.     0.     0.     0.     0.     0.     0.     0.]]

我已设置数据的形状为 (6,30)

我收到值错误

ValueError: Error when checking input: expected lstm_16_input to have 3 dimensions, but got array with shape (1,6, 30)

它正在获得三维输入,我不明白如何以及为什么

【问题讨论】:

  • 尝试以(None, 6, 32)的格式传递数据
  • 让我试试这个@Novak
  • @Novak 不工作我想我必须将我的数据重塑为三个维度

标签: python neural-network keras lstm dimensions


【解决方案1】:

LSTM 输入必须在3 dimensional(samples,timesteps,features) 中。而且您的数据似乎是二维的。您可以使用numpys reshape() 函数将数据转换为 3D。

例如,如果您使用 1 个时间步,则必须将其重塑为 array.reshape(6,1,30),或者如果您使用 6 个时间步,则将其重塑为 array.reshape(1,6,30)

有关重塑 LSTM 输入的更多信息,您可以查看site

[[更新]] 你的代码有很多问题

 model=Sequential()
InputBatch = np.expand_dims(InputBatch, 0)
print(InputBatch.shape)
model.add(LSTM(100,input_shape=(1,6,30),return_sequences=True))
model.compile(loss='mean_absolute_error',optimizer='adam',metrics=['accuracy'])
model.fit(InputBatch,PositiveOrNegativeLabel,batch_size=6,nb_epoch=10,verbose=1,validation_split=0.05)

当您将数据转换为 (1,6,30) 时,您基本上是在说您只有一个样本(只有 1 个),batch_size 为 6 但您只有 1 个样本,您只有一个样本但您正在做验证拆分。由于您只有一个 X 值,因此它将只有一个 Y(PositiveOrNegativeLabel),所以我只分配了一个值,即 1。

我已经用你在问题中显示的代码和数据的一些更改运行了你的程序(我将 NA 更改为 0):

    a=np.array([ 
             [0,1520,1295,0,8396,9322,12715,0,5172,7232,11266,0,11266,2757,4416,12020,12111,0,0,0,0,0,0,0,0,0,0,0,0,0],
             [0,3045,11480,900,5842,11496,4463,0,11956,900,10400,8022,2504,12106,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
             [0,9307,12003,2879,6398,9372,4614,5222,0,0,2879,10364,6923,4709,4860,11871,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
             [0,6689,2818,12003,6480,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
             [0,3395,1087,11904,7232,8840,10115,4494,11516,7441,8535,12106,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
             [0,1287,420,4070,11087,7410,12186,2387,12111,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
           ]
          )

PositiveOrNegativeLabel=np.array([[1]])
PositiveOrNegativeLabel=PositiveOrNegativeLabel.reshape(1,-1)
PositiveOrNegativeLabel.shape
InputBatch =InputBatch.reshape(1,6,30)
InputBatch.shape
model=Sequential()
model.add(LSTM(1,input_shape=(6,30)))
model.add(Dense(1,activation="sigmoid"))
model.compile(loss='mean_absolute_error',optimizer='adam',metrics=['accuracy'])
model.fit(InputBatch,PositiveOrNegativeLabel,batch_size=1,verbose=1)

【讨论】:

  • 能不能给个完整的答案让我接受
  • @TayyabGulsherVohra 我已经更新了。如果您有任何疑问,可以澄清一下。
  • 它不工作我已经尝试了 array.reshape 和 np.expand_dims 我得到这个错误 Input 0 is incompatible with layer lstm_33: expected ndim=3, found ndim=4
  • 您能否将代码粘贴到您尝试重塑的位置,看来您的阵列已被重塑为 4D 阵列而不是 3D 阵列。在 array.reshape(6,1,30) 中,我假设根据您的示例只有 6 个样本,每个样本有 30 个特征。在 LSTM 层中指定时,您只需提及时间步长和特征数量,即 input_shape=(1,30)
  • 现在当我打印我得到的形状时 (1,6,30)
猜你喜欢
  • 2019-01-21
  • 2022-01-20
  • 1970-01-01
  • 2023-03-05
  • 2018-02-10
  • 2018-06-16
  • 2018-09-18
  • 1970-01-01
相关资源
最近更新 更多