gpt4 book ai didi

python - 检查模型输入 : expected lstm_1_input to have 3 dimensions, 时出错,但得到的数组具有形状 (339732, 29)

转载 作者:IT老高 更新时间:2023-10-28 20:53:16 26 4
gpt4 key购买 nike

我的输入只是一个包含 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)) reshape 我的输入,但它没有显示错误:

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 reshape 为:

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

成功了!

关于python - 检查模型输入 : expected lstm_1_input to have 3 dimensions, 时出错,但得到的数组具有形状 (339732, 29),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44704435/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com