s t u d y . . ๐Ÿง/AI ์•ค ML ์•ค DL

[PyTorch] iris ๋ฐ์ดํ„ฐ ๋ถ„๋ฅ˜ ~ (w/๋ฉ€ํ‹ฐ ํผ์…‰ํŠธ๋ก )

H J 2023. 2. 26. 19:49

ํŒŒ์ดํ† ์น˜๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๋ฉ€ํ‹ฐ ํผ์…‰ํŠธ๋ก  ๊ตฌํ˜„ํ•˜๊ธฐ !

iris ๋ฐ์ดํ„ฐ๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๋ถ“๊ฝƒ์˜ ์ข…๋ฅ˜๋ฅผ ๋ถ„๋ฅ˜ํ•ด๋ณด์ž ~


PyTorch

PyTorch๋Š” Python์„ ์œ„ํ•œ ์˜คํ”ˆ์†Œ์Šค ๋จธ์‹  ๋Ÿฌ๋‹ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ !

GPU์‚ฌ์šฉ์ด ๊ฐ€๋Šฅํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์†๋„๊ฐ€ ์ƒ๋‹นํžˆ ๋น ๋ฅด๋‹ค

 

ํŒŒ์ดํ† ์น˜๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์ด์œ ๋Š”

1. ํŒŒ์ด์ฌ๊ณผ ์œ ์‚ฌํ•ด์„œ ์ฝ”๋“œ๋ฅผ ์ดํ•ดํ•˜๊ธฐ ์‰ฝ๋‹ค

2. ์„ค์ •๊ณผ ์‹คํ–‰์ด ๋งค์šฐ ์‰ฝ๋‹ค

3. ๋”ฅ๋Ÿฌ๋‹์„ ๋ฐฐ์šฐ๊ธฐ ์‰ฝ๋‹ค

4. ์—ฐ๊ตฌ์—๋„ ๋งŽ์ด ์‚ฌ์šฉ๋œ๋‹ค

๋“ฑ ~

 

IRIS ๋ฐ์ดํ„ฐ์…‹

๋ถ“๊ฝƒ์— ๋Œ€ํ•œ ๋ฐ์ดํ„ฐ !

๊ฝƒ๋ฐ›์นจ์˜ ๊ธธ์ด, ๋„ˆ๋น„์™€ ๊ฝƒ์žŽ์˜ ๊ธธ์ด, ๋„ˆ๋น„์— ๋Œ€ํ•œ 4์ฐจ์› ๋ฐ์ดํ„ฐ์ด๋‹ค


1. PyTorch ์„ค์น˜

์ผ๋‹จ ๋‚ด ๊ฐœ๋ฐœํ™˜๊ฒฝ์€ ๋งฅ์ด๊ธฐ ๋•Œ๋ฌธ์— mac ๊ธฐ์ค€์œผ๋กœ ์ง„ํ–‰

 

Anaconda | Anaconda Distribution

Anaconda's open-source Distribution is the easiest way to perform Python/R data science and machine learning on a single machine.

www.anaconda.com

์•„๋‚˜์ฝ˜๋‹ค ์„ค์น˜๋ฅผ ํ•ด์ค๋‹ˆ๋‹ค

 

๋งฅ์€ anaconda power shell์ด ๋”ฐ๋กœ ์—†๊ณ  cmd์ฐฝ์ด (base)๊ฐ€ ๋ถ™์œผ๋ฉด ํ™œ์„ฑํ™”๋œ ๊ฒƒ !

(base)๊ฐ€ ๋ถ™์—ˆ์ง€์š” ??

 

๊ทธ๋ฆฌ๊ณ  ํŒŒ์ด์ฌ๊ณผ ์•„๋‚˜์ฝ˜๋‹ค์˜ ๋ฒ„์ „์„ ํ™•์ธํ•ด์ค๋‹ˆ๋‹ค

 

๋‹ค์Œ์œผ๋กœ๋Š” conda๋กœ ํŒŒ์ดํ† ์น˜๋ฅผ ์„ค์น˜ !

 

PyTorch

An open source machine learning framework that accelerates the path from research prototyping to production deployment.

pytorch.org

 

PyTorch ์„ค์น˜ ์„ธ๋ถ€ ์ •๋ณด !

conda install pytorch torchvision torchaudio -c pytorch

์‹œํ‚จ๋Œ€๋กœ.. ๋ช…๋ น ์‹คํ–‰ !

 

๋„ต๋„ต ํ–ˆ์Šต๋‹ˆ๋‹ค

์ œ๋Œ€๋กœ ์„ค์น˜๋๋Š”์ง€ ํ™•์ธ์„ ํ•ด๋ณด๋ฉด..!

