파이토치 (예제)

짬그브·2025년 3월 26일

예제 1

import pandas as pd
import torch
import numpy as np
import torch.nn as nn
import torch.optim as optim
from tqdm import tqdm

data = pd.read_csv('diabetes.csv')
data.info()

data = torch.from_numpy(data.values).float()
x_train = data[:,:-1]
y_train = data[:,-1:]

print(x_train.shape, y_train.shape)

from torch.utils.data import TensorDataset
from torch.utils.data import DataLoader

dataset = TensorDataset(x_train, y_train)
dataloader = DataLoader(dataset, batch_size=32, shuffle=True)


for data in dataloader:
    print(data, end='\n\n')
print()

import torch.nn as nn
import torch.optim as optim

class LogisticModel(nn.Module):
    def __init__(self, input_size, output_size):
        super().__init__()
        self.Linear = nn.Linear(input_size, output_size)
        self.model = nn.Sequential(
            nn.Linear(8, 10),
            nn.Sigmoid(),
            nn.Linear(10, 10),
            nn.Sigmoid(),
            nn.Linear(10, 1),
            nn.Sigmoid()
        )
        self.sigmoid = nn.Sigmoid()

    def forward(self, x):
        # y = self.sigmoid(self.Linear(x))
        y = self.model(x)
        return y

model = LogisticModel(input_size=x_train.size(-1), output_size=y_train.size(-1))
optimizer = optim.SGD(model.parameters(), lr=1e-5)

import torch.nn.functional as F
iter_bar = tqdm(range(1000))
for epoch in iter_bar:
    allhyp = []
    for batch_idx, data in enumerate(dataloader):
        batch_x, batch_y = data
        hypothesis = model(batch_x)
        allhyp.append(hypothesis)
        loss = F.binary_cross_entropy(hypothesis, batch_y)
        optimizer.zero_grad()
        loss.backward()
        optimizer.step()
        iter_bar.set_postfix({"epoch": f"{epoch + 1}",
                              "batch": f'{batch_idx + 1}',
                              'loss': f'{loss.item():.4f}',
                              })
        # print(f'{epoch + 1} / {batch_idx + 1}, loss:{loss.item():.4f}')

correction_cnt = (y_train == (torch.cat(allhyp, dim=0) > 0.5)).sum()
print('accuracy : {:2.1f}'.format(correction_cnt/float(y_train.size(0))*100))


100%|██████████| 1000/1000 [00:18<00:00, 54.53it/s, epoch=1000, batch=24, loss=0.5992]
accuracy : 65.2

예제2

import numpy as np
import torch
from torch.utils.data import Dataset, DataLoader

from day1.linearEx2 import hypothesis
from day2.modelEx4 import correction_prediction
from day3.perception import accuracy


class DiabetsDataset(Dataset):
    def __init__(self):
        super().__init__()
        xy = np.loadtxt('diabetes.csv', delimiter=',', dtype=np.float32)
        self.len_size = xy.shape[0]
        self.x_data = torch.from_numpy(xy[:,:-1])
        self.y_data = torch.from_numpy(xy[:,[-1]])

    def __len__(self):
        return self.len_size

    def __getitem__(self, idx):
        return self.x_data[idx], self.y_data[idx]

dataset = DiabetsDataset()
train_loader = DataLoader(dataset, batch_size=32, shuffle=True)

import torch.nn as nn
class LogisticModel(nn.Module):
    def __init__(self):
        super().__init__()
        self.l1 = nn.Linear(8,50)
        self.l2 = nn.Linear(50,10)
        self.l3 = nn.Linear(10,1)

        self.sigmoid = nn.Sigmoid()

    def forward(self, x):
        out1 = self.sigmoid(self.l1(x))
        out2 = self.sigmoid(self.l2(out1))
        y = self.sigmoid(self.l3(out2))
        return y


import torch.optim as optim

model = LogisticModel()
loss_func = nn.BCELoss()
optimizer = optim.SGD(model.parameters(), lr= 0.001)

for epoch in range(5000):
    for idx, data in enumerate(train_loader):
        x_train, y_train = data
        hypothesis = model(x_train)
        loss = loss_func(hypothesis, y_train)
        optimizer.zero_grad()
        loss.backward()
        optimizer.step()

    if epoch % 100 == 0:
        prediction = hypothesis > torch.FloatTensor([0.5])
        correction_prediction = prediction.float() == y_train
        accuracy = correction_prediction.sum().item() / len(correction_prediction)
        print('epoch:{} loss:{:.4f} accuracy {:2.2f}'.format(epoch, loss.item(), accuracy * 100))


epoch:9601 loss:0.0001
epoch:9701 loss:0.0001
epoch:9801 loss:0.0001
epoch:9901 loss:0.0001
hypothesis:
[[1.3890117e-04]
 [9.9993896e-01]
 [9.9994147e-01]
 [9.9999535e-01]]
prediction:
[[0.]
 [1.]
 [1.]
 [1.]]
target:
[[0.]
 [1.]
 [1.]
 [1.]]
