-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
executable file
·81 lines (51 loc) · 1.43 KB
/
main.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import argparse
import torch
import mlflow
import mlflow.pytorch
# Make this script parametrizable
parser = argparse.ArgumentParser()
parser.add_argument('--lr', type=float, default=1e-3)
parser.add_argument('--hidden', type=int, default=100)
args = parser.parse_args()
# Generate some fake data
n_data = 64
input_dim = 1000
output_dim = 10
x = torch.randn(n_data, input_dim)
y = torch.randn(n_data, output_dim)
# Define a model
hidden_dim = args.hidden
model = torch.nn.Sequential(
torch.nn.Linear(input_dim, hidden_dim),
torch.nn.ReLU(),
torch.nn.Linear(hidden_dim, output_dim)
)
# Define some loss functions
mse = torch.nn.MSELoss()
l1 = torch.nn.L1Loss()
# Define an optimizer
lr = args.lr
opt = torch.optim.SGD(model.parameters(), lr = lr)
# Run a training loop
# Experiments group related runs in MLFlow.
mlflow.set_experiment('demo')
with mlflow.start_run():
# Log some parameters valid for the whole run
mlflow.log_params({
'lr': lr,
'hidden_dim': hidden_dim
})
for epoch in range(100):
predictions = model(x)
mse_loss = mse(predictions, y)
l1_loss = l1(predictions, y)
# Log metrics for each epoch of the run
mlflow.log_metrics({
'mse_loss': mse_loss.item(),
'l1_loss': l1_loss.item()
}, step=epoch)
opt.zero_grad()
mse_loss.backward() # choose mse as optimization target
opt.step()
# save the model artefact
mlflow.pytorch.log_model(model, 'model')