์ƒ˜ํ”Œ PyTorch ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•ด์„œ ์ž„์˜๋กœ ์ดˆ๊ธฐํ™”๋œ ํ…์„œ๋ฅผ ๊ตฌ์„ฑ ~

 

์ค‘๊ฐ„์— ์‹ค์ˆ˜๋Š”.. ๋„˜์–ด๊ฐ€๊ธฐ
vscode์—์„œ๋„ ๋˜๋Š”์ง€ ํ™•์ธ !

 

2. ๋ชจ๋ธ์— ๋Œ€ํ•œ ๋ฐ์ดํ„ฐ ์„ค์ •

๋ฐ์ดํ„ฐ์…‹ ๋‹ค์šด

Fisher์˜ ๋ถ“๊ฝƒ ๋ฐ์ดํ„ฐ ์„ธํŠธ์—์„œ ๋ชจ๋ธ์„ ํ•™์Šต !

์ด ๋ฐ์ดํ„ฐ์…‹์—๋Š” ๋ถ€์ฑ„๋ถ“๊ฝƒ(Iris setosa), ๋ฒ„์‹œ์ปฌ๋Ÿฌ ๋ถ“๊ฝƒ(Iris versicolor) ๋ฐ ๋ฒ„์ง€๋‹ˆ์นด ๋ถ“๊ฝƒ(Iris virginica)์˜ ์„ธ ๊ฐ€์ง€ ๋ถ“๊ฝƒ ์ข… ๊ฐ๊ฐ์— ๋Œ€ํ•œ 50๊ฐœ์˜ ๋ ˆ์ฝ”๋“œ๊ฐ€ ํฌํ•จ๋˜์–ด ์žˆ๋‹ค๊ณ  ํ•จ

 

์šฐ์„  IRIS ๋ฐ์ดํ„ฐ๋ฅผ ์—‘์…€ ํŒŒ์ผ๋กœ ์ €์žฅ

 

GitHub - microsoft/Windows-Machine-Learning: Samples and Tools for Windows ML.

Samples and Tools for Windows ML. Contribute to microsoft/Windows-Machine-Learning development by creating an account on GitHub.

github.com

๋ถ“๊ฝƒ์˜ ๋„ค ๊ฐ€์ง€ ํŠน์ง•๊ณผ ์ข…๋ฅ˜๊ฐ€ ์žˆ์Œ !

 

ํŒจํ‚ค์ง€ ์—‘์„ธ์Šค

import torch 
import pandas as pd 
import torch.nn as nn 
from torch.utils.data import random_split, DataLoader, TensorDataset 
import torch.nn.functional as F 
import numpy as np 
import torch.optim as optim 
from torch.optim import Adam

ํŒŒ์ด์ฌ ๋ฐ์ดํ„ฐ ๋ถ„์„์— ์‚ฌ์šฉ๋˜๋Š” pandas ํŒจํ‚ค์ง€๋„ ์ด์šฉํ•ด torch.nn ํŒจํ‚ค์ง€(๋ฐ์ดํ„ฐ ๋ฐ ์‹ ๊ฒฝ๋ง ๊ตฌ์ถ•์„ ์œ„ํ•œ ๋ชจ๋“ˆ๊ณผ ํ™•์žฅ ๊ฐ€๋Šฅํ•œ ํด๋ž˜์Šค๊ฐ€ ํฌํ•จ) ๋กœ๋“œ

 

๋ฐ์ดํ„ฐ์…‹ ๋กœ๋“œ

๋ฐ์ดํ„ฐ ๋กœ๋“œ -> ํด๋ž˜์Šค ์ˆ˜ ํ™•์ธ

# Loading the Data
df = pd.read_excel(r'C:…\Iris_dataset.xlsx') 
print('Take a look at sample from the dataset:') 
print(df.head()) 

# Let's verify if our data is balanced and what types of species we have  
print('\nOur dataset is balanced and has the following values to predict:') 
print(df['Iris_Type'].value_counts())

์—ฌ๊ธฐ์„œ ๋‚˜๋Š” Iris_dataset.xlsx ํŒŒ์ผ์˜ ๊ฒฝ๋กœ๋ฅผ ๋‚ด ํ™˜๊ฒฝ์— ๋งž๊ฒŒ ์ˆ˜์ •ํ•ด์ฃผ์—ˆ๋‹ค

 

์จ”์ž” ~

 

๋ฐ์ดํ„ฐ์…‹์„ ์‚ฌ์šฉํ•˜๊ณ  ๋ชจ๋ธ์„ ํ•™์Šตํ•˜๊ธฐ ์œ„ํ•ด ์ž…๋ ฅ ๋ฐ ์ถœ๋ ฅ์„ ์ •์˜ํ•ด์ค€๋‹ค

