您传递给模型的Numpy数组列表不是模型预期的大小

时间:2019-10-05 03:24:15

标签: numpy keras

我试图像这样传递它们,从而在Keras中获得三个不同的损失函数

input_img = Input(shape=(728,))
encoded = Dense(450, activation='relu')(input_img)
encoded = Dense(250, activation='relu')(encoded)
encoded= Dense(20, activation='relu')(encoded)

decoded = Dense(250, activation='relu')(encoded)
decoded = Dense(450, activation='relu')(decoded)
decoded = Dense(728, activation='sigmoid')(decoded)
loss1 = Dense(728, activation='sigmoid', name='p1')(decoded)
loss2 = Dense(728, activation='sigmoid', name='p2')(decoded)
loss3 = Dense(728, activation='sigmoid', name='p3')(decoded)

我定义了三个不同的损失函数并成功编译

autoencoder = Model(inputs = [input_img], outputs=[loss1,loss2,loss3])
autoencoder.compile(optimizer='Adam', loss = [w_loss,b_loss, loss], metrics = [w_loss,b_loss], loss_weights=[1., 1., 1.])

然后我拟合模型

history_modified = autoencoder.fit(X_train, X_train, epochs=200, batch_size= 100, shuffle=True, validation_data=(X_test, X_test))

其中X_train尺寸为(100000,728),X_test尺寸为(50000,728)

我得到的错误是

Error when checking model target: the list of Numpy arrays that you are passing to your model is not the size the model expected. Expected to see 3 array(s), but instead got the following list of 1 arrays: [array([[0., 0., 0., ..., 0., 0., 0.],

我并不是造成问题的确切原因,但我认为这可能与层以及如何具有多个损失函数有关。

1 个答案:

答案 0 :(得分:0)

正如错误所述,它具有目标形状的问题。理想情况下,由于该模型有三个输出(loss1,loss2,loss3),因此输出中应该有三个数组以比较这三个输出数组。您必须仅在目标中传递一个数组,因此会出现此错误。