7
$\begingroup$

I have a model like this with multiple outputs and i want to change it's output names

class MyModel(Model):
    def __init__(self):
        super(MyModel, self).__init__()
        self.layer_one = Dense(1, name='output_name_one')
        self.layer_two = Dense(1, name='output_name_two')
    def call(self, inputs):
        output_name_one = self.layer_one(inputs)  
        output_name_two = self.layer_two(inputs)  
        return output_name_one, output_name_two

keras automatically set output names to output_1, output_2, ... how can i change the output names to my desired names?

$\endgroup$
3
  • $\begingroup$ Are you using tensorflow.keras and the associated imports of Model and Dense, or a different source? For imports coming from tensorflow.keras with tensorflow 2 I am unable to reproduce your issue; for me, upon being built, the model in your example yields a model summary with the output names as desired. Similarly, using the functional and sequential apis in a similar way works fine for me. If you'd like, I'm happy to write an answer describing how to verify this. If you are using the same imports as me, in what verifiable sense is your code not changing the output names? $\endgroup$ Commented May 18, 2020 at 21:43
  • $\begingroup$ I am using tensorflow.keras with tensorflow 2.2 but my output names are output_1, output_2, ... i checked it in kaggle notebook and even in my local computer and i have this issue in both cases. @It'sRecreational $\endgroup$
    – amin msh
    Commented May 18, 2020 at 23:44
  • $\begingroup$ Huh, that is odd. In what sense are your output names output_1 and output_2? When you instantiate model = MyModel() and call model.summary()? Or in another context? If you can provide more details about your setup and what precise code is yielding output that is unintended, I can try to be of more help. $\endgroup$ Commented May 19, 2020 at 1:25

2 Answers 2

7
$\begingroup$

I also have this issue, but didn't have the answer with a customized Model. There is a workaround as follows:

 model = Model(inputs=inputs,
                  outputs={'ctr_output': ctr_pred, 'ctcvr_pred': ctcvr_pred, 'cvr_output': cvr_pred})
$\endgroup$
1
  • $\begingroup$ Where or how is 'inputs' defined? Could you show sufficient context for your code snippet? $\endgroup$
    – pygosceles
    Commented Mar 16, 2022 at 14:25
2
$\begingroup$

maybe this is better in your case:

class MyModel(Model):
    def __init__(self):
        super(MyModel, self).__init__()
        self.layer_one = Dense(1, name='output_name_one')
        self.layer_two = Dense(1, name='output_name_two')
    def call(self, inputs):
        output_name_one = self.layer_one(inputs)  
        output_name_two = self.layer_two(inputs)  
        return {'custom_name_one': output_name_one,'custom_name_two': output_name_two}
$\endgroup$
1
  • $\begingroup$ Thanks, this worked for me. After I did this and run model.fit() the losses appeared with the names of the keys. Can you point to a reference in tensorflow documentation that explains that is the way to be done? $\endgroup$ Commented Sep 24, 2021 at 6:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.