Optimizing Part of the Input

yliats: <p>Hi all.<br>
I have a trained model, and I am trying to optimize the input (s.t. loss(output) will decrease). I saw some similar topics, but none of them solved my problem.</p>
<p>I have the following flow:<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/e/1/e1767c60913d3ca9385a0b047101b04af952385a.png" data-download-href="https://discuss.pytorch.org/uploads/default/e1767c60913d3ca9385a0b047101b04af952385a" title="image"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/e/1/e1767c60913d3ca9385a0b047101b04af952385a_2_690x198.png" alt="image" data-base62-sha1="waxhHT1qNqpazQDTc9xESn0yOJs" width="690" height="198" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/e/1/e1767c60913d3ca9385a0b047101b04af952385a_2_690x198.png, https://discuss.pytorch.org/uploads/default/optimized/3X/e/1/e1767c60913d3ca9385a0b047101b04af952385a_2_1035x297.png 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/e/1/e1767c60913d3ca9385a0b047101b04af952385a.png 2x" data-dominant-color="B5C7E7"><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">image</span><span class="informations">1052×302 17.2 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>
Where A is the only image I want to optimize.</p>
<p>My code:</p>
<pre><code class="lang-auto">        model.eval()
        Y = Y.cuda()

        A = torch.tensor(A, requires_grad=True)
        B = torch.tensor(B, requires_grad=False)

        data_optimizer = torch.optim.Adam([A], lr=1.0)

        C = torch.cat((A, B), 1)
        C = torch.tensor(C, requires_grad=True).cuda()

        for i in range(0, iter):

            Y_pred = model(C)

            loss = myloss(Y_pred, Y)

            print('loss: '+str(loss.item()))
            print('random_val: '+str(A[0, 0, 500, 500]))

            data_optimizer.zero_grad()
            loss.backward()
            data_optimizer.step()
</code></pre>
<p>I print the loss (which is just noisy) and a random pixel from image (which doesn’t change).<br>
My question is why A doesn’t change in this process?</p>
<p>When I pass C to the optimizer (which is cat of A and B), the prints show that A changes, but B changes as well (which is not acceptable).</p>
<p>Any advice?<br>
Thank you.</p>

Caruso: <p>Hi,</p>
<p>try this:</p>
<pre><code class="lang-auto">A = torch.tensor(A).cuda().requires_grad_(True)  # or torch.tensor(A, requires_grad_=True, device=device) with device="cuda:0" as example
B = torch.tensor(B).cuda()

data_optimizer = torch.optim.Adam([A], lr=1e-3)

C = torch.cat((A, B), 1)
</code></pre>
<p>This way we say that only for <code>A</code> the gradients should be calculated.<br>
If you do this <code>torch.tensor(C, requires_grad=True).cuda()</code>, the gradients will actually only flow back to the <code>C</code> tensor, because the graph for the creation of C gets lost (someone can probably explain it better).</p>

yliats: <p>Thank you for your reply. It worked nicely.<br>
Another problem was that <code>C = torch.cat((A, B), 1)</code> had to be inside the for loop (since I am iteratively adjusting one of its components).<br>
Thanks!</p>