1

I have a model with multiple outputs. I want to assign different labels to each of the loss functions and metrics. The code is as given below:

   input_img = Input(shape=(n_states,n_features))

   x = Conv1D(32, kernel_size=5, activation='relu', padding='same')(input_img)
   x = Conv1D(32, kernel_size=5, activation='relu', padding='same')(x)
   x = Conv1D(32, kernel_size=5, activation='relu', padding='same')(x)
   decoded = Conv1D(n_outputs, kernel_size=3, activation='linear', padding='same')(x)

   model = Model(inputs=input_img, outputs=[decoded,decoded])

   model.compile(loss={'regression': 'mean_squared_error', 
                       'diffusion': 'mean_absolute_error'},
                 loss_weights={'regression': 1.0,
                           'diffusion': 0.5},
                 optimizer='adam',
                 metrics={'regression': coeff_determination, 
                           'diffusion': coeff_determination})

   model.summary()

   history_callback = model.fit(x_train, 
         {'regression': y_train, 'diffusion': y_train},
         batch_size=batch_size,
         epochs=epochs,
         validation_data= (x_valid, {'regression': y_valid, 'diffusion': y_valid}),
         verbose=1)

If I run the above model, I get an error of unknown entries in the loss dictionary. Specifically, the error isUnknown entries in loss dictionary: ['diffusion', 'regression']. Only expected following keys: ['conv1d_4', 'conv1d_4'].

How can I give different names to each of the loss function? Thank you.

Nicolas Gervais
  • 33,817
  • 13
  • 115
  • 143
user3443033
  • 737
  • 2
  • 6
  • 21

1 Answers1

0

You need to match the names of your outputs with the loss dictionary keys. Here, you didn't name your outputs, so they default to conv1d_4 in the name space. Try:

decoded1 = Conv1D(n_outputs, kernel_size=3, activation='linear', 
    padding='same', name='diffusion')(x)
decoded2 = Conv1D(n_outputs, kernel_size=3, activation='linear', 
    padding='same', name='regression')(x)

I doubled your output because I don't think you can apply two different losses to the same output.

Here's a minimal example of matching output/loss dictionary keys:

from tensorflow.keras import Input, Model
from tensorflow.keras.layers import Dense
import numpy as np

x_train = np.random.rand(1000, 10)
y_train = np.random.rand(1000)

inputs = Input(shape=(10,))

x = Dense(32, activation='relu')(inputs)
out1 = Dense(1, name='first_output')(x)
out2 = Dense(1, name='second_output')(x)

model = Model(inputs=inputs, outputs=[out1, out2])

model.compile(loss={'first_output': 'mean_squared_error',
                    'second_output': 'mean_absolute_error'},
              optimizer='adam')

history_callback = model.fit(x_train,
                             {'first_output': y_train, 'second_output': y_train},
                             batch_size=8, epochs=1)

Notice that loss dictionary keys match the output keys. The same should be done with metrics, loss weights, validation data, etc.

Nicolas Gervais
  • 33,817
  • 13
  • 115
  • 143
  • Thank you very much. I was able to resolve the loss dictionary error. However, I am getting an error related to the custom loss function. Here is the code for custom loss function – user3443033 May 14 '20 at 01:00
  • Let's keep it at one issue per thread. If you make another post with this question I'll give it a shot – Nicolas Gervais May 14 '20 at 01:02
  • Thank you Nicolas. I have posted a new question (https://stackoverflow.com/questions/61787528/multiple-loss-functions-none-for-gradient). Hopefully, you can help to resolve the issue. Thank you in advance. – user3443033 May 14 '20 at 01:10