My_loss() got an unexpected keyword argument 'reduction'

@witenberg

loss=F.cross_entropy
def my_loss(preds,target,weight=None, size_average=None, ignore_index=-100,
reduce=None, reduction= ‘mean’):
if isinstance(preds,tuple):
if size_average is not None or reduce is not None:
reduction = _Reduction.legacy_get_string(size_average, reduce)
loss1 = sum((loss(o, target, weight, None, ignore_index, None, reduction) for o in preds))
else:
if size_average is not None or reduce is not None:
reduction = _Reduction.legacy_get_string(size_average, reduce)
loss1 = loss(preds, target, weight, None, ignore_index, None, reduction)

return(loss1);

ClassificationInterpretation.from_learner(learn)

Error :
my_loss() got an unexpected keyword argument ‘reduction’

TypeError Traceback (most recent call last)
in ()
----> 1 ClassificationInterpretation.from_learner(learn)

2 frames
/usr/local/lib/python3.6/dist-packages/fastai/basic_train.py in get_preds(model, dl, pbar, cb_handler, activ, loss_func, n_batch)
44 zip(*validate(model, dl, cb_handler=cb_handler, pbar=pbar, average=False, n_batch=n_batch))]
45 if loss_func is not None:
—> 46 with NoneReduceOnCPU(loss_func) as lf: res.append(lf(res[0], res[1]))
47 if activ is not None: res[0] = activ(res[0])
48 return res

TypeError: my_loss() got an unexpected keyword argument ‘reduction’

1 Like

Can you try a partial function with all the arguments specified except the preds and target.

my_loss = partial(my_loss, weight=...)
2 Likes