如何调整Keras模型的输出尺寸?

时间:2019-10-03 22:02:18

标签: python python-3.x tensorflow keras keras-layer

我正在尝试使用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 Sequential
from keras.layers import Dense, Flatten
import matplotlib.pyplot as plt
from keras.callbacks import EarlyStopping, ModelCheckpoint

model = 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,001
Trainable params: 1,005,001
Non-trainable params: 0
_________________________________________________________________

模型应基于(5,5,2)数组输出(5,5)数组,但在最低隐藏层失败。我该如何解决?

2 个答案:

答案 0 :(得分:0)

使用以下代码作为参考,根据您的输入值更改值:

train_data = train_data.reshape(train_data.shape [0],10、30、30、1)

同时输入输入的火车数据,

答案 1 :(得分:-1)

您的网络将输出形状为|product_code|product_name|product_id| | code 1 | P1 | SP1 | | code 2 | P2 | SP2 | 的张量。您的输出是4维张量吗? 如果它是|campaign_id |campaign_code|product_code|product_id| | 1 | C1 | code 1 | SP1 | | 2 | C2 | code 1 | SP1 | | 3 | C3 | code 2 | SP2 | 的单个值,则需要将其重塑为SELECT dmspro_mys_product_master.*, dmspro_mys_campaign_product.campaign_code FROM dmspro_mys_product_master INNER JOIN (SELECT DISTINCT dmspro_mys_campaign_product.product_id FROM dmspro_mys_campaign_product) AS cp ON cp.product_id = dmspro_mys_product_master.product_id 我认为