I’m just started with pytorch and trying to understand how to deal with custom loss functions, especially with some non trivial ones.

**Problem 1** . I’d like to stimulate my nn to maximize true positive rate and at the same time minimize false discovery rate. For example increase total score on +2 for true positive, and decrease on -5 for false positive.

```
def tp_fp_loss(yhat, y):
total_score = 0
for i in range(y.size()):
if is_tp(yhat[i],y[i]):
total_score += 2
if is_fp(yhat[i],y[i]):
total_score -= 5
return -total_score
```

**Problem 2** . In case when y is a list of positive and negative rewards (y = [10,-5, -40, 23, 11, -7]), stimulate nn to maximize sum of rewards.

```
def max_reward_loss(yhat,y):
r = torch.autograd.Variable(torch.Tensor(y[yhat >= .5]), requires_grad=True).sum()
return -r
```

Maybe I’m not completely understand some autograd mechanics, functions which I implemented correctly calculate loss but learning with them doesnt work What I’m doing wrong? Can anybody help me with some working solution of any of that problems?