2.1.2 Classifier NN
Runs locally on my i7 with NVidia 3050.

TOC
- 1 Output
- 2 PY script
- 3 Training/inference summary
- 4 Code with detailed comments. Great commentary for each line of the code (from GPT). Study this closely to understand the gist of this core first demo.
For details see #607_2.1_core_NNs_.docx.
1 Output
device: cuda
epoch=0 loss=0.753134 accuracy=0.1790
epoch=100 loss=0.136629 accuracy=0.9640
epoch=200 loss=0.055828 accuracy=0.9940
epoch=300 loss=0.030925 accuracy=0.9970
epoch=400 loss=0.019975 accuracy=0.9990
epoch=500 loss=0.013983 accuracy=1.0000
epoch=600 loss=0.010112 accuracy=1.0000
epoch=700 loss=0.007658 accuracy=1.0000
epoch=800 loss=0.005994 accuracy=1.0000
epoch=900 loss=0.004811 accuracy=1.0000

2 PY script
# d2_tiny_classifier.py
import torch
import torch.nn as nn
import matplotlib.pyplot as plt
# -----------------------------
# D2 Tiny Classification Demo
# -----------------------------
device = "cuda" if torch.cuda.is_available() else "cpu"
print("device:", device)
# 1. Create fake 2D data
N = 1000
# random x,y points between -1 and +1
X = torch.rand(N, 2) * 2 - 1
# class rule:
# if point is inside circle -> class 1
# otherwise -> class 0
radius = torch.sqrt(X[:, 0] ** 2 + X[:, 1] ** 2)
Y = (radius < 0.5).long()
X = X.to(device)
Y = Y.to(device)
# 2. Define classifier
model = nn.Sequential(
nn.Linear(2, 16),
nn.ReLU(),
nn.Linear(16, 16),
nn.ReLU(),
nn.Linear(16, 2), # 2 class scores/logits
).to(device)
# 3. Loss + optimizer
loss_fn = nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(model.parameters(), lr=0.01)
# 4. Train
for epoch in range(1000):
logits = model(X)
loss = loss_fn(logits, Y)
optimizer.zero_grad()
loss.backward()
optimizer.step()
if epoch % 100 == 0:
pred_class = logits.argmax(dim=1)
accuracy = (pred_class == Y).float().mean()
print(
f"epoch={epoch} "
f"loss={loss.item():.6f} "
f"accuracy={accuracy.item():.4f}"
)
# 5. Plot decision boundary
model.eval()
grid_size = 200
xx, yy = torch.meshgrid(
torch.linspace(-1, 1, grid_size),
torch.linspace(-1, 1, grid_size),
indexing="ij",
)
grid = torch.stack([xx.reshape(-1), yy.reshape(-1)], dim=1).to(device)
with torch.no_grad():
grid_logits = model(grid)
grid_pred = grid_logits.argmax(dim=1).cpu()
Z = grid_pred.reshape(grid_size, grid_size)
plt.figure(figsize=(6, 6))
plt.contourf(xx, yy, Z, alpha=0.3)
plt.scatter(
X[:, 0].cpu(),
X[:, 1].cpu(),
c=Y.cpu(),
s=8,
)
plt.title("D2 Tiny Classifier: Inside Circle vs Outside Circle")
plt.xlabel("x")
plt.ylabel("y")
plt.axis("equal")
plt.show()
3 Training/inference summary
TRAINING PHASE
xxxxx
GENERATION / INFERENCE PHASE
xxxx
4 Code with detailed comments
xxx
output
Extremely important concept
text2
26.0526