<jupyter_start><jupyter_text>9. Neural Networks with Sphere example<jupyter_code>import numpy as np

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

from torch.utils.data import TensorDataset, DataLoader
import matplotlib.pyplot as plt
%matplotlib inline<jupyter_output><empty_output><jupyter_text>9.1 Generate Data<jupyter_code>def make_datasets(size, dim) :
    x = torch.randn(size, dim)
    y = torch.randn(size, dim)

    x = x / x.norm(dim=1).view(size, 1)
    y = 4 * y / y.norm(dim=1).view(size, 1)
    
    label_x = torch.ones(size)
    label_y = torch.zeros(size)

    data = torch.cat([x, y])
    label = torch.cat([label_x, label_y])
    
    return TensorDataset(data, label)
train_data = make_datasets(500, 2)
test_data = make_datasets(100, 2)

batch_size = 100

train_loader = DataLoader(batch_size=batch_size, dataset=train_data, shuffle=True)
test_loader = DataLoader(batch_size=batch_size, dataset=test_data, shuffle=False)

# dataset (Dataset) – dataset from which to load the data.
# batch_size (int, optional) – how many samples per batch to load (default: 1).
# shuffle (bool, optional) – set to True to have the data reshuffled at every epoch (default: False).
X = train_data.tensors[0]
Y = train_data.tensors[1]
fig = plt.figure(figsize = (5, 4))
plt.scatter(X[:, 0], X[:, 1], c = Y)
plt.colorbar()
plt.show()<jupyter_output><empty_output><jupyter_text>9.2 Define Model<jupyter_code>model = nn.Sequential(
    nn.Linear(2, 1000),
    nn.ReLU(),
    nn.Linear(1000, 1000),
    nn.ReLU(),
    nn.Linear(1000, 1),
    nn.Sigmoid()
)<jupyter_output><empty_output><jupyter_text>9.3 Train Model<jupyter_code>loss = nn.BCELoss()
optimizer = optim.SGD(model.parameters(), lr=0.1)
num_epochs = 10
model.train()
    
for i, (batch_data, batch_labels) in enumerate(train_loader):

    X = batch_data
    Y = batch_labels

    pre = model(X)
    cost = loss(pre.squeeze(), Y)

    optimizer.zero_grad()
    cost.backward()
    optimizer.step()
    
    print('lter [%d/%d], Loss: %.4f'%(i+1, len(train_loader), cost.item()))
    
print("Learning Finished!")<jupyter_output>lter [1/10], Loss: 0.6726
lter [2/10], Loss: 0.6737
lter [3/10], Loss: 0.5254
lter [4/10], Loss: 0.4963
lter [5/10], Loss: 0.3554
lter [6/10], Loss: 0.3695
lter [7/10], Loss: 0.3047
lter [8/10], Loss: 0.2752
lter [9/10], Loss: 0.2148
lter [10/10], Loss: 0.1778
Learning Finished!<jupyter_text>9.3 Test Model<jupyter_code>correct = 0
total = 0

for data, labels in test_loader:
    
    outputs = model(data)
    
    predicted = outputs.squeeze().data > 0.5
    total += data.shape[0]
    correct += (predicted == labels).sum()
    
print('Accuracy of test images: %f %%' % (100 * float(correct) / total))
print('Misclassified : %d %%' % (total - correct))<jupyter_output>Accuracy of test images: 100.000000 %
Misclassified : 0 %<jupyter_text>9.4 Decision Boundary<jupyter_code>grid_size = 500
x = torch.linspace(-5, 5, grid_size)
y = torch.linspace(-5, 5, grid_size)
xv, yv = torch.meshgrid(x, y)
xv.shape, yv.shape
xv = xv.reshape(-1, 1)
yv = yv.reshape(-1, 1)
torch.cat([xv, yv], dim=1)
z = model(torch.cat([xv, yv], dim=1)) > 0.5
x = xv.data.numpy().reshape(grid_size, grid_size)
y = yv.data.numpy().reshape(grid_size, grid_size)
z = z.data.numpy().reshape(grid_size, grid_size)

fig = plt.figure(figsize = (5, 4))
plt.scatter(X[:, 0], X[:, 1], c = Y)
plt.contourf(x, y, z, alpha=0.3)
plt.colorbar()
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15931514799767846, 'fraction_numerical': 0.05165409170052235, 'mean_word_length': 4.2949308755760365, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 25, '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': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '15184', 'license': 'MIT', 'n_tokens_mistral': 1508, 'n_tokens_neox': 1387, 'n_words': 318, 'path': 'Week3/09. Neural Networks with Sphere example_seungju.ipynb', 'repo_name': 'Seungju182/pytorch-basic', 'size': '3446'}