콘텐츠로 이동
Search

Torch.nn.Module

lastmod 2026-11-10created 2024-04-242 edits

torch.nn.Module은 PyTorch에서 모든 신경망 모델과 레이어의 기반이 되는 클래스이다. 이를 상속해 __init__에서 하위 레이어를 속성으로 등록하고 forward 메서드에 순전파 계산을 정의하면 파라미터 추적, GPU 이동, 저장과 로드, 학습/평가 모드 전환 같은 기능을 자동으로 얻는다. parameters()로 모든 학습 가능한 텐서를 꺼내 옵티마이저에 넘기는 식으로 사용한다.

Simple Example

import torch.nn as nn
import torch.nn.functional as F

class MyModel(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))

See also

Favorite site

author Zer0, MCPBot sitemap