-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpy_torch.py
More file actions
74 lines (61 loc) · 2.08 KB
/
py_torch.py
File metadata and controls
74 lines (61 loc) · 2.08 KB
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import torch
from torch import nn
from torch.utils.data import DataLoader, TensorDataset
DEVICE = "cpu"
BATCH_SIZE = 64
import numpy as np
# seed 7 leads to dead neurons
torch.manual_seed(6)
class NeuralNetwork(nn.Module):
def __init__(self):
super().__init__()
self.linear_relu_stack = nn.Sequential(
nn.Linear(2, 16),
nn.ReLU(),
nn.Linear(16, 8),
nn.ReLU(),
nn.Linear(8, 4),
nn.ReLU(),
nn.Linear(4, 2),
nn.ReLU(),
nn.Linear(2, 1),
)
def forward(self, x):
self.intermediate_outputs = [] # Reset the list for each forward pass
for layer in self.linear_relu_stack:
x = layer(x)
self.intermediate_outputs.append(x.clone())
return x
def neural_net(X, Y, iterations):
X = torch.tensor(X, dtype=torch.float32)
Y = torch.tensor(Y, dtype=torch.float32)
dataset = TensorDataset(X, Y)
data_loader = DataLoader(dataset, batch_size=BATCH_SIZE)
model = NeuralNetwork().to(DEVICE)
test(model, X, Y)
train(model, data_loader, iterations)
test(model, X, Y)
def test(model, X, Y):
model.eval()
with torch.no_grad():
logits = model(X)
probabilities = torch.sigmoid(logits)
predicted_labels = (probabilities >= 0.5).float()
accuracy = (predicted_labels == Y.view(-1, 1)).sum().item() / len(Y)
print(f"Accuracy: {accuracy * 100:.2f}%")
def train(model, data_loader, iterations):
loss_fn = nn.BCEWithLogitsLoss()
optimizer = torch.optim.Adam(model.parameters(), lr=0.01)
model.train() # Sets the model into training mode
for iteration in range(iterations):
for X, y in data_loader:
X, y = X.to(DEVICE), y.to(DEVICE)
# Compute prediction error
pred = model(X)
loss = loss_fn(pred, y)
# Backpropagation
loss.backward()
optimizer.step()
optimizer.zero_grad()
loss = loss.item()
print(f"{iteration}/{iterations}: loss: {loss:>7f}")