# Custom loss function dependent on the samples

I am working with A2C and A3C systems. I have an environment where at each step, I can do several action, each action at his own advantage. I am trying to design a custom loss function where I can vary the advantage (delta) for each datapoint, but I stumble upon some issue when I tried to do that:

``````import numpy as np

import torch
import torch.nn as nn
import torch.optim as optim

class Net(nn.Module):
def __init__(self):
super(Net, self).__init__()
self.fc = nn.Linear(10, 4)

def forward(self, x):
x = self.fc(x)
x = nn.Softmax(dim=1)(x)
return x

def custom_loss(delta):
def loss(y_pred, y_true):
y_pred_clamped = torch.clamp(y_pred, 1e-8, 1 - 1e-8)
log_likelihood = y_true * torch.log(y_pred_clamped)
return loss

batch_size = 32
n_sample = 10000
network = Net()

x = torch.ones((1,10))
print(network(x), '\n')

target = [0.2, 0.4, 0.3, 0.1]

for i in range(int(n_sample/batch_size)):

delta = torch.ones((batch_size))  # Each sample of the dataset has its own delta, but by simplicity I set them all at 1
inputs = torch.ones((batch_size,10))
targets = torch.FloatTensor(batch_size*[target])
outputs = network(inputs)

loss = custom_loss(delta)(outputs, targets)
loss.backward()
optimizer.step()

x = torch.ones((1,10), dtype=torch.float)
print(network(x))
``````

This returns:

RuntimeError: The size of tensor a (4) must match the size of tensor b (32) at non-singleton dimension 1

I understand why it doesn’t work, and I understand that it is a weird question. But it would be very convenient for me if something like this could work. I am guessing that the loss function is computed like it was one line at the time (even if I assume it’s parallelized). Maybe it is possible to vectorise the loss part ?

Any help would be appreciated!
Thomas,

Hi,

When does the error appear? Is it when you do `-log_likelihood * delta` ?

1 Like

yes!

``````---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
<ipython-input-3-c142911ed308> in <module>
43     outputs = network(inputs)
44
---> 45     loss = custom_loss(delta)(outputs, targets)
46     loss.backward()
47     optimizer.step()

<ipython-input-3-c142911ed308> in loss(y_pred, y_true)
21         y_pred_clamped = torch.clamp(y_pred, 1e-8, 1 - 1e-8)
22         log_likelihood = y_true * torch.log(y_pred_clamped)
24     return loss
25

RuntimeError: The size of tensor a (4) must match the size of tensor b (32) at non-singleton dimension 1
``````

I found the solution here. I thought for some reason that the loss was computed differently because of the error message. Turns out it is pretty easy to do it by simply changing the loss function to :

``````def custom_loss(delta):
def loss(y_pred, y_true):
y_pred_clamped = torch.clamp(y_pred, 1e-8, 1 - 1e-8)
log_likelihood = y_true * torch.log(y_pred_clamped)
Thanks alban for your time and making me realize my mistake
{"ft_lang_label":"__label__en","ft_lang_prob":0.8077115,"math_prob":0.9642706,"size":1851,"snap":"2020-34-2020-40","text_gpt3_token_len":493,"char_repetition_ratio":0.10341094,"word_repetition_ratio":0.0,"special_character_ratio":0.28795245,"punctuation_ratio":0.16752577,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9970231,"pos_list":[0,1,2,3,4],"im_url_duplicate_count":[null,null,null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2020-09-23T16:36:40Z\",\"WARC-Record-ID\":\"<urn:uuid:5f04707a-b20f-4de3-863f-6ae1d9608de6>\",\"Content-Length\":\"22000\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:7b67b0c7-88d3-4d5d-a156-a271e052203c>\",\"WARC-Concurrent-To\":\"<urn:uuid:e7abf078-0c47-489e-b3c8-3cc631ab888d>\",\"WARC-IP-Address\":\"159.203.172.63\",\"WARC-Target-URI\":\"https://discuss.pytorch.org/t/custom-loss-function-dependent-on-the-samples/92690\",\"WARC-Payload-Digest\":\"sha1:XCMORKI7JFWYOVQQQJLHU3RBJS5FEBYQ\",\"WARC-Block-Digest\":\"sha1:XR6QUKGOQMYTAHHWTF6XHTZMCSNUKJAI\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2020/CC-MAIN-2020-40/CC-MAIN-2020-40_segments_1600400211096.40_warc_CC-MAIN-20200923144247-20200923174247-00570.warc.gz\"}"}