-
Notifications
You must be signed in to change notification settings - Fork 1
/
ff_experiment_mnist.py
54 lines (42 loc) · 1.75 KB
/
ff_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
import typer
import mlflow
from tqdm import trange
from fff_trainer import FF, 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(layer_width: int, epochs: int):
trainloader, testloader, _ = load_data()
net = FF(784, layer_width, 10).to(DEVICE)
with mlflow.start_run(experiment_id="17"):
mlflow.log_param("leaf_width", layer_width)
mlflow.log_param("depth", 1)
mlflow.log_param("epochs", epochs)
# Train the net and log on mlflow
for i in trange(epochs):
train(net, trainloader, 1)
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)
# Log model
mlflow.pytorch.log_model(net, "model")
if __name__ == "__main__":
typer.run(main)