์ž…๋ ฅ์—๋Š” 150๊ฐœ์˜ ํŠน์ง•์ด ํฌํ•จ๋˜์–ด ์žˆ๊ณ , ์ถœ๋ ฅ์€ ๋ถ“๊ฝƒ ์ข…๋ฅ˜ ์—ด์ด๋‹ค

์‚ฌ์šฉํ•  ์‹ ๊ฒฝ๋ง์—๋Š” ์ˆซ์ž ๋ณ€์ˆ˜๊ฐ€ ํ•„์š”ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ถœ๋ ฅ ๋ณ€์ˆ˜๋ฅผ ์ˆซ์ž ํ˜•์‹์œผ๋กœ ๋ณ€ํ™˜ํ•œ๋‹ค

 

์ˆซ์ž ํ˜•์‹์œผ๋กœ ์ถœ๋ ฅ ํ‘œ์‹œ & ๋ฐ์ดํ„ฐ์…‹์— ์ƒˆ ์—ด ์ถ”๊ฐ€

# Convert Iris species into numeric types: Iris-setosa=0, Iris-versicolor=1, Iris-virginica=2.  
labels = {'Iris-setosa':0, 'Iris-versicolor':1, 'Iris-virginica':2} 
df['IrisType_num'] = df['Iris_Type']   # Create a new column "IrisType_num" 
df.IrisType_num = [labels[item] for item in df.IrisType_num]  # Convert the values to numeric ones 

# Define input and output datasets 
input = df.iloc[:, 1:-2]            # We drop the first column and the two last ones. 
print('\nInput values are:') 
print(input.head())   
output = df.loc[:, 'IrisType_num']   # Output Y is the last column  
print('\nThe output value is:') 
print(output.head())

์—ฌ๊ธฐ์„œ ๋ชจ๋ธ์„ ํ•™์Šตํ•˜๊ธฐ ์œ„ํ•ด์„  ๋ชจ๋ธ ์ž…๋ ฅ ๋ฐ ์ถœ๋ ฅ ํ˜•์‹์œผ๋กœ ํ…์„œ๋กœ ๋ณ€ํ™˜ํ•ด์ค€๋‹ค

 

ํ…์„œ๋กœ ๋ณ€ํ™˜

# Convert Input and Output data to Tensors and create a TensorDataset 
input = torch.Tensor(input.to_numpy())      # Create tensor of type torch.float32 
print('\nInput format: ', input.shape, input.dtype)     # Input format: torch.Size([150, 4]) torch.float32 
output = torch.tensor(output.to_numpy())        # Create tensor type torch.int64  
print('Output format: ', output.shape, output.dtype)  # Output format: torch.Size([150]) torch.int64 
data = TensorDataset(input, output)    # Create a torch.utils.data.TensorDataset object for further data manipulation

์ž…๋ ฅ ๋ฐ ์ถœ๋ ฅ ํ˜•์‹ ํ‘œ์‹œ

์ž…๋ ฅ ๊ฐ’์€ 150๊ฐœ์ด๊ณ , ์ด ์ค‘ 60%๋Š” ๋ชจ๋ธ ํ•™์Šต ๋ฐ์ดํ„ฐ, ์œ ํšจ์„ฑ ๊ฒ€์‚ฌ(val) 20%, ํ…Œ์ŠคํŠธ(test) 30%๋กœ ๋‚˜๋ˆ ์ค€๋‹ค

 

๋ฐ์ดํ„ฐ ๋ถ„ํ• 

# Split to Train, Validate and Test sets using random_split 
train_batch_size = 10        
number_rows = len(input)    # The size of our dataset or the number of rows in excel table.  
test_split = int(number_rows*0.3)  
validate_split = int(number_rows*0.2) 
train_split = number_rows - test_split - validate_split     
train_set, validate_set, test_set = random_split( 
    data, [train_split, validate_split, test_split])    
 
# Create Dataloader to read the data within batch sizes and put into memory. 
train_loader = DataLoader(train_set, batch_size = train_batch_size, shuffle = True) 
validate_loader = DataLoader(validate_set, batch_size = 1) 
test_loader = DataLoader(test_set, batch_size = 1)

 

3. PyTorch ๋ชจ๋ธ ํ•™์Šต

๋„ต๋„ต

์‹ ๊ฒฝ๋ง ์ •์˜

์—ฌ๊ธฐ์„œ๋Š” ์„ธ ๊ฐœ์˜ ์„ ํ˜• ๊ณ„์ธต์„ ์‚ฌ์šฉํ•ด ๊ธฐ๋ณธ ์‹ ๊ฒฝ๋ง ๋ชจ๋ธ์„ ๋นŒ๋“œ !

Linear -> ReLU -> Linear -> ReLU -> Linear

 

