检查模型输入时出错:预期 lstm_1_input 有 3 个维度,但得到了形状为 (339732, 29) 的数组
Error when checking model input: expected lstm_1_input to have 3 dimensions, but got array with shape (339732, 29)
我的输入只是一个包含 339732 行和两列的 csv 文件:
- 第一个是29个特征值,即X
- 第二个是二进制标签值,即 Y
我正在尝试在堆叠式 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)
这会引发错误:
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)
我尝试使用 X_train.reshape((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)
如何将输入输入到 LSTM?
设置 timesteps = 1
(因为我希望每个实例都有一个时间步长)并将 X_train 和 X_test 重塑为:
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]))
这成功了!
对于 timesteps != 1
,您可以使用以下函数(改编自 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)
例子
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]]))
重塑 LSTM 的输入:
X = array([[10, 20, 30], [40, 50, 60], [70, 80, 90]])
X_train = X.reshape(1, 3, 3) # X.reshape(samples, timesteps, features)
我的输入只是一个包含 339732 行和两列的 csv 文件:
- 第一个是29个特征值,即X
- 第二个是二进制标签值,即 Y
我正在尝试在堆叠式 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)
这会引发错误:
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)
我尝试使用 X_train.reshape((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)
如何将输入输入到 LSTM?
设置 timesteps = 1
(因为我希望每个实例都有一个时间步长)并将 X_train 和 X_test 重塑为:
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]))
这成功了!
对于 timesteps != 1
,您可以使用以下函数(改编自 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)
例子
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]]))
重塑 LSTM 的输入:
X = array([[10, 20, 30], [40, 50, 60], [70, 80, 90]])
X_train = X.reshape(1, 3, 3) # X.reshape(samples, timesteps, features)