Tag: multivariate-linear-regression
All the articles with the tag "multivariate-linear-regression".
PYTORCH모두를 위한 딥러닝 2
class MultivariateLinearRegressionModel(nn.Module):
def __init__(self):
super().__init__()
self.model = nn.Linear(3, 1)
def forward(self, x):
return self.model(x)
# 모델 초기화
model = MultivariateLinearRegressionModel()
# optimizer 설정
optimizer = optim.SGD(model.parameters(), lr=1e-5)
모델, optimizer는 lab4_1과 동일하게 정의해주고
from torch.utils.data import Dataset
from torch.utils.data import DataLoader
class CustomDataset(Dataset):
def __init__(self):
self.x_data = [[73, 80, 75],
[93, 88, 93],
[89, 91, 90],
[96, 98, 100],
[73, 66, 70]]
self.y_data = [[152], [185], [180], [196], [142]]
def __len__(self):
return len(self.x_data)
def __getitem__(self, idx):
return torch.FloatTensor(self.x_data[idx]), torch.FloatTensor(self.y_data[idx])
dataset = CustomDataset()
dataloader = DataLoader(
dataset,
batch_size=2,
shuffle=True
)모두를 위한 딥러닝 2 - Lab4_2: Loading Data
lab41에서 다루었던 Multivariate Linear Regression에서는 학습 데이터로 3개의 차원을 가진 5개의 샘플을 사용했었다.
PYTORCH모두를 위한 딥러닝 2
x_train = torch.FloatTensor([[73, 80, 75],
[93, 88, 93],
[89, 91, 90],
[96, 98, 100],
[73, 66, 70]])
y_train = torch.FloatTensor([[152], [185], [180], [196], [142]])
hypothesis = x_train.matmul(W) # W의 차원은 [변수의 개수, 1]로 맞춰 줄 것
이때 주의할 것은 행렬곱 연산이 가능하도록 W의 차원을 변수에 개수에 따라 맞춰줘야 한다는 것이다.
Train
Multivariate Linear Regression의 hypothesis까지 모두 알아봤으니 학습하는 전체적인 코드를 작성해 보자.
모두를 위한 딥러닝 2 - Lab4_1: Multivariate Linear Regression
이전 포스팅까지의 regression은 하나의 변수를 통해 예측을 하는 것이었다. 하지만 직관적으로 생각해 보더라도 여러 변수를 가지고 더 많은 정보를 통해 예측하면 더 좋은 결과가 나올 것 같다.