-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathxor_classification.py
More file actions
85 lines (61 loc) · 1.83 KB
/
Copy pathxor_classification.py
File metadata and controls
85 lines (61 loc) · 1.83 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
75
76
77
78
79
80
81
82
83
84
85
import torch
import torch.nn as nn
import torch.optim as optim
# xor dataset (classification)
x = torch.tensor([[0.0, 0.0],
[0.0, 1.0],
[1.0, 0.0],
[1.0, 1.0]], dtype=torch.float32)
# class labels as integers
y = torch.tensor([0, 1, 1, 0], dtype=torch.long)
# Model
class XORNet(nn.Module):
def __init__(self):
super(XORNet, self).__init__()
self.fc1 = nn.Linear(2, 50)
self.fc2 = nn.Linear(50, 25)
self.fc3 = nn.Linear(25, 10)
self.fc4 = nn.Linear(10, 2)
self.relu = nn.ReLU()
# weight reused in forward pass
def forward(self, x):
x = self.fc1(x)
x = self.relu(x)
x = self.fc2(x)
x = self.relu(x)
x = self.fc3(x)
x = self.relu(x)
x = self.fc4(x)
return x
# create an instance of the network
model = XORNet()
# loss function and
criterion = nn.CrossEntropyLoss()
# optimizer
optimizer = optim.Adam(model.parameters(), lr=0.01)
# training loop
epochs = 50
for epoch in range(epochs):
optimizer.zero_grad()
# x is input
outputs = model(x)
loss = criterion(outputs, y)
loss.backward()
optimizer.step()
if (epoch+1) % 10 == 0:
print(f'Epoch [{epoch+1}/{epochs}], Loss: {loss.item():.4f}')
# testing the model
test_inputs = torch.tensor([[1, 0],
[0, 1],
[1, 1],
[0, 0]])
test_inputs = test_inputs.float()
test_targets = torch.tensor([1, 1, 0, 0])
# with test model after training
with torch.no_grad():
outputs = model(test_inputs)
# apply softmax to get probabilities
probs = torch.softmax(outputs, dim=1)
_, predicted = torch.max(probs, dim=1)
print("Predicted labels:", predicted.tolist())
print("True labels:", test_targets.tolist())