Custom Loss in autoregressive neural network

Vaibhav_Chahar: <p>Hi, I am working with Auto regressive neural network, which is based on conditional probability. I have a 1d tensor which consist of [1,-1] and total 2500 elements in it like[1,-1,1,1,-1,…]. This is the input and output of my network is between [0,1] of same 2500 elements.<br>
Now I take the first element of my output and determine the first element of my input using random sampling (say my first element of output is 0.5365 then I create a random number between 0 and 1 if this random number is less then 0.5365 then first element of input is -1 and if it is greater, then it is 1).<br>
I do this 2500 time. In the last iteration, i got the final sets of input and output. I use this to determine the loss. My loss is:<br>
Loss=sum of final inputs - log(sum of final outputs). Now, what do next, like how to calculate the gradient, loss function, back propagation etc. and all the other operations.<br>
Please help me.</p>

ptrblck: <p>Could you post your current code (or a pseudo code) showing how exactly you are calculating the model output and loss?<br>
Generally, you should be able to directly call <code>backward()</code> on the output tensor assuming you’ve used differentiable PyTorch operations in the forward pass and didn’t detach the computation graph explicitly.</p>

Vaibhav_Chahar: <p>Hi, ptrblck<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/d/3/d3a964e9dd3e0f0a3f37ae65bce05470846a2d22.png" data-download-href="https://discuss.pytorch.org/uploads/default/d3a964e9dd3e0f0a3f37ae65bce05470846a2d22" title="first_net"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/d/3/d3a964e9dd3e0f0a3f37ae65bce05470846a2d22_2_525x500.png" alt="first_net" data-base62-sha1="ucrFDP7qz7kiBp9sjSK4k24zpjY" width="525" height="500" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/d/3/d3a964e9dd3e0f0a3f37ae65bce05470846a2d22_2_525x500.png, https://discuss.pytorch.org/uploads/default/optimized/3X/d/3/d3a964e9dd3e0f0a3f37ae65bce05470846a2d22_2_787x750.png 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/d/3/d3a964e9dd3e0f0a3f37ae65bce05470846a2d22.png 2x" data-dominant-color="0B2B55"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">first_net</span><span class="informations">853×811 115 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div><br>
this is my class of neural network,n and m controls the size of the input tensor n<em>m</em>m<em>m</em>m is 2500</p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/5/9/5920fb96ec56b57692b09d97e51ecd2cdba2d214.png" data-download-href="https://discuss.pytorch.org/uploads/default/5920fb96ec56b57692b09d97e51ecd2cdba2d214" title="2ses"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/5/9/5920fb96ec56b57692b09d97e51ecd2cdba2d214_2_658x500.png" alt="2ses" data-base62-sha1="cIt9Vy199mDnrS22bMcS0si1aXa" width="658" height="500" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/5/9/5920fb96ec56b57692b09d97e51ecd2cdba2d214_2_658x500.png, https://discuss.pytorch.org/uploads/default/original/3X/5/9/5920fb96ec56b57692b09d97e51ecd2cdba2d214.png 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/5/9/5920fb96ec56b57692b09d97e51ecd2cdba2d214.png 2x" data-dominant-color="072A55"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">2ses</span><span class="informations">888×674 70.9 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>
<p>in this I am doing my calculation for conditional probability. In the 2nd line I created instance for the Neural Network as Net. P_list is a tensor for all the probabilities stored that I will need in calculation for loss.</p>
<p><img src="https://discuss.pytorch.org/uploads/default/original/3X/9/1/91ae6643d91aa1a5a8b731d468efb99f7eb28400.png" alt="third_png" data-base62-sha1="kMKSHNDGoYiKCL7adzbKqgtMWWY" width="472" height="396"></p>
<p>In this, Q_list is a tensor which contains the final configuration [1,-1,1,1,1,-1,…].<br>
I am doing the calculations on GPU in batches that’s why I am taking mean.<br>
After applying loss.backword(). I am getting a runtime error:</p>
<p>UserWarning: Error detected in AddmmBackward0. Traceback of forward call that caused the error:<br>
File “/hac/home/vchahar/gauge_neural.py”, line 58, in <br>
Net_1=(Net(I_Par))<span class="hashtag-raw">#forward</span> pass of the neural network<br>
File “/hac/home/vchahar/.local/lib/python3.10/site-packages/torch/nn/modules/module.py”, line 1501, in _call_impl<br>
return forward_call(*args, **kwargs)<br>
File “/hac/home/vchahar/py_func.py”, line 37, in forward<br>
y=self.fc1(x)<br>
File “/hac/home/vchahar/.local/lib/python3.10/site-packages/torch/nn/modules/module.py”, line 1501, in _call_impl<br>
return forward_call(*args, **kwargs)<br>
File “/hac/home/vchahar/.local/lib/python3.10/site-packages/torch/nn/modules/linear.py”, line 114, in forward<br>
return F.linear(input, self.weight, self.bias)<br>
(Triggered internally at …/torch/csrc/autograd/python_anomaly_mode.cpp:114.)<br>
Variable._execution_engine.run_backward(  # Calls into the C++ engine to run the backward pass<br>
Traceback (most recent call last):<br>
File “/hac/home/vchahar/gauge_neural.py”, line 109, in <br>
loss.backward(retain_graph=True)<br>
File “/hac/home/vchahar/.local/lib/python3.10/site-packages/torch/_tensor.py”, line 487, in backward<br>
torch.autograd.backward(<br>
File “/hac/home/vchahar/.local/lib/python3.10/site-packages/torch/autograd/<strong>init</strong>.py”, line 200, in backward<br>
Variable._execution_engine.run_backward(  # Calls into the C++ engine to run the backward pass<br>
RuntimeError: one of the variables needed for gradient computation has been modified by an inplace operation: [torch.cuda.FloatTensor [4, 2500]] is at version 5000; expected version 4998 instead. Hint: the backtrace further above shows the operation that failed to compute its gradient. The variable in question was changed in there or anywhere later. Good luck!</p>
<p>Thank you for your help.</p>

ptrblck: <p>Could you post the code by wrapping it into three backticks ``` instead of screenshots as it would be easier to debug it?</p>

Vaibhav_Chahar: <p>Sure,</p>
<p>"class Neural_Made(nn.Module):<br>
def <strong>init</strong>(self,n,m):<br>
super(Neural_Made,self).<strong>init</strong>()<br>
self.n=n<br>
self.m=m<br>
self.in_size=n<em>m</em>m<em>m</em>m<br>
self.register_buffer(‘Mask1’,torch.ones([self.in_size]*2))<br>
self.register_buffer(‘Mask2’,torch.eye(self.in_size))<br>
self.Mask1=torch.tril(self.Mask1)<br>
self.Mask2=torch.sub(self.Mask1,self.Mask2)</p>
<pre><code>    self.fc1=nn.Linear(self.in_size,self.in_size,bias=False)
    nn.init.xavier_uniform_(self.fc1.weight)
    self.fc1.weight.data=torch.mul(self.fc1.weight.data,self.Mask2)


    self.fc2=nn.Linear(self.in_size,self.in_size,bias=False)
    nn.init.xavier_uniform_(self.fc2.weight)
    self.fc2.weight.data=torch.mul(self.fc2.weight.data,self.Mask1)

    self.out=nn.Linear(self.in_size,self.in_size,bias=False)
    nn.init.xavier_uniform_(self.out.weight)
    self.out.weight.data=torch.mul(self.out.weight.data,self.Mask1)

    self.prelu=nn.PReLU()
    self.sig=nn.Sigmoid()

def forward(self,x):
    y=self.fc1(x)
    z=self.prelu(y)
    y1=self.fc2(z)
    z1=self.prelu(y1)
    x1=self.out(z1)
    x2=self.sig(x1)
    return x2 ""
</code></pre>
<p><strong>This is Neural Network class</strong></p>
<p>"Start1=time.time()<br>
I_Par=torch.zeros((j,m<em>m</em>m<em>m</em>n)).to(dev)<span class="hashtag-raw">#rand_mat</span>(j,m,n,I_Val).to(dev)<br>
print(I_Par.size())<br>
P_list=torch.empty([len(I_Par[0]),j]).to(dev)<span class="hashtag-raw">#torch</span>.as_tensor([]).to(dev)<br>
print(P_list.size())<br>
Endi=time.time()</p>
<pre><code>print('Time in initialize a tensor and transferring to gpus ',Endi-Start1)
Net=pf.Neural_Made(n,m).to(dev)
optimizer = torch.optim.Adam(Net.parameters(), lr=1e-3) 

Start2=time.time()
for i in range(len(I_Par[0])):
    print(i)
    rand=torch.rand(j).to(dev)

        #create the instance of beural network class
    Net_1=(Net(I_Par))#forward pass of the neural network

    comparison=torch.gt(Net_1[:,i],rand)

    I_Par[:,i][comparison]=0
    I_Par[:,i][~comparison]=2

    P_list[i,:]=Net_1[:,i]
End2=time.time()
print('Time in network',End2-Start2)


print(I_Par)
P_list1=torch.log(P_list)
P_list2=torch.sum(P_list1,dim=0) 
Q_list=torch.as_tensor([]).to(dev) "
</code></pre>
<p>and the last part is</p>
<p>“Q_list=torch.cat((Q_list,act.reshape(1)),0)<br>
print(‘Q_list’,Q_list)<br>
print(‘P_list2’,P_list2)<br>
loss=P_list2-Q_list<br>
loss=torch.mean(loss)<br>
optimizer.zero_grad()<br>
loss.backward()<br>
optimizer.step()<br>
End11=time.time()<br>
print(‘Time in calculation’,End11-Start11)”</p>
<p>the error showing is</p>
<p>/hac/home/vchahar/.local/lib/python3.10/site-packages/torch/autograd/<strong>init</strong>.py:200: UserWarning: Error detected in MmBackward0. Traceback of forward call that caused the error:<br>
File “/hac/home/vchahar/gauge_neural.py”, line 59, in <br>
Net_1=(Net(I_Par))<span class="hashtag-raw">#forward</span> pass of the neural network<br>
File “/hac/home/vchahar/.local/lib/python3.10/site-packages/torch/nn/modules/module.py”, line 1501, in _call_impl<br>
return forward_call(*args, **kwargs)<br>
File “/hac/home/vchahar/py_func.py”, line 37, in forward<br>
y=self.fc1(x)<br>
File “/hac/home/vchahar/.local/lib/python3.10/site-packages/torch/nn/modules/module.py”, line 1501, in _call_impl<br>
return forward_call(*args, **kwargs)<br>
File “/hac/home/vchahar/.local/lib/python3.10/site-packages/torch/nn/modules/linear.py”, line 114, in forward<br>
return F.linear(input, self.weight, self.bias)<br>
(Triggered internally at …/torch/csrc/autograd/python_anomaly_mode.cpp:114.)<br>
Variable._execution_engine.run_backward(  # Calls into the C++ engine to run the backward pass<br>
Traceback (most recent call last):<br>
File “/hac/home/vchahar/gauge_neural.py”, line 110, in <br>
loss.backward()<br>
File “/hac/home/vchahar/.local/lib/python3.10/site-packages/torch/_tensor.py”, line 487, in backward<br>
torch.autograd.backward(<br>
File “/hac/home/vchahar/.local/lib/python3.10/site-packages/torch/autograd/<strong>init</strong>.py”, line 200, in backward<br>
Variable._execution_engine.run_backward(  # Calls into the C++ engine to run the backward pass<br>
RuntimeError: one of the variables needed for gradient computation has been modified by an inplace operation: [torch.cuda.FloatTensor [4, 2500]] is at version 5000; expected version 4998 instead. Hint: the backtrace further above shows the operation that failed to compute its gradient. The variable in question was changed in there or anywhere later. Good luck!</p>
<p>I found the inplace error(in 2 snippet):<br>
I_Par[:,i][comparison]=0<br>
I_Par[:,i][~comparison]=2<br>
but don,t know how to correct it.</p>
<p>Please help me with this. This is not full code but some part of code.</p>