Backward Loss between outputs of different runs

username: jcarv
cooked: <p>I am trying to incorporate the difference between model outputs for different iterations, into the loss for training the model.<br>
Is it possible to have a computation graph that results from the application of the same model to two different inputs?</p>
<p>Something like:</p>
<pre><code class="lang-auto">model = nn.Sequential(nn.Linear(input_size, 100),
            nn.ReLU(),
            nn.Linear(100, out_size),
            nn.ReLU()
        )
outputs = []
for idx, (x,_) in enumerate(train_loader):
  output = model(x)
  outputs.append(outputs)
  if idx%2 == 0:
    loss = criterion(outputs[0], outputs[1])
    optimizer.zero_grad()
    loss.backward()
    optimizer.step()
    outputs = []
</code></pre>
<p>In this case, we would be saving the output of two sequential iterations, and minimize the loss between those two outputs. My main concern is that the same tensors are included twice in the computation graph. Would this be a problem?</p>
<p>Thanks for the help!</p>

username: Scott_Hoang
cooked: <p>I think this is okay. remember to zero_grad your optimizer.</p>

username: jcarv
cooked: <p>Thanks for the response! I will remember to add the zero_grad.<br>
Do you have any idea on how the computation graph will be? Or how to test if it’s backpropagating correctly?</p>

username: albanD
cooked: <blockquote>
<p>Is it possible to have a computation graph that results from the application of the same model to two different inputs?</p>
</blockquote>
<p>Yes this is not problem.</p>
<blockquote>
<p>Do you have any idea on how the computation graph will be?</p>
</blockquote>
<p>The Parameters from your net have been used twice and their gradient will correspond to these two use (the sum of the gradients).<br>
If you’re not afraid of very verbose representation, you can use <a href="https://github.com/szagoruyko/pytorchviz">torchviz</a> to see how the graph will look like.</p>

username: jcarv
cooked: <p><a class="mention" href="/u/alband">@albanD</a><br>
Thanks for the feedback.<br>
I just tried to test one iteration of the model with torchviz, and run into an issue.<br>
It seems that it just shows what the graph would be when we compute the model’s output with just one x, (ie. after 1 forward).</p>
<p>In my case I am actually doing 2 forwards of the model, and back-propagating the loss between the 2 outputs of the forward.</p>
<p>Testing it with MNIST I get:<img src="https://discuss.pytorch.org/uploads/default/original/3X/3/b/3b8f78d62b3f3f4a40808df460632382e885d512.png" alt="Screenshot 2020-06-23 at 11.17.29" data-base62-sha1="8uTyBtKv2gc1Wv8rOB9tugOFWKK" width="441" height="494"></p>
<p>Is there any workaround to test this?<br>
My only concern is that computation might get messed up when the loss is computed between the two forwards (and not against a target).</p>
<p>Thanks!</p>

username: albanD
cooked: <p>Do you give both outputs to torchviz to get this? Or just one? You will see the graph for a given output. So if you give the output of a single forward, you’ll see the graph for a single forward.<br>
Or you want to give your loss directly which should depend on both outputs.</p>