简体   繁体   English

LSTM keras-值错误如何解析输入维

[英]LSTM keras - Value error how to resolve input dimension

I have made a function for training data which pick random files either positive or negative. 我提供了一个用于训练数据的功能,该功能可以选择正或负的随机文件。 it is a binary classification problem. 这是一个二进制分类问题。

    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)

the shape of InputBatch variable is (1,6,30) InputBatch变量的形状为(1,6,30)

for example my input data is 例如我的输入数据是

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

I have set the shape of data which is (6,30) 我将数据的形状设置为(6,30)

I am getting value error 我收到价值错误

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

it is getting three dimension input i don't understand how and why 它正在获取三维输入,我不知道如何以及为什么

LSTM inputs have to be in 3 dimensional(samples,timesteps,features) . LSTM输入必须是3 dimensional(samples,timesteps,features) And your data seems to be in 2D. 而且您的数据似乎是二维的。 You can use the numpys reshape() function to convert your data to 3D. 您可以使用numpys reshape()函数将数据转换为3D。

For example if you are using 1 time-step then you will have to reshape it as array.reshape(6,1,30) or if you are using 6 timestep then array.reshape(1,6,30) 例如,如果您使用1个时间步长,则必须将其array.reshape(6,1,30)array.reshape(6,1,30)如果您使用6个时间步长,则必须将其array.reshape(6,1,30)array.reshape(6,1,30) array.reshape(1,6,30)

For more information on reshaping input for LSTM you can check this site 有关重塑LSTM输入的更多信息,请访问此站点。

[[update]] There are so many problems with your code [[更新]]您的代码有太多问题

 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)

When you convert your data to (1,6,30) you are basically saying that you have just one sample(only 1) ,batch_size is 6 but you have only 1 sample,you have only one sample but you are doing validation split.And since you have only one X value it will have only one Y ( PositiveOrNegativeLabel),So I have asisgneed just one value ie 1. 当您将数据转换为(1,6,30)时,基本上是在说您只有一个样本(只有1),batch_size为6,但是只有1个样本,只有一个样本,但是您正在进行验证拆分。而且由于您只有一个X值,所以它将只有一个Y(PositiveOrNegativeLabel),因此我只建议了一个值,即1。

I have ran your program with the changes few changes with code and data that u have shown in the question (i changed NA to 0) : 我已经运行了您的程序,所做的更改几乎没有改变您在问题中显示的代码和数据(我将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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM