-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample1.py
82 lines (60 loc) · 2.04 KB
/
example1.py
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
import numpy as np
import torch
import torch.nn as nn
import torch.optim as optim
from tqdm import tqdm
BITS = 100
NUMBERS = 1000
def int_to_binary(n, bits=BITS):
return np.array([int(bit) for bit in np.binary_repr(n, width=bits)])
num_array = np.arange(1, NUMBERS + 1)
X = np.array([int_to_binary(i) for i in num_array], dtype=np.float32)
Y = np.array([1 if i % 2 == 0 else 0 for i in num_array], dtype=np.float32).reshape(
-1, 1
)
X_tensor = torch.tensor(X, dtype=torch.float32)
Y_tensor = torch.tensor(Y, dtype=torch.float32)
class BinaryClassifier(nn.Module):
def __init__(self, input_size=BITS):
super(BinaryClassifier, self).__init__()
self.fc1 = nn.Linear(input_size, 8)
self.relu = nn.ReLU()
self.fc2 = nn.Linear(8, 1)
self.sigmoid = nn.Sigmoid()
def forward(self, x):
x = self.fc1(x)
x = self.relu(x)
x = self.fc2(x)
x = self.sigmoid(x)
return x
criterion = nn.BCELoss()
model = BinaryClassifier()
optimizer = optim.Adam(model.parameters(), lr=0.001)
print("Training:")
with tqdm(range(NUMBERS), desc="", position=0, leave=True) as progress_bar:
for epoch in range(NUMBERS):
model.train()
optimizer.zero_grad()
outputs = model(X_tensor)
loss = criterion(outputs, Y_tensor)
loss.backward()
optimizer.step()
preds = (outputs.detach().cpu().numpy() > 0.5).astype(int).flatten()
accuracy = (preds == Y.flatten()).mean() * 100
progress_bar.update()
progress_bar.set_description(
f"loss: {loss.item():6.2f} accuracy: {accuracy:5.2f}%"
)
def test_model(number):
model.eval()
with torch.no_grad():
number_tensor = torch.tensor(
np.array(int_to_binary(number)), dtype=torch.float32
)
prediction = model(number_tensor).item()
pred = "even" if prediction > 0.5 else "odd"
print(f"Number {number} -> {pred}")
test_numbers = [17, 44, 62, 503, 12321]
print("Prediction:")
for n in test_numbers:
test_model(n)