티스토리 뷰

카테고리 없음

[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__()도 괜찮다)

 

공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/10   »
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31
글 보관함