accuracy:1.0000
epoch:0 loss:0.7057 accuracy 53.12
epoch:100 loss:0.6432 accuracy 65.62
epoch:200 loss:0.6421 accuracy 65.62
epoch:300 loss:0.7002 accuracy 56.25
epoch:400 loss:0.6402 accuracy 65.62
epoch:500 loss:0.6783 accuracy 59.38
epoch:600 loss:0.6919 accuracy 56.25
epoch:700 loss:0.6433 accuracy 65.62
epoch:800 loss:0.5763 accuracy 75.00
epoch:900 loss:0.6729 accuracy 59.38
epoch:1000 loss:0.5287 accuracy 81.25
epoch:1100 loss:0.6691 accuracy 59.38
epoch:1200 loss:0.5760 accuracy 75.00
epoch:1300 loss:0.5768 accuracy 75.00
epoch:1400 loss:0.6627 accuracy 59.38
epoch:1500 loss:0.5887 accuracy 71.88
epoch:1600 loss:0.6328 accuracy 65.62
epoch:1700 loss:0.5561 accuracy 78.12
epoch:1800 loss:0.6706 accuracy 59.38
epoch:1900 loss:0.6334 accuracy 62.50
epoch:2000 loss:0.6827 accuracy 59.38
epoch:2100 loss:0.6361 accuracy 65.62
epoch:2200 loss:0.7319 accuracy 43.75
epoch:2300 loss:0.6352 accuracy 59.38
epoch:2400 loss:0.6013 accuracy 65.62
epoch:2500 loss:0.6357 accuracy 56.25
epoch:2600 loss:0.6511 accuracy 56.25
epoch:2700 loss:0.6002 accuracy 62.50
epoch:2800 loss:0.6043 accuracy 65.62
epoch:2900 loss:0.5813 accuracy 68.75
epoch:3000 loss:0.5811 accuracy 62.50

예제3

import pandas as pd
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
from sklearn.model_selection import train_test_split


iris = pd.read_csv('iris.csv')
iris.info()
print(iris.head(10))
print(iris.Name)
print(iris.Name.unique())
iris['Name']= iris['Name'].map({'Iris-setosa':0,'Iris-versicolor':1,'Iris-virginica':2})
print(iris.Name.unique())

data = torch.from_numpy(iris.values).float()

x_train,x_test,y_train,y_test = train_test_split(
    data[:, :-1],data[:, -1].long(), test_size=0.2, random_state=42)

class NN(nn.Module):
    def __init__(self):
        super().__init__()
        self.fc1 = nn.Linear(4,16)
        self.fc2 = nn.Linear(16,12)
        self.fc3 = nn.Linear(12,3)

    def forward(self,x):
        out = F.relu(self.fc1(x))
        out = F.relu(self.fc2(out))
        y = self.fc3(out)
        return y

model = NN()
loss_func = nn.CrossEntropyLoss()
optimizer = optim.Adam(model.parameters(), lr=0.01)

for epoch in range(100):
    hypothesis = model(x_train)
    loss = loss_func(hypothesis, y_train)
    optimizer.zero_grad()
    loss.backward()
    optimizer.step()

    if epoch % 10 == 0:
        print(epoch, loss.item())

preds = []
for val in x_test:
    hypothesis = model(val)
    preds.append(hypothesis.argmax().numpy())

df = pd.DataFrame({'target':y_test, 'pred':preds})
df['correct'] = [1 if corr == pred else 0 for corr, pred in zip(df['target'], df['pred'])]
print(df)

<class 'pandas.core.frame.DataFrame'>
RangeIndex: 150 entries, 0 to 149
Data columns (total 5 columns):
 #   Column        Non-Null Count  Dtype  
---  ------        --------------  -----  
 0   sepal_length  150 non-null    float64
 1   sepal_width   150 non-null    float64
 2   petal_length  150 non-null    float64
 3   petal_width   150 non-null    float64
 4   Name          150 non-null    object 
dtypes: float64(4), object(1)
memory usage: 6.0+ KB
   sepal_length  sepal_width  petal_length  petal_width         Name
0           5.1          3.5           1.4          0.2  Iris-setosa
1           4.9          3.0           1.4          0.2  Iris-setosa
2           4.7          3.2           1.3          0.2  Iris-setosa
3           4.6          3.1           1.5          0.2  Iris-setosa
4           5.0          3.6           1.4          0.2  Iris-setosa
5           5.4          3.9           1.7          0.4  Iris-setosa
6           4.6          3.4           1.4          0.3  Iris-setosa
7           5.0          3.4           1.5          0.2  Iris-setosa
8           4.4          2.9           1.4          0.2  Iris-setosa
9           4.9          3.1           1.5          0.1  Iris-setosa
0         Iris-setosa
1         Iris-setosa
2         Iris-setosa
3         Iris-setosa
4         Iris-setosa
            ...      
145    Iris-virginica
146    Iris-virginica
147    Iris-virginica
148    Iris-virginica
149    Iris-virginica
Name: Name, Length: 150, dtype: object
['Iris-setosa' 'Iris-versicolor' 'Iris-virginica']
[0 1 2]
0 1.0447551012039185
10 0.7430341839790344
20 0.4817091226577759
30 0.35662034153938293
40 0.2434498518705368
50 0.14152878522872925
60 0.0894223228096962
70 0.07082732766866684
80 0.06427659839391708
90 0.06143709644675255
    target pred  correct
0        1    1        1
1        0    0        1
2        2    2        1
3        1    1        1
4        1    1        1
5        0    0        1
6        1    1        1
7        2    2        1
8        1    1        1
9        1    1        1
10       2    2        1
11       0    0        1
12       0    0        1
13       0    0        1
14       0    0        1
15       1    1        1
16       2    2        1
17       1    1        1
18       1    1        1
19       2    2        1
20       0    0        1
21       2    2        1
22       0    0        1
23       2    2        1
24       2    2        1
25       2    2        1
26       2    2        1
27       2    2        1
28       0    0        1
29       0    0        1
profile
+AI to AI+

0개의 댓글