AFAIK there are 2 ways to express ResNet Block in pytorch:
- Copy the input in the beginning, modify the input in the process, add the copy in the end.
- Preserve the input in the beginning, create new variable in the process, add the input in the end.
Which leads to 2 kinds of code:
def forward(self, x):
y = x
x = self.conv1(x)
x = self.norm1(x)
x = self.act1(x)
x = self.conv2(x)
x = self.norm2(x)
x += y
x = self.act2(x)
return x
def forward(self, x):
y = self.conv1(x)
y = self.norm1(y)
y = self.act1(y)
y = self.conv2(y)
y = self.norm2(y)
y += x
y = self.act2(y)
return y
Are they identical? Which one is preferred? Why?
New contributor
amp-likes-linux is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.