PyTorch: MNIST¶
Small CNN on MNIST. Uses every common line: seed, model, workload, phases, per-epoch results, claim.
| Framework | PyTorch |
| Task | Image classification |
Install¶
Plus the K-Veritas CLI.
train.py¶
Only the KV lines are new.
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.utils.data import DataLoader
from torchvision import datasets, transforms
SEED = 42
torch.manual_seed(SEED)
print(f"KV seed {SEED}")
device = "cuda" if torch.cuda.is_available() else "cpu"
print("KV phase data")
tfm = transforms.Compose([transforms.ToTensor(), transforms.Normalize((0.1307,), (0.3081,))])
train_ds = datasets.MNIST("./data", train=True, download=True, transform=tfm)
test_ds = datasets.MNIST("./data", train=False, download=True, transform=tfm)
train_loader = DataLoader(train_ds, batch_size=128, shuffle=True)
test_loader = DataLoader(test_ds, batch_size=512)
class Net(nn.Module):
def __init__(self):
super().__init__()
self.conv1 = nn.Conv2d(1, 32, 3, padding=1)
self.conv2 = nn.Conv2d(32, 64, 3, padding=1)
self.fc1 = nn.Linear(64 * 7 * 7, 128)
self.fc2 = nn.Linear(128, 10)
def forward(self, x):
x = F.max_pool2d(F.relu(self.conv1(x)), 2)
x = F.max_pool2d(F.relu(self.conv2(x)), 2)
x = x.flatten(1)
x = F.relu(self.fc1(x))
return self.fc2(x)
model = Net().to(device)
n_params = sum(p.numel() for p in model.parameters())
print(f"KV model params={n_params} arch=cnn precision=fp32")
print(f"KV workload dataset_size={len(train_ds)} epochs=3 batch_size=128")
opt = torch.optim.Adam(model.parameters(), lr=1e-3)
def accuracy(loader):
model.eval()
correct = total = 0
with torch.no_grad():
for x, y in loader:
x, y = x.to(device), y.to(device)
correct += (model(x).argmax(1) == y).sum().item()
total += y.size(0)
return correct / total
print("KV phase train")
for epoch in range(1, 4):
model.train()
running = 0.0
for x, y in train_loader:
x, y = x.to(device), y.to(device)
opt.zero_grad()
loss = F.cross_entropy(model(x), y)
loss.backward()
opt.step()
running += loss.item() * y.size(0)
train_loss = running / len(train_ds)
test_acc = accuracy(test_loader)
print(f"KV train_loss={train_loss:.4f} step={epoch}")
print(f"KV test_accuracy={test_acc:.4f} step={epoch}")
print(f"epoch {epoch}/3 train_loss={train_loss:.4f} test_acc={test_acc:.4f}", flush=True)
print("KV phase evaluate")
final_acc = accuracy(test_loader)
print(f"KV claim test_accuracy={final_acc:.4f}")
print(f"final test_accuracy={final_acc:.4f}", flush=True)
Run it¶
Result¶
3 epochs, one GPU: about 99.0% test accuracy. Report: train loss and test accuracy per epoch, 3 phases, the seed, one claim.