Classification
A straight line cannot separate every pattern. XOR is the classic case: points are class 1 when exactly one coordinate is positive. No single line splits them, but a small network with a hidden layer and a non-linearity can.
import torch
from torch import nn
x = torch.rand(8, 2) * 2 - 1
y = (x[:, 0] > 0).long()
model = nn.Sequential(
nn.Linear(2, 16), nn.Tanh(),
nn.Linear(16, 2),
)
# one score per class
logits = model(x)
# y holds class numbers 0 and 1
loss = nn.CrossEntropyLoss()(logits, y)
print(loss.item())
print(logits.argmax(dim=1))The network outputs one score, a logit, per class. CrossEntropyLoss turns them into probabilities and compares them with the true class; argmax picks the predicted class. Adam usually trains such models faster than plain SGD.
Your task
Write xor_data(n, seed) returning (x, y): n points uniform in the square from -1 to 1 (use torch.manual_seed(seed) then torch.rand), and labels 1 where exactly one coordinate is positive, else 0 (long). Write train_classifier(x, y, steps=500) returning a trained model, and accuracy(model, x, y) returning the share of correct predictions as a float.
The explanation opens here once every check passes.