Pytorch tutorial (3)
I stacked the neural network layers using nn.Sequential() to predict the California Housing data
I will use the California Housing dataset from sklearn to predict house prices based on the data.
Data
First, load the data and take a look at it.
1
2
3
4
from sklearn.datasets import fetch_california_housing
dataset = fetch_california_housing()
print(dataset.keys())
1
2
# output
dict_keys(['data', 'target', 'frame', 'target_names', 'feature_names', 'DESCR'])
1
2
3
4
5
6
7
import pandas as pd
dataFrame = pd.DataFrame(dataset['data'])
dataFrame.columns = dataset['feature_names']
dataFrame['target'] = dataset['target']
display(dataFrame)
There are eight features and one target value for California houses.
Model
Let’s define a model to train on the data and implement the code to train it.
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
32
33
34
35
36
37
38
39
40
41
import torch
import torch.nn as nn
from torch.optim.adam import Adam
# 모델 정의
model = nn.Sequential(
nn.Linear(8, 100), # 입력 8차원, 출력 100차원
nn.ReLU(),
nn.Linear(100, 1)
)
X = dataFrame.iloc[:,:8].values # target을 제외한 특징 8개
Y = dataFrame["target"].values # target 값 Y에 입력
batch_size = 100
learning_rate = 0.001
# 가중치를 수정하는 최적화 함수 정의
optim = Adam(model.parameters(), lr=learning_rate)
# epoch 반복
for epoch in range(200):
# 배치 반복
for i in range(len(X) // batch_size):
start = i * batch_size
end = start + batch_size
# 파이토치 실수형 텐서로 변환
x = torch.FloatTensor(X[start:end])
y = torch.FloatTensor(Y[start:end])
optim.zero_grad() # 가중치의 기울기를 0으로 초기화
preds = model(x) # 모델의 예측값 계산
loss = nn.MSELoss()(preds, y) # MSE 손실 계산
loss.backward() # 오차 역전파
optim.step() # 최적화 진행
if epoch % 20 ==0:
print(f"epoch : {epoch} , loss : {loss.item()}")
1
2
3
4
5
6
7
8
9
10
11
# output
epoch : 0 , loss : 1.8836865425109863
epoch : 20 , loss : 0.5972123146057129
epoch : 40 , loss : 0.7149457335472107
epoch : 60 , loss : 0.5533088445663452
epoch : 80 , loss : 0.39492934942245483
epoch : 100 , loss : 0.44756537675857544
epoch : 120 , loss : 0.7748529314994812
epoch : 140 , loss : 2.0179498195648193
epoch : 160 , loss : 2.9701578617095947
epoch : 180 , loss : 6.726071834564209
Model evaluation
Let’s input a single data point into the trained model to compare the predicted value with the actual value.
1
2
3
4
prediction = model(torch.FloatTensor(X[0, :8]))
answer = Y[0]
print(f"prediction : {prediction.item()}, answer : {answer}")
1
2
# output
prediction : 3.940722703933716, answer : 4.526
reference - 텐초의 파이토치 딥러닝 특강
This post is licensed under CC BY 4.0 by the author.