Linear : ๋“ค์–ด์˜ค๋Š” ๋ฐ์ดํ„ฐ์— ์„ ํ˜• ๋ณ€ํ™˜ ์ ์šฉ, ํด๋ž˜์Šค ์ˆ˜์— ํ•ด๋‹นํ•˜๋Š” ์ž…๋ ฅ, ์ถœ๋ ฅ ์ˆ˜๋ฅผ ์ง€์ •ํ•ด์•ผ ํ•จ

ReLU : ๋“ค์–ด์˜ค๋Š” ๋ชจ๋“  ๊ธฐ๋Šฅ์„ 0 ์ด์ƒ์œผ๋กœ ์ •์˜ํ•˜๋Š” ํ™œ์„ฑํ™” ํ•จ์ˆ˜, 0๋ณด๋‹ค ์ž‘์€ ์ˆซ์ž๋Š” 0์œผ๋กœ ๋ณ€๊ฒฝ๋˜๊ณ  ๋‚˜๋จธ์ง€๋Š” ์œ ์ง€

-> ๋‘ ๊ฐœ์˜ ๊ณ„์ธต์— ํ™œ์„ฑํ™” ๊ณ„์ธต์„ ์ ์šฉํ•˜๊ณ  ๋งˆ์ง€๋ง‰ ์„ ํ˜• ๊ณ„์ธต์—์„œ๋Š” ํ™œ์„ฑํ™”๋ฅผ ์ ์šฉํ•˜์ง€ ์•Š์Œ

 

๋ชจ๋ธ์˜ ๋งค๊ฐœ ๋ณ€์ˆ˜ + ์‹ ๊ฒฝ๋ง ์ •์˜

๋ชจ๋ธ์˜ ๋งค๊ฐœ ๋ณ€์ˆ˜๋Š” ๋ชฉํ‘œ์™€ ํ•™์Šต ๋ฐ์ดํ„ฐ์— ๋”ฐ๋ผ ๋‹ฌ๋ผ์ง„๋‹ค

์ž…๋ ฅ ํฌ๊ธฐ๋Š” ๋ชจ๋ธ์„ ์ œ๊ณตํ•˜๋Š” ๊ธฐ๋Šฅ ์ˆ˜์— ๋”ฐ๋ผ ๋‹ฌ๋ผ์ง (iris ๋ฐ์ดํ„ฐ์˜ ๊ฒฝ์šฐ 4)

iris ๋ฐ์ดํ„ฐ์˜ ์ถœ๋ ฅ ํฌ๊ธฐ๋Š” 3 (์„ธ ๊ฐ€์ง€ ์ข…๋ฅ˜์˜ ๋ถ“๊ฝƒ)

 

3๊ฐœ์˜ ์„ ํ˜• ๊ณ„์ธต((4,24) -> (24,24) -> (24,3))์ด ์žˆ๋Š” ๋„คํŠธ์›Œํฌ์—๋Š” 744๊ฐœ์˜ ๊ฐ€์ค‘์น˜(96+576+72)๊ฐ€ ์žˆ์Œ

 

lr(ํ•™์Šต ์†๋„)์€ ์†์‹ค ๊ทธ๋ ˆ์ด๋””์–ธํŠธ์™€ ๊ด€๋ จํ•˜์—ฌ ๋„คํŠธ์›Œํฌ์˜ ๊ฐ€์ค‘์น˜๋ฅผ ์กฐ์ •ํ•˜๋Š” ์ •๋„์— ๋Œ€ํ•œ ์ œ์–ด๋ฅผ ์„ค์ •

์ด ๊ฐ’์ด ๋‚ฎ์„์ˆ˜๋ก ํ•™์Šต ์†๋„๊ฐ€ ๋Š๋ ค์ง -> ์—ฌ๊ธฐ์„œ๋Š” 0.01๋กœ ์„ค์ •

 

ํ•™์Šต ์ค‘ ๋ชจ๋“  ๊ณ„์ธต์„ ํ†ตํ•ด ์ž…๋ ฅ ์ฒ˜๋ฆฌ -> ์†์‹ค ๊ณ„์‚ฐ -> ์˜ˆ์ธก ๋ ˆ์ด๋ธ”๊ณผ ์˜ฌ๋ฐ”๋ฅธ ๋ ˆ์ด๋ธ”์˜ ๋‹ค๋ฅธ ์ •๋ณด ํŒŒ์•… -> ๊ทธ๋ ˆ์ด๋””์–ธํŠธ๋ฅผ ๋„คํŠธ์›Œํฌ์— ์žฌ์ „ํŒŒ -> ๊ณ„์ธต ๊ฐ€์ค‘์น˜ ์—…๋ฐ์ดํŠธ => ๋ฐฉ๋Œ€ํ•œ ์ž…๋ ฅ ๋ฐ์ดํ„ฐ์…‹์„ ๋ฐ˜๋ณตํ•ด ๊ฐ€์ค‘์น˜ ์„ค์ •ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ํ•™์Šต

 

