检查模型输入时出错:期望lstm_1_input有3个维度,但得到了具有形状的数组(339732,29)

时间:2022-12-29 19:52:23

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

我的输入只是一个包含339732行和两列的csv文件:

  • the first being 29 feature values, i.e. X
  • 第一个是29个特征值,即X
  • the second being a binary label value, i.e. Y
  • 第二个是一个二元标签值,即Y

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

我正在尝试将我的数据训练在一个堆叠的LSTM模型上:

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)

回溯(最近一次调用):File“first_approach”。在模型中,第80行。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)

ValueError:检查模型输入时出错:期望lstm_1_input有3个维度,但得到具有形状的数组(339732,29)

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

我尝试使用X_train重新调整输入。整形((1,339732,29)),但没有显示错误:

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

ValueError:检查模型输入时出错:期望lstm_1_input具有shape (None, 8,29),但具有shape(1,339732, 29)的数组

How can I feed in my input to the LSTM ?

如何向LSTM提供输入?

2 个解决方案

#1


4  

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

设置timesteps = 1(因为每个实例需要一个timestep),并将X_train和X_test重新设置为:

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!

这个工作!

#2


1  

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

对于timesteps != 1,您可以使用下面的函数(改编自这里)

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

#1


4  

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

设置timesteps = 1(因为每个实例需要一个timestep),并将X_train和X_test重新设置为:

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!

这个工作!

#2


1  

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

对于timesteps != 1,您可以使用下面的函数(改编自这里)

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