Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pytorch Custom Loss Function with If Statement

I am trying to create a custom loss function in Pytorch that evaluates each element of a tensor with an if statement and acts accordingly.

def my_loss(outputs,targets,fin_val):
    if (outputs.detach()-fin_val.detach())*(targets.detach()-fin_val.detach())<0:
        loss=3*(outputs-targets)**2
    else:
        loss=0.3*(outputs-targets)**2
    return loss

I have also tried:

def my_loss(outputs,targets,fin_val):
    if torch.gt((outputs.detach()-fin_val.detach())*(targets.detach()-fin_val.detach()),0):
        loss=0.3*(outputs-targets)**2
    else:
        loss=3*(outputs-targets)**2
    return loss

In both cases, I get the following error:

RuntimeError: Boolean value of Tensor with more than one value is ambiguous

TIA

like image 260
user2299067 Avatar asked Sep 27 '26 09:09

user2299067


1 Answers

You are getting this error because the condition you are passing to the if statement is not a boolean but a tensor of booleans. Just check what's the nature of (outputs.detach()-fin_val.detach())*(targets.detach()-fin_val.detach())<0, it is a tensor!

What you should be looking to do instead is handling this in vectorized form. You can use torch.where which is built for this use:

torch.where(condition=(outputs - fin_val)*(targets - fin_val) < 0,
            x=3*(outputs-targets)**2,
            y=0.3*(outputs-targets)**2)

This will return a tensor of "xs" and "ys" based on the point-wise condition tensor condition. Then, you could average it depending on your needs to get an actual loss value.

like image 130
Ivan Avatar answered Sep 28 '26 21:09

Ivan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!