Using one loss function in two loss evaluation circumstances

I want to build a model that evaluates two loss functions with the same l1 metric.

which way is a more pythonic method(or Pytorch method?)

# status 1
criterion1 = nn.L1Loss()
criterion2 = nn.L1Loss()

criterion1(input_1,output_1)
criterion2(input_2,output_2)
loss.backward()
# status 2
criterion1 = nn.L1Loss()

criterion1(input_1,output_1)
criterion1(input_2,output_2)
loss.backward()

Both solutions are correct and wrong at the same time, because you are not defining the variable loss. If the two criteria are exactly the same one, you can create just one object and do:

criterion = nn.L1Loss()

loss = criterion(input_1,output_1)
loss += criterion(input_2,output_2)

loss.backward()
1 Like