[Python] Multi-task in Tensorflow

Discussão em 'Python' iniciado por Stack, Fevereiro 14, 2025.

  1. Stack

    Stack Membro Participativo

    I am bulding a multi-task model with two outputs. I saw many coding examples in which every output layer should be given a name that is the same as the one passed for compile function:

    class Model_3(Model):
    def __init__(self, data_format='channels_first', **kwargs ):
    super(Model_3, self).__init__(**kwargs)
    # ...... Initializing the models block..........
    # showing only the two 1x1 conv responsible for generating the 2 outputs:
    # 1x1 conv. blocks
    self.conv_1 = Conv2D(filters=1, kernel_size=1, strides=1, padding='same',
    data_format=self.data_format, name = 'bone_mask')
    self.conv_2 = Conv2D(filters=1, kernel_size=1, strides=1, padding='same',
    data_format=self.data_format, name = 'lesion_mask')

    def build(self, input_shape):
    super(Model_3, self).build(input_shape)

    def call(self, inputs):


    bone_mask = self.conv_1(x_decoder_4)



    lesion_mask = self.conv_2(x_decoder_8)



    return{'bone_mask': bone_mask, 'lesion_mask': lesion_mask}
    model.compile(optimizer=adam,
    loss={'bone_mask': soft_dice_loss,

    'lesion_mask': soft_dice_loss} ,
    loss_weights={

    'bone_mask': task_1_weight,

    'lesion_mask': task_2_weight}


    )


    I noticed that if I omit the name argument in the two 1x1 conv layers but if still the model returns a dictionary, tensorflow can still train the model using model.fit :

    class Model_3(Model):
    def __init__(self, data_format='channels_first', **kwargs ):
    super(Model_3, self).__init__(**kwargs)
    # ...... Initializing the models block..........
    # showing only the two 1x1 conv responsible for generating the 2 outputs:
    # 1x1 conv. blocks
    self.conv_1 = Conv2D(filters=1, kernel_size=1, strides=1, padding='same',
    data_format=self.data_format)
    self.conv_2 = Conv2D(filters=1, kernel_size=1, strides=1, padding='same',
    data_format=self.data_format)

    def build(self, input_shape):
    super(Model_3, self).build(input_shape)

    def call(self, inputs):


    bone_mask = self.conv_1(x_decoder_4)



    lesion_mask = self.conv_2(x_decoder_8)

    return{'bone_mask': bone_mask, 'lesion_mask': lesion_mask}

    model.compile(optimizer=adam,
    loss={'bone_mask': soft_dice_loss,

    'lesion_mask': soft_dice_loss} ,
    loss_weights={

    'bone_mask': task_1_weight,

    'lesion_mask': task_2_weight}


    )


    So, my question is if I omit the names of the last layers but return a dictionary with keys that match the names specified in the model.compile function (e.g., {'bone_mask': output1, 'lesion_mask': output2}), will TensorFlow still be able to correctly map the loss functions and metrics to the corresponding outputs based solely on the dictionary keys? I'm asking because I've already done a significant amount of work without explicitly naming the last layers, and I want to ensure that this omission won't cause any issues.

    Continue reading...

Compartilhe esta Página