-
Notifications
You must be signed in to change notification settings - Fork 12
/
train.py
32 lines (22 loc) · 913 Bytes
/
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
import torch.nn.functional as F
def train_epoch(model, optimizer, train_loader, device, epoch, log_interval):
model.train()
losses = []
for batch_idx, (data, target) in enumerate(train_loader):
data = data.to(device)
target = target.to(device)
output = model(data)
loss = F.nll_loss(output.squeeze(), target)
optimizer.zero_grad()
loss.backward()
optimizer.step()
if batch_idx % log_interval == 0:
print(f"Train Epoch: {epoch}\tLoss: {loss.item():.4f}")
losses.append(loss.item())
return losses
def train(n_epoch, model, optimizer, train_loader, device, log_interval):
print(f"--- Start train {n_epoch} epoches")
for epoch in range(n_epoch):
print(f"--- Start epoch {epoch+1}")
train_epoch(model, optimizer, train_loader, device, epoch, log_interval)
print("--- Done train")