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:
  
    
    
    How to create a Siamese network vision
  
  
    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…
  


  
    
    
    Setting up a Siamese Network model 
  
  
    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(…
  


it seems like the default way to do this in PyTorch is something like (for example):
def forward(self, input1, input2):
     out1 = self.conv(input1)
     out2 = self.conv(input2)

     return self.fc(torch.concat(out1,out2,dim=1)

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?
Clearly, this isn’t the case but I’d love to understand why.