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 | 31 |
Tags
- SOMs
- Generative model
- Attention
- textmining
- RNN
- tensorflow
- LSTM
- ResNet
- 자기조직화지도
- 군집화
- NMF
- MLOps
- Logistic Regression
- Support Vector Machine
- Binary classification
- stemming
- Ann
- Transfer Learning
- cross domain
- 경사하강법
- TFX
- gaze estimation
- NER
- Clustering
- VGGNet
- Python
- AI 윤리
- nlp
- Gradient Descent
- BERT
Archives
- Today
- Total
juooo1117
Modeling - Linear model define (pytorch) 본문
Modeling - pytorch를 이용한 Linear model define
import torch
import tqdm as notebook_tqdm
X_train = torch.tensor([[1],
[2],
[3]], dtype=torch.float32)
y_train = torch.tensor([[20],
[40],
[60]], dtype=torch.float32)
Tensor의 size 확인:
torch.Size([3, 1]) torch.Size([3, 1])
# [3, 1] : [데이터개수, 개별데이터의 shape] -> 3개의 데이터, 각 데이터는 1개의 값을 가진 1차원배열
# [5, 10, 20] -> 5개의 데이터, 각 데이터의 size는 (10x20)
print(X_train.shape, y_train.shape)
Linear Model을 정의
pred(output) = X(input) * weight + bias
- initial weight: tensor([[1.7810]], requires_grad=True)
- initial bias: tensor([-0.5186], requires_grad=True)
# 'random seed' define
torch.manual_seed(1117)
# weight와 bias를 정의! ( .randn -> 표준정규분포를 따름)
weight = torch.randn(1, 1, requires_grad=True) # (1:입력값의 개수, 1:출력값의 개수)
bias = torch.randn(1, requires_grad=True)
print('initial parameter')
print('weight: ', weight)
print('bias: ', bias)
def linear_model(X):
pred = X * weight + bias
return pred
Loss Function 정의
모델이 추론한 값과 정답 사이의 차이(error)를 계산하는 function (Mean Squared Error 로 계산)
# 오차함수 정의 -> 모델이 추론한 값과 정답 사이의 차이를 계산하는 함수 (Mean Squared Error)
def mse_loss_fn(pred:"예측값", y:"정답"):
return torch.mean((pred - y)**2)
Backpropagation을 통한 weight, bias의 Gradient 계산
- weight의 grad: tensor([[-172.1180]])
- bias의 grad: tensor([-73.9130])
# weight, bias 에 대한 gradient(미분값)를 계산
loss.backward()
print("weight의 grad: ", weight.grad)
print("bias의 grad: ", bias.grad)
Optimizer
model을 학습시키기 위해서 optimization 적용.
weight의 torch.backward()라는 함수로, gradient를 구해서 weight의 값을 변화시켜주는 역할
가장 기본적인 방법으로 SGD가 있는데, loss를 줄이기 위해 고안된 방법으로, loss의 미분을 이용하여 loss를 줄이는 것이 목표이다.
(gradient가 -가 되도록 값을 이동시키면 언젠가 최소값을 찾을 수 있다는 아이디어에서 출발한 것이 SGD)
# optimalize -> gradient descent (pytorch 사용)
optimizer = torch.optim.SGD([weight, bias], # optimalize할 대상 -> 'requires_grad=True' 이어야 함
lr=0.001, # 학습률
)
# gradient descent 연산
print('update 전 weight: ', weight)
optimizer.step()
print('update 후 weight: ', weight)
optimizer.zero_grad()
Model Learning 과정
inference → error 계산 → weight & bias 값 업데이트 → optimization(gradient descent optimizer) 을 steps 만큼 반복하여 미분값이 0이되도록 optimal value에 수렴해 나간다.
STEPS = 300 # parameter(weight, bias)를 update할 횟수, 많이 돌릴수록 'loss3(error)'는 줄어듦!
for step in range(STEPS):
# 1. inference(predict)
pred = linear_model(X_train)
# 2. error calculate
loss = mse_loss_fn(pred, y_train)
# 3. weight, bias의 gradient calculate
loss.backward()
# 4. Optimalize (gradient descent optimizer 이용)
optimizer.step()
# 5. gradient value를 initializing (weight, bias를 넣어둔 optimizer를 0으로 만드는 것)
optimizer.zero_grad()
# error calculate
pred3 = linear_model(X_train)
loss3 = mse_loss_fn(pred3, y_train)
'Artificial Intelligence' 카테고리의 다른 글
Clustering (k-means, silhouette coefficient) (0) | 2023.10.18 |
---|---|
SVM(Support Vector Machines) (1) | 2023.10.17 |
Backpropagation (1) | 2023.10.16 |
Optimizer - SGD, Momentum, AdaGrad, Adam (0) | 2023.10.15 |
Linear model (Regression) (1) | 2023.10.14 |