Full training code(Linear regression)

매일 공부(ML)·2022년 1월 27일
0

Pytorch

목록 보기
8/8
! pip install torchvision



import numpy as np
import torch

#데이터 정의

x_train = torch.FloatTensor([[1], [2], [3]])
y_train = torch.FloatTensor([[2], [4], [6]])

# Hypothesis초기화

W = torch.zeros(1, requires_grad = True)

b = torch.zeros(1, requires_grad = True)

#Optimizer정의

optimizer = torch.optim.SGD([W, b], lr = 0.01)

#Hypothesis 예측

nb_epochs = 1000
for epoch in range(1, nb_epochs + 1):
    hypothesis = x_train * W + b
    cost = torch.mean((hypothesis - y_train) ** 2)
    
    optimizer.zero_grad()
    cost.backward() #cost계산
    optimizer.step() #Optimizer로 학습
profile
성장을 도울 아카이빙 블로그

0개의 댓글