Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rename Keras tensor name for loss function

Tags:

keras

Are there simple renaming for keras tensor? I needs it for giving tensor more appropriate name in certain context.

When i compile model that has multiple output or input, the model needs the name of the input or output tensor. For example, below model needs name of 'advc_out_tn' and 'atoz.output'.

advc_out_tn = self.advc_model(atoz.output)

self.advc_atoz_model = Model(input=atoz.input, output=[advc_out_tn, atoz.output])
self.advc_atoz_model.compile(optimizer='adam', loss="binary_crossentropy", loss_weights={"advc_out_tn":1.0, "atoz_out_tn":1.0})

But it gives me error, because They are not their real name.

ValueError: Unknown entry in loss_weights dictionary: "atoz_out_tn". Only expected the following keys: ['model_2', 'concat_decoded_img']

But the real name of the tensors are somewhat clumsy. Especially if i use given model(in this case, atoz) and access their output tensor by '.out', it gives last layer name('concat_decoded_img'), not the name of the model.

How can i solve this?

like image 706
jinhwan Avatar asked Feb 06 '23 10:02

jinhwan


1 Answers

Had a similar problem. For me the following worked to assign a name to an output of the tensor type. I used a dummy Lambda layer (identity function) and assign the new name to this lambda layer (the result is no longer of the tensor type though, but for my purpose this didn't matter).

from keras.layers import Lambda

naming_layer = Lambda(lambda x: x, name='your_name')
newly_named_output = naming_layer (tensor_output)
like image 80
tlqs Avatar answered Jun 11 '23 15:06

tlqs