Tensorflow 2.0 Custom loss function with multiple inputs

后端 未结 3 1549
谎友^
谎友^ 2021-02-15 18:11

I am trying to optimize a model with the following two loss functions

def loss_1(pred, weights, logits):
    weighted_sparse_ce = kls.SparseCategoricalCrossentro         


        
3条回答
  •  不知归路
    2021-02-15 18:40

    To expand on Jon's answer. In case you want to still have the benefits of a Keras Model you can expand the model class and write your own custom train_step:

    from tensorflow.python.keras.engine import data_adapter
    
    # custom loss function that takes two outputs of the model
    # as input parameters which would otherwise not be possible
    def custom_loss(gt, x, y):
        return tf.reduce_mean(x) + tf.reduce_mean(y)
    
    class CustomModel(keras.Model):
        def compile(self, optimizer, my_loss):
            super().compile(optimizer)
            self.my_loss = my_loss
    
        def train_step(self, data):
            data = data_adapter.expand_1d(data)
            input_data, gt, sample_weight = data_adapter.unpack_x_y_sample_weight(data)
    
            with tf.GradientTape() as tape:
                y_pred = self(input_data, training=True)
                loss_value = self.my_loss(gt, y_pred[0], y_pred[1])
    
            grads = tape.gradient(loss_value, self.trainable_variables)
            self.optimizer.apply_gradients(zip(grads, self.trainable_variables))
    
            return {"loss_value": loss_value}
    
    ...
    
    model = CustomModel(inputs=input_tensor0, outputs=[x, y])
    model.compile(optimizer=tf.keras.optimizers.Adam(), my_loss=custom_loss)
    

提交回复
热议问题