Anyone can help me?I have this problem! Thanks!

‘’‘Traceback (most recent call last):
File “train.py”, line 73, in
main()
File “train.py”, line 63, in main
test_loss+=loss_fn(output, label, reduction=‘sum’).item()
File “/home/a/anaconda3/envs/bcw_env/lib/python3.6/site-packages/torch/nn/modules/module.py”, line 489, in call
result = self.forward(*input, **kwargs)
TypeError: forward() got an unexpected keyword argument ‘reduction’
‘’’
when I run my code ,I have this error.Anyone can help me to solve it?Thank you very much!

1 Like

Well, a detailed loss_fn may help us to fix this problem.
But generally, you shold make an instance of the loss function, where the reduction can be set and then you can call this instance for your calculation of loss.
e.g. loss_fn = nn.MSELoss(reduction='sum') then test_loss += loss_fn(output, label).item()

I use the crossentropy loss function,the code is the offical code ‘minist.py’ in there ‘https://github.com/pytorch/examples/blob/master/mnist/main.py’.when I in model.train() I don’t use the ‘reduction’ parameters,when I in model.eval(),I use the ‘reduction’ parameters like this.

I use the crossentropy loss function,the code is the offical code ‘minist.py’ in there ‘https://github.com/pytorch/examples/blob/master/mnist/main.py’.when I in model.train() I don’t use the ‘reduction’ parameters,when I in model.eval(),I use the ‘reduction’ parameters like this.

Thank you very much! I think I have some error in the code. In the loss function object ‘loss_fn’ set the ‘reduction’ parameters is error.I shoule set it in the class ‘nn.Loss’! Thank you very much!

1 Like