Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- NLU
- 임베딩
- SLASH22
- 유데미부트캠프
- sql정리
- 사이드프로젝트
- 그로스해킹
- 서비스기획
- 취업부트캠프 5기
- pytorch
- 특성중요도
- 유데미큐레이션
- 데이터도서
- 유데미코리아
- 서비스기획부트캠프
- 추천시스템
- 딥러닝
- 취업부트캠프
- NLP
- 토스
- 스타터스부트캠프
- SQL
- 스타터스
- 알고리즘
- 그래프
- BERT
- MatchSum
- AARRR
- AWS builders
- 부트캠프후기
Archives
- Today
- Total
다시 이음
Pytorch 모델 생성 및 layer 설정 본문
안녕하세요.
오늘은 Pytorch에서 모델을 생성하고 배정하는 방법에 대해서 알아보겠습니다.
모델을 정의하는 방법에서 tensorflow와 다른 점이 보여서 그 점을 비교하면서 하면 좋을 것 같습니다.
Pytorch
- Module 클래스
Model 을 정의할 때에는 Module 클래스를 무조건 상속 받아야합니다.
forward() 메소드는 모든 자식클래스에서 반드시 오버라이딩 해야합니다.
공식문서 // https://pytorch.org/docs/stable/generated/torch.nn.Module.html#torch.nn.Module
공식 문서를 통해 메소드를 보면 좋습니다.(eval(), train(), parameters(), state_dict(), to() 등등)
class Model(nn.Module):
def __init__(self, input_shape):
super(Model, self).__init__()
self.layer1 = nn.Linear(input_shape, 32)
self.layer2 = nn.Linear(32, 64)
self.layer_out = nn.Linear(64,1)
self.relu = nn.ReLU()
def forward(self, x):
x = self.relu(self.layer1(x))
x = self.relu(self.layer2(x))
x = self.layer_out(x)
return x
- Sequential 클래스
model = nn.Sequential(
nn.Linear(30, 32),
nn.ReLU(),
nn.Linear(32,64),
nn.ReLU(),
nn.Linear(64,1)
)
TensorFlow
- Sequential 클래스
from keras import models
from keras import layers
model = models.Sequential()
model.add(layers.Dense(32, activation='relu', input_shape=(784,)))
model.add(layers.Dense(10, activation='softmax'))
- 함수형 API
input_tensor = layers.Input(shape=(784,))
x = layers.Dense(32, activation='relu')(input_tensor)
output_tensor = layers.Dense(10, activation='softmax')(x)
model = models.Model(inputs=input_tensor, outputs=output_tensor)
이제 Layer을 어떻게 사용하면 될까요?
Pytorch layer 링크를 들어가서 공식문서를 통해 레이어가 어떻게 정의되는지 확인하세요.
'Pre_Onboarding by Wanted(자연어 처리)' 카테고리의 다른 글
Pytorch - Autograd 이해하기 (0) | 2022.03.05 |
---|---|
Pytorch Dataset / Dataloader (0) | 2022.03.02 |
BERT 모델 임베딩 이해하기 (0) | 2022.03.01 |
Pytorch - 기초 구조 (0) | 2022.03.01 |
Week1-4. 리뷰 긍정부정 판별 모델 설정 프로세스 (0) | 2022.02.24 |