<jupyter_start><jupyter_text>얕은 신경망(Shallow Neural Network)매우 심플한 얕은 신경망을 설계한 후 임의의 숫자로 예측하기<jupyter_code>import torch
import numpy as np
import matplotlib.pyplot as plt

%matplotlib inline<jupyter_output><empty_output><jupyter_text>데이터 생성임의의 데이터 생성<jupyter_code>np.random.seed(10)

center_points = np.random.uniform(-8, 8, (10,2)) # 랜덤한 중앙값 10개 생성

inputs = []
labels = []
for i, cp in enumerate(center_points):
    for _ in range(1000):
        inputs.append(cp + np.random.randn(*cp.shape)) # 중앙값을 기준으로 임의의 점 100개 생성
        labels.append(i) # 각 레이블
        
inputs = np.stack(inputs, axis = 0)
labels = np.stack(labels, axis = 0)
print(inputs.shape, labels.shape)

# plot
plt.figure()
for i in range(10):
    mask = labels == i
    plt.scatter(inputs[mask, 0], inputs[mask, 1])<jupyter_output>(10000, 2) (10000,)<jupyter_text>데이터셋 생성<jupyter_code>from torch.utils.data import Dataset
import torchvision.transforms as transforms
class MyDataset(Dataset):
    
    def __init__(self, transform=None):
        self.inputs = inputs
        self.lables = labels
        self.transform = transform
        
    def __len__(self):
        return len(self.inputs)
    
    def __getitem__(self, idx):
        input_data = inputs[idx]
        label_data = labels[idx]
        
        if self.transform:
            input_data = self.transform(input_data)
            
        return input_data, label_data
    

transform = transforms.Compose([transforms.ToTensor()])

mydataset = MyDataset()

dataloader = torch.utils.data.DataLoader(mydataset, batch_size=16, shuffle = True)
next(iter(dataloader))<jupyter_output><empty_output><jupyter_text>Model 생성<jupyter_code>import torch.nn as nn
import torch.nn.functional as F
class MyModel(nn.Module):
    
    def __init__(self, input_size, output_size):
        super(MyModel, self).__init__()
        self.fc1 = nn.Linear(input_size, 128)
        self.fc2 = nn.Linear(128, 10)
        
        
    def forward(self, x):
        out = F.relu(self.fc1(x))
        out = self.fc2(out)
        
        return F.log_softmax(out, dim=1)
    
model = MyModel(2, 10)<jupyter_output><empty_output><jupyter_text>loss함수와 최적화<jupyter_code>loss_func = nn.CrossEntropyLoss()
optimizer = optim.Adam(model.parameters())<jupyter_output><empty_output><jupyter_text>학습<jupyter_code>EPOCH = 100
loss_list = []

model.train()
for e in range(EPOCH):
    losses = 0
    for i, data in enumerate(dataloader):
        input_, label_ = data
        
        optimizer.zero_grad()
        output_ = model(input_.float())
        loss = loss_func(output_.float(), label_.long())
        loss.backward()
        optimizer.step()
        
        losses += loss
        
    loss_list.append(losses/len(input_))
    
    print('EPOCH : [{}/{}] LOSS : {}'.format(e+1, EPOCH, losses))<jupyter_output>EPOCH : [1/100] LOSS : 288.7618713378906
EPOCH : [2/100] LOSS : 139.5413360595703
EPOCH : [3/100] LOSS : 112.19017028808594
EPOCH : [4/100] LOSS : 103.91460418701172
EPOCH : [5/100] LOSS : 99.71634674072266
EPOCH : [6/100] LOSS : 96.42254638671875
EPOCH : [7/100] LOSS : 93.48419952392578
EPOCH : [8/100] LOSS : 92.83816528320312
EPOCH : [9/100] LOSS : 92.42862701416016
EPOCH : [10/100] LOSS : 91.25983428955078
EPOCH : [11/100] LOSS : 91.79927062988281
EPOCH : [12/100] LOSS : 91.1563720703125
EPOCH : [13/100] LOSS : 90.41301727294922
EPOCH : [14/100] LOSS : 90.98528289794922
EPOCH : [15/100] LOSS : 89.96204376220703
EPOCH : [16/100] LOSS : 89.7914047241211
EPOCH : [17/100] LOSS : 89.9959945678711
EPOCH : [18/100] LOSS : 89.38280487060547
EPOCH : [19/100] LOSS : 89.2765121459961
EPOCH : [20/100] LOSS : 89.29798889160156
EPOCH : [21/100] LOSS : 89.28173828125
EPOCH : [22/100] LOSS : 90.70471954345703
EPOCH : [23/100] LOSS : 89.50208282470703
EPOCH : [24/100] LOSS : 89.66610717773438
EPOCH [...]<jupyter_text>정확도(Accuracy)<jupyter_code>model.eval()
predict = model(torch.Tensor(inputs)).argmax(axis=1).numpy()

correct = predict == labels
accuracy = sum(correct) / len(labels) * 100

print('The Accuracy is : %.2f%%' %accuracy)<jupyter_output>The Accuracy is : 94.18%<jupyter_text>Before & After<jupyter_code>plt.figure(figsize = (10,10))
plt.subplot(2,1,1)
for i in range(10):
    mask = labels == i
    plt.scatter(inputs[mask, 0], inputs[mask, 1])
plt.title('Before')

plt.subplot(2,1,2)
for j in range(10):
    mask = predict == j
    plt.scatter(inputs[mask, 0], inputs[mask, 1])
plt.title('After')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13535353535353536, 'fraction_numerical': 0.1286195286195286, 'mean_word_length': 3.6128364389233956, 'pattern_counts': {'":': 0, '<': 30, '<?xml version=': 0, '>': 30, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '398134', 'license': 'MIT', 'n_tokens_mistral': 2194, 'n_tokens_neox': 1862, 'n_words': 341, 'path': '1. Beginner/Pytorch4_Shallow_NN.ipynb', 'repo_name': 'gjustin40/Pytorch', 'size': '4455'}