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
- deeplearning
- 자연어분석
- fastapi #파이썬웹개발
- konlpy
- chatGPT
- Python
- 파이썬웹개발
- HTML
- pytorch
- fastapi
- langchain
- sklearn
- MachineLearning
- 랭체인
- 딥러닝
- NLP
- 파이썬
- 사이킷런
- 파이토치기본
- 판다스
- fastapi #python웹개발
- python 정렬
- 판다스 데이터정렬
- 파이토치
- pandas
- 챗gpt
- 비지도학습
- 머신러닝
- OpenAIAPI
- programmablesearchengine
Archives
- Today
- Total
Data Navigator
[PyTorch] Mini Batch and Data Load를 이용한 다중 회귀 분석 모델 본문
Machine Learning, Deep Learning
[PyTorch] Mini Batch and Data Load를 이용한 다중 회귀 분석 모델
코딩하고분석하는돌스 2021. 2. 14. 17:41In [62]:
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
In [63]:
from torch.utils.data import TensorDataset
from torch.utils.data import DataLoader
In [64]:
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]])
In [65]:
# TensorDataset은 기본적으로 텐서를 입력
dataset = TensorDataset(X_train, y_train)
In [66]:
# 데이터로더는 기본적으로 2개의 인자를 입력받는다.
# 하나는 데이터셋, 미니 배치의 크기. 미니 배치의 크기는 2의 배수 사용
# shuffle=True를 선택하면 Epoch마다 데이터셋을 섞어 학습 순서를 바꿈
dataloader = DataLoader(dataset, batch_size=2, shuffle=True)
In [67]:
# 모델과 옵티마이저 설계
model = nn.Linear(3,1)
optimizer = torch.optim.SGD(model.parameters(), lr=1e-5)
In [68]:
nb_epochs = 40
for epoch in range(nb_epochs + 1):
for batch_idx, samples in enumerate(dataloader):
# print(batch_idx)
# print(samples)
x_train, y_train = samples
# H(x) 계산
prediction = model(x_train)
# cost 계산
cost = F.mse_loss(prediction, y_train)
# cost로 H(x) 계산
optimizer.zero_grad()
cost.backward()
optimizer.step()
print('Epoch {:4d}/{} Batch {}/{} Cost: {:.6f}'.format(
epoch, nb_epochs, batch_idx+1, len(dataloader),
cost.item()
))
Epoch 0/40 Batch 1/3 Cost: 109422.382812 Epoch 0/40 Batch 2/3 Cost: 13926.935547 Epoch 0/40 Batch 3/3 Cost: 9553.985352 Epoch 1/40 Batch 1/3 Cost: 2407.589355 Epoch 1/40 Batch 2/3 Cost: 630.397583 Epoch 1/40 Batch 3/3 Cost: 332.481873 Epoch 2/40 Batch 1/3 Cost: 54.563324 Epoch 2/40 Batch 2/3 Cost: 15.682707 Epoch 2/40 Batch 3/3 Cost: 15.891177 Epoch 3/40 Batch 1/3 Cost: 2.497978 Epoch 3/40 Batch 2/3 Cost: 1.184473 Epoch 3/40 Batch 3/3 Cost: 0.711837 Epoch 4/40 Batch 1/3 Cost: 0.044865 Epoch 4/40 Batch 2/3 Cost: 1.406529 Epoch 4/40 Batch 3/3 Cost: 2.957722 Epoch 5/40 Batch 1/3 Cost: 1.657993 Epoch 5/40 Batch 2/3 Cost: 1.495775 Epoch 5/40 Batch 3/3 Cost: 1.046519 Epoch 6/40 Batch 1/3 Cost: 1.526840 Epoch 6/40 Batch 2/3 Cost: 1.215872 Epoch 6/40 Batch 3/3 Cost: 1.332472 Epoch 7/40 Batch 1/3 Cost: 1.218087 Epoch 7/40 Batch 2/3 Cost: 1.446331 Epoch 7/40 Batch 3/3 Cost: 0.300509 Epoch 8/40 Batch 1/3 Cost: 0.889232 Epoch 8/40 Batch 2/3 Cost: 1.583050 Epoch 8/40 Batch 3/3 Cost: 0.173692 Epoch 9/40 Batch 1/3 Cost: 0.295575 Epoch 9/40 Batch 2/3 Cost: 1.129346 Epoch 9/40 Batch 3/3 Cost: 2.637502 Epoch 10/40 Batch 1/3 Cost: 0.944092 Epoch 10/40 Batch 2/3 Cost: 1.281095 Epoch 10/40 Batch 3/3 Cost: 1.154995 Epoch 11/40 Batch 1/3 Cost: 0.160964 Epoch 11/40 Batch 2/3 Cost: 1.212293 Epoch 11/40 Batch 3/3 Cost: 2.744630 Epoch 12/40 Batch 1/3 Cost: 1.786660 Epoch 12/40 Batch 2/3 Cost: 1.454112 Epoch 12/40 Batch 3/3 Cost: 1.002473 Epoch 13/40 Batch 1/3 Cost: 1.289761 Epoch 13/40 Batch 2/3 Cost: 1.020319 Epoch 13/40 Batch 3/3 Cost: 1.456298 Epoch 14/40 Batch 1/3 Cost: 0.453115 Epoch 14/40 Batch 2/3 Cost: 3.129420 Epoch 14/40 Batch 3/3 Cost: 0.284713 Epoch 15/40 Batch 1/3 Cost: 0.882871 Epoch 15/40 Batch 2/3 Cost: 1.571089 Epoch 15/40 Batch 3/3 Cost: 0.166343 Epoch 16/40 Batch 1/3 Cost: 0.915380 Epoch 16/40 Batch 2/3 Cost: 0.512459 Epoch 16/40 Batch 3/3 Cost: 3.065117 Epoch 17/40 Batch 1/3 Cost: 1.628652 Epoch 17/40 Batch 2/3 Cost: 0.476308 Epoch 17/40 Batch 3/3 Cost: 1.290222 Epoch 18/40 Batch 1/3 Cost: 2.241758 Epoch 18/40 Batch 2/3 Cost: 1.175711 Epoch 18/40 Batch 3/3 Cost: 0.508409 Epoch 19/40 Batch 1/3 Cost: 1.225560 Epoch 19/40 Batch 2/3 Cost: 0.207209 Epoch 19/40 Batch 3/3 Cost: 3.247816 Epoch 20/40 Batch 1/3 Cost: 0.790679 Epoch 20/40 Batch 2/3 Cost: 1.899174 Epoch 20/40 Batch 3/3 Cost: 2.129081 Epoch 21/40 Batch 1/3 Cost: 1.683914 Epoch 21/40 Batch 2/3 Cost: 0.716574 Epoch 21/40 Batch 3/3 Cost: 0.859389 Epoch 22/40 Batch 1/3 Cost: 1.073815 Epoch 22/40 Batch 2/3 Cost: 1.591716 Epoch 22/40 Batch 3/3 Cost: 0.101120 Epoch 23/40 Batch 1/3 Cost: 0.504671 Epoch 23/40 Batch 2/3 Cost: 1.815940 Epoch 23/40 Batch 3/3 Cost: 1.404979 Epoch 24/40 Batch 1/3 Cost: 1.258580 Epoch 24/40 Batch 2/3 Cost: 1.488407 Epoch 24/40 Batch 3/3 Cost: 1.007675 Epoch 25/40 Batch 1/3 Cost: 1.269281 Epoch 25/40 Batch 2/3 Cost: 0.724787 Epoch 25/40 Batch 3/3 Cost: 2.530030 Epoch 26/40 Batch 1/3 Cost: 0.429210 Epoch 26/40 Batch 2/3 Cost: 1.682514 Epoch 26/40 Batch 3/3 Cost: 1.233186 Epoch 27/40 Batch 1/3 Cost: 0.386668 Epoch 27/40 Batch 2/3 Cost: 1.869890 Epoch 27/40 Batch 3/3 Cost: 1.461754 Epoch 28/40 Batch 1/3 Cost: 0.921992 Epoch 28/40 Batch 2/3 Cost: 1.965172 Epoch 28/40 Batch 3/3 Cost: 1.559793 Epoch 29/40 Batch 1/3 Cost: 0.889630 Epoch 29/40 Batch 2/3 Cost: 1.770577 Epoch 29/40 Batch 3/3 Cost: 1.301199 Epoch 30/40 Batch 1/3 Cost: 0.196662 Epoch 30/40 Batch 2/3 Cost: 3.392961 Epoch 30/40 Batch 3/3 Cost: 0.213224 Epoch 31/40 Batch 1/3 Cost: 0.876909 Epoch 31/40 Batch 2/3 Cost: 1.136801 Epoch 31/40 Batch 3/3 Cost: 1.421413 Epoch 32/40 Batch 1/3 Cost: 1.700405 Epoch 32/40 Batch 2/3 Cost: 0.732435 Epoch 32/40 Batch 3/3 Cost: 1.076102 Epoch 33/40 Batch 1/3 Cost: 0.065904 Epoch 33/40 Batch 2/3 Cost: 3.042134 Epoch 33/40 Batch 3/3 Cost: 1.361230 Epoch 34/40 Batch 1/3 Cost: 1.622181 Epoch 34/40 Batch 2/3 Cost: 0.974258 Epoch 34/40 Batch 3/3 Cost: 0.626623 Epoch 35/40 Batch 1/3 Cost: 1.162878 Epoch 35/40 Batch 2/3 Cost: 1.340035 Epoch 35/40 Batch 3/3 Cost: 0.813756 Epoch 36/40 Batch 1/3 Cost: 1.067085 Epoch 36/40 Batch 2/3 Cost: 0.347904 Epoch 36/40 Batch 3/3 Cost: 3.170678 Epoch 37/40 Batch 1/3 Cost: 0.878515 Epoch 37/40 Batch 2/3 Cost: 1.548400 Epoch 37/40 Batch 3/3 Cost: 0.206153 Epoch 38/40 Batch 1/3 Cost: 1.446815 Epoch 38/40 Batch 2/3 Cost: 0.541471 Epoch 38/40 Batch 3/3 Cost: 1.373899 Epoch 39/40 Batch 1/3 Cost: 1.694149 Epoch 39/40 Batch 2/3 Cost: 0.997300 Epoch 39/40 Batch 3/3 Cost: 0.072738 Epoch 40/40 Batch 1/3 Cost: 0.287293 Epoch 40/40 Batch 2/3 Cost: 1.123619 Epoch 40/40 Batch 3/3 Cost: 2.561045
In [69]:
new_var = torch.FloatTensor([[73, 80, 75]])
pred_y = model(new_var)
print("훈련 후 입력이 73, 80, 75일 때의 예측값 :", pred_y)
훈련 후 입력이 73, 80, 75일 때의 예측값 : tensor([[152.7064]], grad_fn=<AddmmBackward>)
예측값 152.7064 / 정답은 152¶
In [ ]:
In [ ]:
In [ ]:
In [ ]: