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

前端 未结 3 859
自闭症患者
自闭症患者 2020-11-29 06:08

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.
相关标签:
3条回答
  • 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)
    
    0 讨论(0)
  • 2020-11-29 06:13

    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]]))
    
    0 讨论(0)
  • 2020-11-29 06:14

    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!

    0 讨论(0)
提交回复
热议问题