How to generate output_names in customerized loss function?


#1

Hi,
I am trying to set up weighted multi-loss functions in mxnet. The multi-loss alone works, the weighted loss for one task also works. But when I combine these two, I got some problems.
The output of the weighted loss seems didn’t pass the output_names to the multi_loss evalulation metrics, so I got this error:
pred = [pred[name] for name in self.output_names]
KeyError: ‘softmax_animal_print_output’

‘softmax_animal_print_output’ is one of the loss_name I gave through:
softmax_list = []

for i in range((len(classifier_def))):
    fc = mx.symbol.FullyConnected(data=flat,
                                  num_hidden=classifier_def[i][1],
                                  name=classifier_def[i][0])
    softmax_list.append(mx.sym.MakeLoss(mx.symbol.Custom(data=fc,
                                                name='softmax_' + classifier_def[i][0],
                                                positive_cls_weight=classifier_def[i][2],
                                                op_type='weighted_softmax_ce_loss')))

softmax = mx.symbol.Group(softmax_list)
return softmax

I also tried to add name to MakeLoss like this:
for i in range((len(classifier_def))):
fc = mx.symbol.FullyConnected(data=flat,
num_hidden=classifier_def[i][1],
name=classifier_def[i][0])
softmax_list.append(mx.sym.MakeLoss(mx.symbol.Custom(data=fc,
name=‘softmax_’ + classifier_def[i][0],
positive_cls_weight=classifier_def[i][2],
op_type=‘weighted_softmax_ce_loss’),name=‘softmax_’ + classifier_def[i][0]))

But neither works.
The weighted loss function I used is:

Thanks!


#2

Add one observation, the symbol.output_list() is:
[‘makeloss0_output’, ‘makeloss1_output’]
it didn’t get named as what I passed to name in MakeLoss
Any suggestions?