Help on basic class automatic diff usage

username: tcapelle
cooked: <p>Hello everyone,<br>
I am starting to build a physical simulation framework, that will need optimisation in the future.<br>
It is porting a matlab set of routines that do mostly linear algebra. I have already ported one part in pytorch, directly replacing numpy functionality. Beeing able to send workflows to GPU is beyond great (100x speed up for our case). But now, I am refactoring to be able to do gradient descent on the objects and classes defined on the framework.<br>
I want some help on understanding the best practices on how to do so. I am using a lot of code from Kornia and Kaolin as my problem is descriptible in 3D meshes.<br>
I have some questions:</p>
<ul>
<li>How to store class states that are goind to be diferentiated afterwards. A minimal example would be something like this:</li>
</ul>
<pre><code class="lang-auto">class Foo:
    def __init__(self, x: Tensor):
        self.x = x
    def transform(self, p: Tensor):
        self.x = self.x + p
    def loss(self):
        return self.x.norm()**2
</code></pre>
<p>In this class, the sate is <code>x</code>, and I would transform this state using built-in methods as <code>transform</code> and would like to be able to compute a loss of his new transformed state. So I do this:</p>
<pre><code class="lang-auto">test = Foo(Tensor([1.,3.,5.]))
p = torch.tensor([-3., 1., 4. ], requires_grad=True)
</code></pre>
<p>And then a minimal training loop:</p>
<pre><code class="lang-auto">#train loop
learning_rate = 1e-5
for t in range(10):
    test.transform(p)
    loss = test.loss()
    loss.backward()
    print(f'loss: {loss}')
    with torch.no_grad():
        print(f' p:{p},\n grad {p.grad}')
        p -= learning_rate * p.grad      
        if p.grad.norm()&lt;1e-4: 
            break
        p.grad.zero_()
</code></pre>
<p>But this is no working as expected, probably because I am overwriting the state <code>x</code> with a modified <code>x+p</code> in each state. In my case transformations are a little more complicated (rotations, scaling and translates).</p>
<ul>
<li>How would you proceed? (nn.Module extension?)</li>
<li>Is there tutorials on this?<br>
Thanks you =)</li>
</ul>

username: albanD
cooked: <p>Hi, welcome !</p>
<p>In general, to avoid this issue, we make (most) <code>nn.Module</code> stateless.</p>
<p>I think the problem you’re seeing is that if you run this for loop for a while, it will eat all your memory.<br>
This is because, every new x you compute is given by <code>x = transform(x, p)</code>.<br>
At the very first iteration, x does not require gradient and p does.<br>
So the backward sets the gradients of p and you’re happy.<br>
But at the next iteration, the x that was the result of the op above does require gradients. And so at the second iteration, when you call backward, you will backward through both the second and the first iterations.</p>
<p>To prevent this, you want to break the link (in the autograd sense) between the x from one iteration to the other. You can do so with <code>self.x = self.x.detach()</code>.</p>
<p>If you want to make sure that the graph of what is considered in the backward is what you expect, you can use a tool like <a href="https://github.com/szagoruyko/pytorchviz/tree/master/torchviz">torchviz</a> to plot this graph.</p>
<p>Hope this helps !</p>

username: tcapelle
cooked: <p>It helps a lot!<br>
You are completely right, I will take a look.</p>

username: tcapelle
cooked: <p>My current solution is modifying the class <code>Foo</code> as follows:</p>
<pre><code class="lang-auto">class Foo:
    def __init__(self, x: Tensor):
        self.x = x
    def transform(self, p: Tensor):
        self.x = self.x.detach()
        self.x = self.x + p
    def loss(self):
        return self.x.norm()**2
</code></pre>