• Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

Keras supports model multiple input and multiple output. In this paper, the Settings of Loss, Loss weight and Metrics in multiple output are recorded.

The model output

Assume that the model has multiple outputs

  • Classify: two-dimensional array, classify softmax output, need to configure cross entropy loss
  • Segmentation: map with the same size as input and sigmoID as output
  • Others: You need to customize losses for other outputs

Specific configuration

model

  • All variables are network layer in the model
 inputs = [input_1 , input_2]
 outputs = [classify, segmentation, others]
 model = keras.models.Model(inputs, outputs)
Copy the code

loss

 my_loss = {
     'classify': 'categorical_crossentropy',\
     'segmentation':'binary_crossentropy',\
     'others':my_loss_fun}
Copy the code

loss weight

 my_loss_weights = {
     'classify':1,\
     'segmentation':1,\
     'others':10}
Copy the code

metrics

 my_metrics ={
     'classify':'acc',\
     'segmentation':[mean_iou,'acc'],\
     'others':['mse','acc']
     }
Copy the code

compile

 model.compile(optimizer=Adam(lr=config.LEARNING_RATE), loss=my_loss, loss_weights= my_loss_weights, metrics= my_metrics)
Copy the code