0

I am trying to obtain three different loss functions in Keras by passing them like so

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)

I defined three different loss functions and compiled succesfully

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

I then fit the model

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

Where X_train dimensions are (100000, 728) and X_test dimensions are (50000,728)

The error I'm getting is

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.],

I don't what exactly is causing the problem, but I think it might have to do with the layers and how I have multiple loss functions.

kdf
  • 358
  • 4
  • 16

1 Answers1

0

As the error says, it has the problem with the shape of the target. Ideally, as there are three outputs (loss1,loss2,loss3) from the model, there should be three arrays in the output to compare the three output arrays. You must be passing only one array in the target and hence this error.

Sree
  • 973
  • 2
  • 14
  • 32
  • Is there a way where I can get three different loss functions given one input? – kdf Oct 05 '19 at 15:40
  • https://stackoverflow.com/questions/49404309/how-does-keras-handle-multiple-losses this might be useful for you. Otherwise you can just change the ground truth labels to have 3 arrays. – Sree Oct 06 '19 at 08:39