์ •๋ฐฉํ–ฅ ํ•จ์ˆ˜๋Š” ์†์‹ค ํ•จ์ˆ˜์˜ ๊ฐ’์„ ๊ณ„์‚ฐ + ์—ญ๋ฐฉํ–ฅ ํ•จ์ˆ˜๋Š” ํ•™์Šต ๊ฐ€๋Šฅํ•œ ๋งค๊ฐœ ๋ณ€์ˆ˜์˜ ๊ทธ๋ ˆ์ด๋””์–ธํŠธ ๊ณ„์‚ฐ

pytorch๋ฅผ ์‚ฌ์šฉํ•ด ์‹ ๊ฒฝ๋ง์„ ๋งŒ๋“œ๋Š” ๊ฒฝ์šฐ ์ •๋ฐฉํ–ฅ ํ•จ์ˆ˜๋งŒ ์ •์˜ (์—ญ๋ฐฉํ–ฅ ํ•จ์ˆ˜๋Š” ์ž๋™์œผ๋กœ ์ •์˜๋จ)

 

๋ชจ๋ธ์˜ ๋งค๊ฐœ ๋ณ€์ˆ˜์™€ ์‹ ๊ฒฝ๋ง์„ ์ •์˜ํ•ด์ค๋‹ˆ๋‹ค

# Define model parameters 
input_size = list(input.shape)[1]   # = 4. The input depends on how many features we initially feed the model. In our case, there are 4 features for every predict value  
learning_rate = 0.01 
output_size = len(labels)           # The output is prediction results for three types of Irises.  


# Define neural network 
class Network(nn.Module): 
   def __init__(self, input_size, output_size): 
       super(Network, self).__init__() 
        
       self.layer1 = nn.Linear(input_size, 24) 
       self.layer2 = nn.Linear(24, 24) 
       self.layer3 = nn.Linear(24, output_size) 


   def forward(self, x): 
       x1 = F.relu(self.layer1(x)) 
       x2 = F.relu(self.layer2(x1)) 
       x3 = self.layer3(x2) 
       return x3 
 
# Instantiate the model 
model = Network(input_size, output_size)

 

์‹คํ–‰ ๋””๋ฐ”์ด์Šค ์ •์˜

PC์—์„œ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•œ ์‹คํ–‰ ๋””๋ฐ”์ด์Šค๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์‹คํ–‰ ๋””๋ฐ”์ด์Šค๋ฅผ ์ •์˜

๋””๋ฐ”์ด์Šค๋Š” Nvidia GPU๊ฐ€ ์ปดํ“จํ„ฐ์— ์žˆ์œผ๋ฉด Nvidia GPU๊ฐ€ ๋˜๊ณ , ์—†์œผ๋ฉด CPU๋กœ ์„ค์ •๋จ

# Define your execution device 
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu") 
print("The model will be running on", device, "device\n") 
model.to(device)    # Convert model parameters and buffers to CPU or Cuda

 

๋ชจ๋ธ ์ €์žฅ

# Function to save the model 
def saveModel(): 
    path = "./NetModel.pth" 
    torch.save(model.state_dict(), path)

 

์†์‹ค ํ•จ์ˆ˜ ์ •์˜

์†์‹ค ํ•จ์ˆ˜๋Š” ์ถœ๋ ฅ์ด ๋Œ€์ƒ๊ณผ ๋‹ค๋ฅธ ์ •๋„๋ฅผ ์˜ˆ์ธกํ•˜๋Š” ๊ฐ’์„ ๊ณ„์‚ฐ

์ฃผ์š” ๋ชฉํ‘œ๋Š” ์‹ ๊ฒฝ๋ง์˜ ์—ญ๋ฐฉํ–ฅ ์ „ํŒŒ๋ฅผ ํ†ตํ•ด ๊ฐ€์ค‘์น˜ ๋ฒกํ„ฐ ๊ฐ’์„ ๋ณ€๊ฒฝํ•˜์—ฌ ์†์‹ค ํ•จ์ˆ˜์˜ ๊ฐ’์„ ์ค„์ด๋Š” ๊ฒƒ !

 

์†์‹ค ๊ฐ’์€ ๋ชจ๋ธ ์ •ํ™•๋„์™€ ๋‹ค๋ฆ„! ์†์‹ค ํ•จ์ˆ˜๋Š” ํ•™์Šต ์„ธํŠธ์—์„œ ์ตœ์ ํ™”๋ฅผ ๋ฐ˜๋ณตํ•  ๋•Œ๋งˆ๋‹ค ๋ชจ๋ธ์ด ์–ผ๋งˆ๋‚˜ ์ž˜ ์ž‘๋™ํ•˜๋Š”์ง€ ๋‚˜ํƒ€๋ƒ„

