我正在尝试使用 Keras 构建人工神经网络。模型的输入尺寸为 (5, 5, 2),而输出尺寸为 (5,5)。在运行 keras.fit() 函数时,我遇到以下错误:ValueError: Error when checking target: expected dense_3 to have 4 dimensions, but got array with shape (5, 5)这是我正在执行的代码from keras.models import Sequentialfrom keras.layers import Dense, Flattenimport matplotlib.pyplot as pltfrom keras.callbacks import EarlyStopping, ModelCheckpointmodel = Sequential()model.add(Dense(1000, input_shape=(5, 5, 2), activation="relu"))model.add(Dense(1000, activation="relu"))model.add(Dense(2), output_shape=(5,5))model.summary()model.compile(optimizer="adam",loss="mse", metrics = ["mse"])monitor_val_acc = EarlyStopping(monitor="loss", patience = 10)history = model.fit(trainX, trainYbliss, epochs=1000, validation_data=(testX, testY), callbacks = [monitor_val_acc], verbose = 1)clinical = model.predict(np.arange(0, len(testY)))这是网络的架构:Layer (type) Output Shape Param # =================================================================dense_1 (Dense) (None, 5, 5, 1000) 3000 _________________________________________________________________dense_2 (Dense) (None, 5, 5, 1000) 1001000 _________________________________________________________________dense_3 (Dense) (None, 5, 5, 1) 1001 =================================================================Total params: 1,005,001Trainable params: 1,005,001Non-trainable params: 0_________________________________________________________________模型应该基于 (5,5,2) 数组输出 (5,5) 数组,但在最低隐藏层失败。我该如何解决这个问题?
2 回答
开满天机
TA贡献1786条经验 获得超13个赞
使用下面的代码作为参考根据您的输入值更改值:
train_data = train_data.reshape(train_data.shape[0], 10, 30, 30, 1)
对于您的输入火车数据,
慕尼黑8549860
TA贡献1818条经验 获得超11个赞
您的网络将输出一个 shape 的张量(batch_size, 5, 5, 1)
。您的输出是 4 维张量吗?如果它是一个单一的价值,(5,5)
你需要将它重塑成(1,5,5,1)
我认为
添加回答
举报
0/150
提交
取消