How to modify the gradient manually?

username: Brasnold
cooked: <p>I need to have access to the gradient before the weights are updated.<br>
In particular I need to modify it by multiplying it for another function.</p>
<pre><code> loss.backward()
 # Here i need to access the gradients and modified it.
 optimizer.step()</code></pre>

username: jkarimi91
cooked: <aside class="quote quote-modified" data-post="3" data-topic="688">
  <div class="title">
    <div class="quote-controls"></div>
    <img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/tudor_berariu/48/419_2.png" class="avatar">
    <a href="https://discuss.pytorch.org/t/directly-getting-gradients/688/3">Directly getting gradients</a> 
  </div>
  <blockquote>
    Let’s say you compute variable y as a function of variable x. 
When you call y.backward(dL_dy) you’ll get the value of dL_dx in x.grad (which is dL_dy * dy_dx). If you just put a tensor full of ones instead of dL_dy you’ll get precisely the gradient you are looking for. 
import torch
from torch.autograd import Variable

x = Variable(torch.ones(10), requires_grad=True)
y = x * Variable(torch.linspace(1, 10, 10), requires_grad=False)
y.backward(torch.ones(10))
print(x.grad)

produces 
Variable con…
  </blockquote>
</aside>


username: Brasnold
cooked: <p>Thanks, but the response is not conclusive. I need to know how to implement somenthing like this in tensorflow.</p>
<pre><code>grads_and_vars = opt.compute_gradients(loss, parameter_list)
my_grads_and_vars = [(g*C, v) for g, v in grads_and_vars]
opt.apply_gradients(my_grads_and_vars)
</code></pre>
<p>If i’ve understand it well, torch.backward() store the grandient in parameter.grad, so i’ve to access every parameter iterating long the module parameters list.</p>

username: jkarimi91
cooked: <pre><code class="lang-auto">loss.backward()
for p in model.parameters():
    p.grad *= C  # or whatever other operation
optimizer.step()
</code></pre>

username: Brasnold
cooked: <p>Thanks.</p>
<p>And if i’ve two loss function and different operations to do?</p>
<p>For example, i’ve f_1 and f_2, and I want the learning signal (or the gradient to apply) to be D(f_1) + D(f_2) * C, where D(f_1) stand for derivative.</p>
<p>It should be:</p>
<blockquote>
<p>loss2.backward()<br>
for p in model.parameters():<br>
p.grad *= C<br>
loss1.backward()<br>
optimizer.step()</p>
</blockquote>
<p>This works because i compute the gradients in f_2 before than f_1, otherwise grad accumulate gradients and i can’t acces only f_2 part. There’s a way to do it regardless of order? So is there a way to access the gradient before it is accumulated in p.grad?</p>

username: 11193
cooked: <p>how about this:</p>
<pre><code class="lang-auto">loss2.backward(retain_graph=True)
for p in model.parameters():
    p.grad *= C
optimizer.step()
optimizer.zero_grad()
loss1.backward()
optimizer.step()
</code></pre>
<p>so you can avoid accumulate gradients on parameters.</p>

username: rohun
cooked: <p>I see that this allows us to update the weights twice.<br>
But what I want to weight the gradient from loss2 with C2 and from loss1 with C1 and make a single update?</p>

username: huahuanZ
cooked: <p>How about this</p>
<pre><code class="lang-auto">loss1.backward(torch.ones_like(loss1)*C1)
loss2.backward(torch.ones_like(loss2)*C2)
optimizer.step()
</code></pre>