Understanding Siamese Network Implementation in PyTorch

username: Arham_Khan
cooked: <p>I am trying to understand the implementation of a Siamese Network in PyTorch. In particular, I am confused as to why we are able to run two images through the same model twice. In examples such as these:</p><aside class="quote quote-modified" data-post="1" data-topic="16735">
  <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/feynman27/48/1719_2.png" class="avatar">
    <a href="https://discuss.pytorch.org/t/how-to-create-a-siamese-network/16735">How to create a Siamese network</a> <a class="badge-category__wrapper " href="/c/vision/5"><span data-category-id="5" data-drop-close="true" class="badge-category " title="Topics related to either pytorch/vision or vision research related topics"><span class="badge-category__name">vision</span></span></a>
  </div>
  <blockquote>
    I’m trying to send 2 images through a siamese network. It looks like it’s as easy as writing a for-loop, calling forward for each leg of the siamese net. Is this correct? I’ve written a baby siamese net below: 
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.autograd import Variable
import pdb

class BabyNet(torch.nn.Module):
    def __init__(self):
        super(BabyNet, self).__init__()
        self.conv1 = nn.Conv2d(3, 64, kernel_size=(7, 7), stride=(2, 2), paddi…
  </blockquote>
</aside>
<aside class="quote quote-modified" data-post="1" data-topic="82912">
  <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/vdw/48/10074_2.png" class="avatar">
    <a href="https://discuss.pytorch.org/t/setting-up-a-siamese-network-model/82912">Setting up a Siamese Network model</a> 
  </div>
  <blockquote>
    I’m trying my hands on Siamese Network models. After searching for some examples, this seems to be the common way to set up the model 
class SiameseNetwork(nn.Module):

	def __init__(self, core_model):
    	super(SiameseNetwork, self).__init__()
    	# Define layers
    	self.layer1 = ...
    	self.layer2 = ...
    	...

    def forward_one(input):
    	X = self.layer1(input)
    	X = self.layer2(X)
    	...
    	return X
    
    def forward(self, input1, input2):
        output1 = self.layers(…
  </blockquote>
</aside>

<p>it seems like the default way to do this in PyTorch is something like (for example):</p>
<pre><code class="lang-auto">def forward(self, input1, input2):
     out1 = self.conv(input1)
     out2 = self.conv(input2)

     return self.fc(torch.concat(out1,out2,dim=1)
</code></pre>
<p>my question is, why can we run two input images through the same layers twice? I understand that in a Siamese network both legs should share the same parameters, but in torch aren’t the local gradients at each layer computed during the forward pass? Wouldn’t running the second image through the network remove the local gradients from the first image and ruin the backpropogation step?</p>
<p>Clearly, this isn’t the case but I’d love to understand why.</p>