34

My input is simply a csv file with 339732 rows and two columns :

  • the first being 29 feature values, i.e. X
  • the second being a binary label value, i.e. Y

I am trying to train my data on a stacked LSTM model:

data_dim = 29
timesteps = 8
num_classes = 2

model = Sequential()
model.add(LSTM(30, return_sequences=True,
               input_shape=(timesteps, data_dim)))  # returns a sequence of vectors of dimension 30
model.add(LSTM(30, return_sequences=True))  # returns a sequence of vectors of dimension 30
model.add(LSTM(30))  # return a single vector of dimension 30
model.add(Dense(1, activation='softmax'))

model.compile(loss='binary_crossentropy',
              optimizer='rmsprop',
              metrics=['accuracy'])

model.summary()
model.fit(X_train, y_train, batch_size = 400, epochs = 20, verbose = 1)

This throws the error:

Traceback (most recent call last): File "first_approach.py", line 80, in model.fit(X_train, y_train, batch_size = 400, epochs = 20, verbose = 1)

ValueError: Error when checking model input: expected lstm_1_input to have 3 dimensions, but got array with shape (339732, 29)

I tried reshaping my input using X_train.reshape((1,339732, 29)) but it did not work showing error:

ValueError: Error when checking model input: expected lstm_1_input to have shape (None, 8, 29) but got array with shape (1, 339732, 29)

How can I feed in my input to the LSTM ?

Saurav--
  • 1,360
  • 2
  • 12
  • 29

3 Answers3

40

Setting timesteps = 1 (since, I want one timestep for each instance) and reshaping the X_train and X_test as:

import numpy as np
X_train = np.reshape(X_train, (X_train.shape[0], 1, X_train.shape[1]))
X_test = np.reshape(X_test, (X_test.shape[0], 1, X_test.shape[1]))

This worked!

E. Zeytinci
  • 2,323
  • 1
  • 12
  • 30
Saurav--
  • 1,360
  • 2
  • 12
  • 29
4

For timesteps != 1, you can use the below function (adapted from here)

import numpy as np
def create_dataset(dataset, look_back=1):
  dataX, dataY = [], []
  for i in range(len(dataset)-look_back+1):
    a = dataset[i:(i+look_back), :]
    dataX.append(a)
    dataY.append(dataset[i + look_back - 1, :])
  return np.array(dataX), np.array(dataY)

Examples

X = np.reshape(range(30),(3,10)).transpose()
array([[ 0, 10, 20],
       [ 1, 11, 21],
       [ 2, 12, 22],
       [ 3, 13, 23],
       [ 4, 14, 24],
       [ 5, 15, 25],
       [ 6, 16, 26],
       [ 7, 17, 27],
       [ 8, 18, 28],
       [ 9, 19, 29]])

create_dataset(X, look_back=1 )
(array([[[ 0, 10, 20]],
       [[ 1, 11, 21]],
       [[ 2, 12, 22]],
       [[ 3, 13, 23]],
       [[ 4, 14, 24]],
       [[ 5, 15, 25]],
       [[ 6, 16, 26]],
       [[ 7, 17, 27]],
       [[ 8, 18, 28]],
       [[ 9, 19, 29]]]),
array([[ 0, 10, 20],
       [ 1, 11, 21],
       [ 2, 12, 22],
       [ 3, 13, 23],
       [ 4, 14, 24],
       [ 5, 15, 25],
       [ 6, 16, 26],
       [ 7, 17, 27],
       [ 8, 18, 28],
       [ 9, 19, 29]]))

create_dataset(X, look_back=3)
(array([[[ 0, 10, 20],
        [ 1, 11, 21],
        [ 2, 12, 22]],
       [[ 1, 11, 21],
        [ 2, 12, 22],
        [ 3, 13, 23]],
       [[ 2, 12, 22],
        [ 3, 13, 23],
        [ 4, 14, 24]],
       [[ 3, 13, 23],
        [ 4, 14, 24],
        [ 5, 15, 25]],
       [[ 4, 14, 24],
        [ 5, 15, 25],
        [ 6, 16, 26]],
       [[ 5, 15, 25],
        [ 6, 16, 26],
        [ 7, 17, 27]],
       [[ 6, 16, 26],
        [ 7, 17, 27],
        [ 8, 18, 28]],
       [[ 7, 17, 27],
        [ 8, 18, 28],
        [ 9, 19, 29]]]),
array([[ 2, 12, 22],
       [ 3, 13, 23],
       [ 4, 14, 24],
       [ 5, 15, 25],
       [ 6, 16, 26],
       [ 7, 17, 27],
       [ 8, 18, 28],
       [ 9, 19, 29]]))
Shadi
  • 7,343
  • 3
  • 34
  • 58
  • Getting this error when I pass the object returned from `create_dataset()` into `model.fit()` `AttributeError: 'tuple' object has no attribute 'shape'` – jlewkovich Mar 19 '19 at 01:45
  • `create_dataset` returns a tuple of `x, y`. Try `x_train, y_train = create_dataset(dataset)` and then `model.fit(x_train, y_train)` – Shadi Mar 19 '19 at 01:53
  • I have an input training set that is a np.array that is 100 rows and 50 columns. Some of those columns contain float values, some contain "hot encodings" built with `keras.utils.to_categorical()` which are basically just arrays. I'm confused as to how I'd use `x_train` and `y_train`. My training labels are in a separate array, the input model just contains the training data (first input into model.fit()) – jlewkovich Mar 19 '19 at 02:01
  • In this case, just ignore `y_train` from this function and use the one that you have already in your separate array. Also, model.fit would take in the `x_train` from this function and your own target – Shadi Mar 19 '19 at 02:29
  • still not working, I've opened a bounty on a question that mimics my problem: https://stackoverflow.com/questions/51469446/keras-and-error-setting-an-array-element-with-a-sequence Basically handling, in a Sequential layer, a training set that contains both arrays and numeric values – jlewkovich Mar 19 '19 at 03:03
  • I just posted [an answer](https://stackoverflow.com/a/55233535/4126114) for you there with a working jupyter notebook – Shadi Mar 19 '19 at 04:12
  • @JLewkovich did my answer help you? – Shadi Mar 25 '19 at 07:12
4

Reshape input for LSTM:

X = array([[10, 20, 30], [40, 50, 60], [70, 80, 90]])
X_train = X.reshape(1, 3, 3) # X.reshape(samples, timesteps, features)
Ashok Kumar Jayaraman
  • 2,171
  • 2
  • 24
  • 34