(๋ชจ๋ธ์˜ ์ •ํ™•๋„๋Š” ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ์—์„œ ๊ณ„์‚ฐ๋˜๋ฉฐ ์ •ํ™•ํ•œ ์˜ˆ์ธก์˜ ๋ฐฑ๋ถ„์œจ)

 

์—ฌ๊ธฐ์„œ๋Š” ๋ถ„๋ฅ˜์— ์ตœ์ ํ™”๋œ ๊ธฐ์กด ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉ, Classification Cross-Entropy ์†์‹ค ํ•จ์ˆ˜์™€ Adam ์ตœ์ ํ™” ํ”„๋กœ๊ทธ๋žจ์„ ์‚ฌ์šฉ

์ตœ์ ํ™” ํ”„๋กœ๊ทธ๋žจ์—์„œ lr(ํ•™์Šต ์†๋„)์€ ์†์‹ค ๊ทธ๋ ˆ์ด๋””์–ธํŠธ์™€ ๊ด€๋ จํ•˜์—ฌ ๋„คํŠธ์›Œํฌ์˜ ๊ฐ€์ค‘์น˜๋ฅผ ์กฐ์ •ํ•˜๋Š” ์ •๋„์— ๋Œ€ํ•œ ์ œ์–ด๋ฅผ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” 0.001๋กœ ์„ค์ •

 

# Define the loss function with Classification Cross-Entropy loss and an optimizer with Adam optimizer
loss_fn = nn.CrossEntropyLoss()
optimizer = Adam(model.parameters(), lr=0.001, weight_decay=0.0001)

 

๋ชจ๋ธ ํ•™์Šต

ํ•™์Šต ์„ธํŠธ์— ๊ฑธ์นœ ๋ชจ๋“  Epoch ๋˜๋Š” ๋ชจ๋“  ์ „์ฒด ๋ฐ˜๋ณต์— ๋Œ€ํ•ด ํ•™์Šต ์†์‹ค, ์œ ํšจ์„ฑ ๊ฒ€์‚ฌ ์†์‹ค ๋ฐ ๋ชจ๋ธ์˜ ์ •ํ™•๋„๋ฅผ ํ‘œ์‹œ

๊ฐ€์žฅ ๋†’์€ ์ •ํ™•๋„๋กœ ๋ชจ๋ธ์„ ์ €์žฅํ•˜๊ณ  10๋ฒˆ์˜ Epoch ํ›„์— ํ”„๋กœ๊ทธ๋žจ์ด ์ตœ์ข… ์ •ํ™•๋„๋ฅผ ํ‘œ์‹œ

 

# Training Function 
def train(num_epochs): 
    best_accuracy = 0.0 
     
    print("Begin training...") 
    for epoch in range(1, num_epochs+1): 
        running_train_loss = 0.0 
        running_accuracy = 0.0 
        running_vall_loss = 0.0 
        total = 0 
 
        # Training Loop 
        for data in train_loader: 
        #for data in enumerate(train_loader, 0): 
            inputs, outputs = data  # get the input and real species as outputs; data is a list of [inputs, outputs] 
            optimizer.zero_grad()   # zero the parameter gradients          
            predicted_outputs = model(inputs)   # predict output from the model 
            train_loss = loss_fn(predicted_outputs, outputs)   # calculate loss for the predicted output  
            train_loss.backward()   # backpropagate the loss 
            optimizer.step()        # adjust parameters based on the calculated gradients 
            running_train_loss +=train_loss.item()  # track the loss value 
 
        # Calculate training loss value 
        train_loss_value = running_train_loss/len(train_loader) 
 
        # Validation Loop 
        with torch.no_grad(): 
            model.eval() 
            for data in validate_loader: 
               inputs, outputs = data 
               predicted_outputs = model(inputs) 
               val_loss = loss_fn(predicted_outputs, outputs) 
             
               # The label with the highest value will be our prediction 
               _, predicted = torch.max(predicted_outputs, 1) 
               running_vall_loss += val_loss.item()  
               total += outputs.size(0) 
               running_accuracy += (predicted == outputs).sum().item() 
 
        # Calculate validation loss value 
        val_loss_value = running_vall_loss/len(validate_loader) 
                
        # Calculate accuracy as the number of correct predictions in the validation batch divided by the total number of predictions done.  
        accuracy = (100 * running_accuracy / total)     
 
        # Save the model if the accuracy is the best 
        if accuracy > best_accuracy: 
            saveModel() 
            best_accuracy = accuracy 
         
        # Print the statistics of the epoch 
        print('Completed training batch', epoch, 'Training Loss is: %.4f' %train_loss_value, 'Validation Loss is: %.4f' %val_loss_value, 'Accuracy is %d %%' % (accuracy))

 

ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ๋กœ ๋ชจ๋ธ ํ…Œ์ŠคํŠธ

