Search code examples
pythonneural-networkkeraslstmdimensions

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)

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)

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


Solution

  • LSTM inputs have to be in 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.

    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)

    For more information on reshaping input for LSTM you can check this site

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

    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) :

        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)