ValueError:检查目标时出错:在测试LSTM网络时,预期密集_24具有形状(1),但获得具有形状(77,)的数组

2024-04-26 10:26:09 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图建立一个LSTM神经网络,训练和测试数据集如下:

x_train=np.array(train)
x_train=x_train.reshape(51,77,6)
y_train=list(train.Close[1:])
y_train.append(train.Close[-1])
y_train=np.array(y_train).reshape(51,77)
x_test=np.array(test)
x_test=x_test.reshape(1,77, 6)
y_test=list(test.Close[1:])
y_test.append(test.Close[-1])
y_test=np.array(y_test)

LSTM网络代码为

BATCH_SIZE = 1
TIME_STEP = 77
FEATURES = 6
model = Sequential()

model.add(LSTM(64,input_shape=(TIME_STEP, FEATURES),return_sequences=True))
model.add(LSTM(32))
model.add(Dense(1, activation="softmax"))

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

model.summary()

[![][[1]][1] 当我尝试使用

model.fit(x_train, y_train,

      batch_size=BATCH_SIZE,

      nb_epoch=15,

      validation_data=(x_test, y_test))


score, acc = model.evaluate(x_test, y_test, batch_size=batch_size)

为了训练模型,它返回

ValueError: Error when checking target: expected dense_24 to have shape (1,) but got array with shape (77,)

我曾尝试更改输入训练集维度,但它仍会返回如下错误