Pytorch > Pytorch 입문 > (3) 코드 리뷰 - (1) 선형회기분석

Pytorch > Pytorch 입문 > 선형회기분석


PyTorch 의 모든것은 PyTorch Tutorials 에서 확인 할 수 있지만
다른 언어, 다른 환경에서 몰입 하다 보면, "파이 토치 딥러닝 수행 과정" 이 잘 기억 나지 않을 때가 종종 있습니다. 

이에 그 기본 적인 절차와 내용을 기록해 두고자 합니다.

[바로가기] 



[파이 토치 딥러닝 수행 과정]

    1. nn.Module 클래스 상속 받아 (forward 함수를 통해) 모델 아키텍처 클래스 선언
      1. 해당 클래스 객체 생성
    2. SGD 등 Optimizer 생성
      1. 생성 모델의 파라미터를 최적화 대상으로 등록
    3. 데이터로 미니배치 구성 (for 문 수행)
      1. feed forward 반복 수행 (연산 그래프 생성) 
      2. 손실 함수를 통해 최종 결과 값 (scalar) 와 손실값 (loss) 계산
      3. 손실에 대하여 backward 호출
        1. 연산 그래프 상의 텐서들의 기울기가 채워짐
      4. 옵테마이저에서 step 을 호출 하여 경사 하상 수행
      5. 수렴 조건이 만족할 때까지 반복 수행 

[선형 회귀 분석 코드 리뷰]

###########################################################
# 선형 회기 분석
import random
import torch
import torch.nn as nn

# 학습 하고자 하는 모델 생성 (상속)
class MyModel(nn.Module):

    # 네트워크 구성
    def __init__(selfinput_sizeoutput_size):
        super(MyModelself).__init__()
        
        # 네트워크 구성
        # 추후 forward 로 입력 받을 Layer 계층 이외 의 모든 구성  
        # - nn.Linear 이내 "nn.Parameter" 는 수행 됨
        self.linear = nn.Linear(input_sizeoutput_size)

    # x 를 입력 받아 w 곱하기하고 b 편차를 더함 
    def forward(selfx):    
        y = self.linear(x)  #y = torch.mm(x, W) + b

        return y

###########################################################
# 임의의 함수 정의
## y = f(x_1,x_2,x_3) = 3x_1+x_2 - 2x_3
def ground_truth(x): 

    ground_truth_return = 3 * x[:, 0] + x[:, 1] - 2 * x[:, 2
    
    print("ground_truth_return {0}".formatground_truth_return ) )

    return ground_truth_return