๋‘ ๊ฐœ์˜ ํ…Œ์ŠคํŠธ ํ•จ์ˆ˜๋ฅผ ์ถ”๊ฐ€

 

์ฒซ ๋ฒˆ์งธ๋Š” ์ด์ „ ํŒŒํŠธ์—์„œ ์ €์žฅํ•œ ๋ชจ๋ธ์„ ํ…Œ์ŠคํŠธ

45๊ฐœ ํ•ญ๋ชฉ์˜ ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ ์„ธํŠธ๋กœ ๋ชจ๋ธ์„ ํ…Œ์ŠคํŠธํ•˜๊ณ  ๋ชจ๋ธ์˜ ์ •ํ™•๋„๋ฅผ ์ถœ๋ ฅ

 

๋‘ ๋ฒˆ์งธ๋Š” ์„ธ ๊ฐ€์ง€ ๋ถ“๊ฝƒ ์ข… ์ค‘์—์„œ ๊ฐ ์ข…์„ ์„ฑ๊ณต์ ์œผ๋กœ ๋ถ„๋ฅ˜ํ•˜๋Š” ํ™•๋ฅ ์„ ์˜ˆ์ธกํ•˜๋Š” ๋ชจ๋ธ์˜ ์‹ ๋ขฐ๋„๋ฅผ ํ…Œ์ŠคํŠธํ•˜๋Š” ์„ ํƒ์  ํ•จ์ˆ˜

# Function to test the model 
def test(): 
    # Load the model that we saved at the end of the training loop 
    model = Network(input_size, output_size) 
    path = "NetModel.pth" 
    model.load_state_dict(torch.load(path)) 
     
    running_accuracy = 0 
    total = 0 
 
    with torch.no_grad(): 
        for data in test_loader: 
            inputs, outputs = data 
            outputs = outputs.to(torch.float32) 
            predicted_outputs = model(inputs) 
            _, predicted = torch.max(predicted_outputs, 1) 
            total += outputs.size(0) 
            running_accuracy += (predicted == outputs).sum().item() 
 
        print('Accuracy of the model based on the test set of', test_split ,'inputs is: %d %%' % (100 * running_accuracy / total))    
 
 
# Optional: Function to test which species were easier to predict  
def test_species(): 
    # Load the model that we saved at the end of the training loop 
    model = Network(input_size, output_size) 
    path = "NetModel.pth" 
    model.load_state_dict(torch.load(path)) 
     
    labels_length = len(labels) # how many labels of Irises we have. = 3 in our database. 
    labels_correct = list(0. for i in range(labels_length)) # list to calculate correct labels [how many correct setosa, how many correct versicolor, how many correct virginica] 
    labels_total = list(0. for i in range(labels_length))   # list to keep the total # of labels per type [total setosa, total versicolor, total virginica] 
  
    with torch.no_grad(): 
        for data in test_loader: 
            inputs, outputs = data 
            predicted_outputs = model(inputs) 
            _, predicted = torch.max(predicted_outputs, 1) 
             
            label_correct_running = (predicted == outputs).squeeze() 
            label = outputs[0] 
            if label_correct_running.item():  
                labels_correct[label] += 1 
            labels_total[label] += 1  
  
    label_list = list(labels.keys()) 
    for i in range(output_size): 
        print('Accuracy to predict %5s : %2d %%' % (label_list[i], 100 * labels_correct[i] / labels_total[i]))

 

๋งˆ์ง€๋ง‰์œผ๋กœ, ์ฃผ ์ฝ”๋“œ๋ฅผ ์ถ”๊ฐ€

๋ชจ๋ธ ํ•™์Šต์ด ์‹œ์ž‘, ๋ชจ๋ธ ์ €์žฅ, ๊ฒฐ๊ณผ๊ฐ€ ํ™”๋ฉด์— ํ‘œ์‹œ

ํ•™์Šต ์„ธํŠธ์— ๋Œ€ํ•ด ๋‘ ๋ฒˆ์˜ ๋ฐ˜๋ณต([num_epochs = 25])๋งŒ ์‹คํ–‰ -> ํ•™์Šต ํ”„๋กœ์„ธ์Šค๊ฐ€ ๋„ˆ๋ฌด ์˜ค๋ž˜ ๊ฑธ๋ฆฌ์ง€ ์•Š์Œ

if __name__ == "__main__": 
    num_epochs = 10
    train(num_epochs) 
    print('Finished Training\n') 
    test() 
    test_species()

๊ฒฐ๊ณผ..!

์Œ.. ์ €๋Š” ์™œ ์ด๋ž˜์š”....................

์ง€์ณค๋„๋‹ค....

 

