-
Notifications
You must be signed in to change notification settings - Fork 1
/
fff_experiment_mnist.py
65 lines (52 loc) · 2.29 KB
/
fff_experiment_mnist.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
import typer
import mlflow
from tqdm import trange
from fff_trainer import Net, train, test, DEVICE
from torchvision.datasets import MNIST
from torch.utils.data import DataLoader
import torchvision.transforms as transforms
def load_data():
"""Load MNIST (training and test set)."""
transform = transforms.Compose(
[
transforms.ToTensor(),
transforms.Normalize((0.1307,), (0.3081,)),
]
)
trainset = MNIST("./data", train=True, download=True, transform=transform)
testset = MNIST("./data", train=False, download=True, transform=transform)
# Select class to keep
trainloader = DataLoader(trainset, batch_size=1024, shuffle=True)
testloader = DataLoader(testset, batch_size=1024)
num_examples = {"trainset" : len(trainset), "testset" : len(testset)}
return trainloader, testloader, num_examples
def main(leaf_width: int, depth: int, epochs: int, norm_weight: float):
trainloader, testloader, _ = load_data()
net = Net(784, leaf_width, 10, depth, 0, 0).to(DEVICE)
with mlflow.start_run():
mlflow.log_param("leaf_width", leaf_width)
mlflow.log_param("depth", depth)
mlflow.log_param("epochs", epochs)
mlflow.log_param("norm_weight", norm_weight)
mlflow.log_param("hardened", net.fff.train_hardened)
# Train the net and log on mlflow
for i in trange(epochs):
train(net, trainloader, 1, norm_weight=norm_weight)
train_loss, train_acc = test(net, trainloader)
test_loss, test_acc = test(net, testloader)
mlflow.log_metric("train_accuracy", train_acc, step=i)
mlflow.log_metric("train_loss", train_loss, step=i)
mlflow.log_metric("test_accuracy", test_acc, step=i)
mlflow.log_metric("test_loss", test_loss, step=i)
# Evaluation
net.eval()
train_loss, train_acc = test(net, trainloader)
test_loss, test_acc = test(net, testloader)
mlflow.log_metric("eval_train_accuracy", train_acc)
mlflow.log_metric("eval_train_loss", train_loss)
mlflow.log_metric("eval_test_accuracy", test_acc)
mlflow.log_metric("eval_test_loss", test_loss)
# Log model
mlflow.pytorch.log_model(net, "model")
if __name__ == "__main__":
typer.run(main)