###########################################################
# 모델과 텐서를 입력 받아 feedforward 수행, 
# 역전파 알고지름 수행하여 경사 하강법의 "한 스텝" 수행
def train(modelxyoptim):
    # initialize gradients in all parameters in module.
    optim.zero_grad()
    
    y_hat = model(x# feed-forward 수행, y 을 모델에 넣어 y 예측인 y_hat 확보 

    # answer and inferenced 간 에러 값 
    loss = ((y - y_hat)**2).sum() / x.size(0)
    
    loss.backward() # back-propagation
    
    optim.step() # one-step of gradient descent

    return loss.data

###########################################################
# Main 수행
###########################################################
batch_size = 1
n_epochs = 10   # 집단 무리,
n_iter = 5

model = MyModel(31)  # 네트워크 구성 (input 사이즈, output size)

#optimizer 생성 주로 SGD(Stochastic Gradient Descent)나 Adam 생성함
optim = torch.optim.SGD(model.parameters(), lr=0.0001momentum=0.1)  
print(" 1. torch.optim.SGD ---------  : ["  , model , "]\n")

##########################################################
# 모든 epoch 
# - 각 epoch
for epoch in range(n_epochs):
    avg_loss = 0

    #####
    # 각 epoch 마다 n_iter 만큼 반복 수행
    for i in range(n_iter):

        x = torch.rand(batch_size3#임의로 텐서를 생성 --> 
        y = ground_truth(x.data)      #정답 함수에 넣어 정답을 확보
        
        #손실 값을 구해 모델 학습 하기
        # - 구현 모델과, x 와 y 의 차이
        # - optim , 한 step 수행  --> 학습
        loss = train(modelxyoptim)

        avg_loss += loss  #누적 손실 -> 평균 내기 위해

    # 한 epoch 내  n_iter 반복하여 얻으 평균 loss 값 구하기
    avg_loss = avg_loss / n_iter  
    ###################################################################
    # simple test sample to check the network.
    # epoch 단위 "y = f(x_1,x_2,x_3) = 3x_1+x_2 - 2x_3" 검증용 텐서 생성
    x_valid = torch.FloatTensor([[.3.2.1]]) 
    #  얻어진 x_valid 를 정답 함수에 넣어 "정답"을 확보
    y_valid = ground_truth(x_valid.data)        

    model.eval()           #평가 모드로 전환 (기본은 학습 모드)

    y_hat = model(x_valid# 검증 데이터 x_valid 입력으로 y 예측 값 y_hat 확보

    model.train()  #다시 학습 모드로 전환

    ##################################################################
    # "y_valid.data[0], y_hat.data[0, 0]" 의 차이로 근사 여부 판단
    # simple test sample 인 x_valid 의 y_hat 이 
    # 기존 학습된 y_valid (학습 검증이 필요)에 근 사 되어 지는 것을 확인 
    print(avg_lossy_valid.data[0], y_hat.data[00])

    # n_epochs 만큼 반복 학습을 수행하되, 그 과정에서 근사 하게 되면 학습 종료
    if avg_loss < .001
        break


[결과]

1. torch.optim.SGD --------- : [ MyModel( (linear): Linear(in_features=3, out_features=1, bias=True) ) ] ground_truth_return tensor([0.6122]) # n_iter 내 ground_truth 값, 이후 train,optim
ground_truth_return tensor([2.2992]) ground_truth_return tensor([1.9740]) ground_truth_return tensor([0.8218]) ground_truth_return tensor([-0.1526]) ground_truth_return tensor([0.9000]) tensor(3.9595) tensor(0.9000) tensor(-0.4674) # n_epochs ground_truth_return tensor([1.0779]) ground_truth_return tensor([1.9274]) ground_truth_return tensor([1.7066]) ground_truth_return tensor([0.1239]) ground_truth_return tensor([0.7213]) ground_truth_return tensor([0.9000]) tensor(3.4216) tensor(0.9000) tensor(-0.4649) ground_truth_return tensor([1.1843]) ground_truth_return tensor([1.5315]) ground_truth_return tensor([-0.8453]) ground_truth_return tensor([0.5411]) ground_truth_return tensor([2.9766]) ground_truth_return tensor([0.9000]) tensor(4.7910) tensor(0.9000) tensor(-0.4626) ground_truth_return tensor([3.3473]) ground_truth_return tensor([0.8126]) ground_truth_return tensor([2.0930]) ground_truth_return tensor([-0.2611]) ground_truth_return tensor([0.9762]) ground_truth_return tensor([0.9000]) tensor(6.1908) tensor(0.9000) tensor(-0.4596) ground_truth_return tensor([1.5612]) ground_truth_return tensor([1.7278]) ground_truth_return tensor([0.0567]) ground_truth_return tensor([1.5653]) ground_truth_return tensor([-0.6958]) ground_truth_return tensor([0.9000]) tensor(3.0491) tensor(0.9000) tensor(-0.4575) ground_truth_return tensor([-0.2793]) ground_truth_return tensor([0.1960]) ground_truth_return tensor([1.5891]) ground_truth_return tensor([1.8505]) ground_truth_return tensor([0.0595]) ground_truth_return tensor([0.9000]) tensor(2.5226) tensor(0.9000) tensor(-0.4557) ground_truth_return tensor([0.7098]) ground_truth_return tensor([1.3405]) ground_truth_return tensor([-0.6193]) ground_truth_return tensor([1.5655]) ground_truth_return tensor([1.3473]) ground_truth_return tensor([0.9000]) tensor(2.5948) tensor(0.9000) tensor(-0.4538) ground_truth_return tensor([3.0331]) ground_truth_return tensor([0.1155]) ground_truth_return tensor([1.0238]) ground_truth_return tensor([-0.4885]) ground_truth_return tensor([1.3030]) ground_truth_return tensor([0.9000]) tensor(4.3670) tensor(0.9000) tensor(-0.4514) ground_truth_return tensor([-1.5281]) ground_truth_return tensor([-1.0364]) ground_truth_return tensor([1.6226]) ground_truth_return tensor([3.0214]) ground_truth_return tensor([0.3844]) ground_truth_return tensor([0.9000]) tensor(4.7789) tensor(0.9000) tensor(-0.4499) ground_truth_return tensor([-0.2825]) ground_truth_return tensor([0.6866]) ground_truth_return tensor([2.6080]) ground_truth_return tensor([2.4613]) ground_truth_return tensor([0.4135]) ground_truth_return tensor([0.9000]) tensor(4.7468) tensor(0.9000) tensor(-0.4474)

ref

- 김기현의 자연어 처리 딥러닝 캠프(파이토치 편)

- 위키피디아 : https://ko.wikipedia.org/wiki/선형_회귀

- 최적화: https://blog.naver.com/slykid/222297529363 



- 용어 : https://losskatsu.github.io/machine-learning/epoch-batch/#3-epoch%EC%9D%98-%EC%9D%98%EB%AF%B8 

Comments

Popular posts from this blog

[MaritimeCyberTrend] Relationship and prospects between U.S. Chinese maritime operations and maritime cybersecurity

Examining the Reality of Cyber Incidents and the Shortfalls in Compliance Frameworks

Comprehensive List of Shipboard Systems in Commercial Vessels