-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain.py
92 lines (84 loc) · 3 KB
/
train.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
81
82
83
84
85
86
87
88
89
90
91
92
from typing import Callable, Union
import numpy as np
from tqdm import tqdm
import torch
import torch.nn as nn
def train(model: nn.Module,
train_x: torch.Tensor,
train_y: torch.Tensor,
test_x: torch.Tensor,
test_y: torch.Tensor,
n_epochs: int,
lr: float,
train_x0: torch.Tensor = None,
test_x0: torch.Tensor = None,
batch_size: int = 32,
compute_accuracy: bool = False,
acc_fn: Callable[[torch.Tensor, torch.Tensor], int] = None,
cuda: Union[int, bool] = None,
) -> dict:
if compute_accuracy:
assert acc_fn is not None
if cuda is None or not torch.cuda.is_available():
device = torch.device('cpu')
else:
if cuda is True:
cuda = 0
device = torch.device(f'cuda:{cuda}')
model = model.to(device)
train_x = train_x.to(device)
train_y = train_y.to(device)
test_x = test_x.to(device)
test_y = test_y.to(device)
if train_x0 is not None:
train_x0 = train_x0.to(device)
if test_x0 is not None:
test_x0 = test_x0.to(device)
optim = torch.optim.Adam(model.parameters(), lr=lr)
loss_fn = nn.MSELoss()
history = {"train_loss": [],
"test_loss": [],
"train_acc": [],
"test_acc": []
}
print("Training...")
for epoch in range(n_epochs):
model.train()
batch_train_losses = []
batch_train_accs = []
print(f"Epoch {epoch+1}/{n_epochs}")
for batch in tqdm(range(train_x.shape[0] // batch_size)):
optim.zero_grad()
rand_idxes = torch.randint(train_x.shape[0], (batch_size,))
x = train_x[rand_idxes]
y = train_y[rand_idxes]
if train_x0 is not None:
x0 = train_x0[rand_idxes]
preds = model(x, x0)
else:
preds = model(x)
loss = loss_fn(preds, y)
loss.backward()
optim.step()
loss.detach_()
batch_train_losses.append(loss.item())
if compute_accuracy:
batch_train_accs.append(acc_fn(preds, y))
history["train_loss"].append(np.mean(batch_train_losses))
history["train_acc"].append(np.mean(batch_train_accs))
# test
model.eval()
with torch.no_grad():
if test_x0 is not None:
preds = model(test_x, test_x0)
else:
preds = model(test_x)
loss = loss_fn(preds, test_y)
history["test_loss"].append(loss.item())
if compute_accuracy:
history["test_acc"].append(acc_fn(preds, test_y))
print(f"Train loss: {history['train_loss'][-1]}, Test loss: {history['test_loss'][-1]}")
if compute_accuracy:
print(f"Train acc: {history['train_acc'][-1]}, Test acc: {history['test_acc'][-1]}")
print('Done.')
return history