카테고리 없음
[PyTorch] nn.Module
SweetDev
2022. 1. 24. 16:16
import torch.nn as nn
import torch.nn.functional as F
class Model(nn.Module):
def __init__(self):
super(Model, self).__init__()
self.conv1 = nn.Conv2d(1, 20, 5)
self.conv2 = nn.Conv2d(20, 20, 5)
def forward(self, x):
x = F.relu(self.conv1(x))
return F.relu(self.conv2(x))
왜 super(Model, self).__init__()을 써줄까?
you need the super() call so that the mn.Module class itself is initialised. IN Python superclass constructors/initialisers aren't called automatically - they have to be called explicitly, and that is what super() does - it works out what superclass to call.
I assume you are using Python 3 - in which case you don't need the arguments in the super() call - this is sufficient :
인자는 꼭 안써줘도 된다. (super().__init__()도 괜찮다)