์—.. ๋‚ด๊ฐ€ ์ฐธ๊ณ ํ•œ ๊ธ€์—์„œ๋Š” ์—‘์…€ ํŒŒ์ผ์—์„œ #๊ฐ€ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ๋ผ์žˆ๊ธธ๋ž˜ ์ •๋ ฌํ•˜๊ณ  ๋‹ค์‹œ ๋Œ๋ ธ๋”๋‹ˆ..

๊ทผ๋ฐ ์—ฌ๋Ÿฌ ๋ฒˆ ๋Œ๋ฆฌ๋ฉด ์‹น์‹น ๋ณ€ํ•ด ์•„์ฃผ..

์–ด์ฐจํ”ผ ์…”ํ”Œํ•˜๋Š”๋ฐ ์ด๊ฒŒ ๋ฌด์Šจ ์†Œ์šฉ์ด๋žŒ

 

์•„๋ž˜๋Š” ๋‚ด๊ฐ€ ์ฐธ๊ณ ํ•œ ๊ธ€

๋”๋ณด๊ธฐ
 

PyTorch ๋ฐ Windows ML์„ ์‚ฌ์šฉํ•œ ๋ฐ์ดํ„ฐ ๋ถ„์„

PyTorch๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ML ๋ฐ์ดํ„ฐ ๋ถ„์„ ๋ชจ๋ธ์„ ๋งŒ๋“ค๊ณ  ONNX๋กœ ๋‚ด๋ณด๋‚ด๊ณ  ๋กœ์ปฌ ์•ฑ์— ๋ฐฐํฌํ•˜๋Š” ๋‹จ๊ณ„ ์•Œ์•„๋ณด๊ธฐ

learn.microsoft.com

๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค... ๋•๋ถ„์— ์—‰๋ฉ์ด ๋ถ™์ด๊ณ  ๊ธ€ ์ฝ๊ณ  ๋”ฐ๋ผํ•˜๊ธฐ๋งŒ ํ–ˆ๋Š”๋ฐ ์•„์ด๋ฆฌ์Šค ๋ถ„๋ฅ˜ ๋ชจ๋ธ์„ ๋งŒ๋“ค๊ธฐ๋Š” ํ–ˆ์–ด์š”..

๋‘ ๋ฒˆ์งธ ์ข…๋ฅ˜ ์ •ํ™•๋„๋Š” 0%์ด์ง€๋งŒ...

 

์•„๋ž˜ ๊ธ€์€ ๋ญ”๊ฐ€ ๋„์›€์ด ๋  ๊ฒƒ ๊ฐ™์•„์„œ ๊ฐ€์ ธ์™€๋ดค๋‹ค ๋‚˜์ค‘์— ์ด๊ฑธ๋กœ ํ•ด๋ด์•ผ์ง€

 

์ž…๋ฌธ์ž๋ฅผ ์œ„ํ•œ ๋จธ์‹ ๋Ÿฌ๋‹ ๋ถ„๋ฅ˜ ํŠœํ† ๋ฆฌ์–ผ - IRIS ๋ถ„๋ฅ˜

๊ฐœ์š” ์‚ฌ์ดํ‚ท๋Ÿฐ(scikit-learn)์€ ํŒŒ์ด์ฌ ๋จธ์‹ ๋Ÿฌ๋‹ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์ด๋‹ค. ํŒŒ์ด์ฌ์—์„œ ๋‚˜์˜ค๋Š” ์ตœ์‹  ์•Œ๊ณ ๋ฆฌ์ฆ˜๋“ค๋„ ์ด์ œ๋Š” ์‚ฌ์ดํ‚ท๋Ÿฐ์— ํ†ตํ•ฉํ•˜๋Š” ํ˜•ํƒœ๋กœ ์ทจํ•˜๊ณ  ์žˆ๋‹ค. ๊ตฌ๊ธ€ ์ฝ”๋žฉ์€ ๊ธฐ๋ณธ์ ์œผ๋กœ ์‚ฌ์ดํ‚ท๋Ÿฐ๊นŒ์ง€

dschloe.github.io

 

 

Classifying the Iris Data Set with PyTorch

In this short article we will have a look on how to use PyTorch with the Iris data set. We will create and train a neural network with Linear layers and we will employ a Softmax activation function and the Adam optimizer.

janakiev.com

์‚ฌ์‹ค ๋‘๋ฒˆ์งธ ๊ธ€๋„ ๋”ฐ๋ผ์„œ ํ•ด๋ดค๋Š”๋ฐ (์ œ๋Œ€๋กœ ๊ณต๋ถ€ํ•˜์ง„ ์•Š์•˜๋„๋‹ค)

๋ง˜๋Œ€๋กœ max ์ถœ๋ ฅํ•˜๊ณ  ์ •ํ™•๋„๋ผ๊ณ  ์ƒ๊ฐํ•˜๊ธฐ ..