Unresolved attribute reference 'backward' for class 'int' less

Hi, all

How to solve this warning?

loss = torch.mean(torch.sum(pred* torch.log(pred)),1))

Unresolved attribute reference 'backward' for class 'int' less


What is the full warning, it seems to be truncated? I have never seen it before. Do you have a small code sample that reproduces it?

loss = 0
if cond1:
    loss += torch.mean(torch.sum(pred1* torch.log(pred1)),1))
if cond2:
    loss += torch.mean(torch.sum(pred2* torch.log(pred2)),1))

As far as I know this is because the loss is initialized with int.

Unresolved attribute reference 'backward' for class 'int' less... (Ctrl+F1) 
Inspection info: This inspection detects names that should resolve but don't. Due to dynamic dispatch and duck typing, this is possible in a limited but useful number of cases. Top-level and class-level items are supported better than instance items.

Ho, it is a warning from the type checker? Because you define loss=0 and if both conditions are False, you would call .backward() on an int.
I guess you will need to either define loss as a Tensor to begin with, or only call backward if either cond1 or cond2 is met.