How forward() method is used when it have more than one two input parameters in pytorch

Viewed 5447

Can someone tell me the concept behind the multiple parameters in forward() method? Generally, the implementation of forward() method has two parameters

  1. self
  2. input

if a forward method has more than these parameters how PyTorch is using the forward method.

Let's consider this codebase: https://github.com/bamps53/kaggle-autonomous-driving2019/blob/master/models/centernet.py here online 236 authors have used forward method with two more parameters:

  1. centers
  2. return_embeddings

I am unable to find a single article that can answer my query on what condition Line 254(return_embeddings:) and Line 257(if centers is not None:) will execute. As per my knowledge forward, the method is internally called by nn module. Can someone please put some lights on this?

1 Answers

Forward function set by you. That means you can add more parameters as you want. For example, you could add inputs as shown below

def forward(self, input1, input2, input3):
    x = self.layer1(input1)
    y = self.layer2(input2)
    z = self.layer3(input3)

    net = torch.cat((x,y,z),1)
     
    return net

You have to control your parameters while feeding the network. Layers couldn't be feed with more than a parameter. Therefore, you need to extract features from your input one by one and concatenate with torch.cat((x,y),1)(1 for dimension) them.

Related