-
Notifications
You must be signed in to change notification settings - Fork 0
/
engine.py
73 lines (69 loc) · 2.55 KB
/
engine.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
import torch
import tqdm
from typing import Iterable
def train_one_epoch_kd(student_model: torch.nn.Module,
teacher_model: torch.nn.Module,
criterion: torch.nn.Module, data_loader: Iterable,
optimizer: torch.optim.Optimizer, device: torch.device):
student_model.train()
running_loss = 0.0
total_samples = 0
for samples, targets in tqdm.tqdm(data_loader, total = len(data_loader)):
samples = samples.to(device)
targets = targets.to(device)
student_outputs = student_model(samples)
teacher_outputs = teacher_model(samples)
loss = criterion(student_outputs, teacher_outputs, targets)
optimizer.zero_grad()
loss.backward()
optimizer.step()
running_loss += loss.item()
total_samples += samples.size(0)
return running_loss / total_samples
def eval_kd(student_model: torch.nn.Module,
criterion: torch.nn.Module,
data_loader: Iterable,
device: torch.device):
student_model.eval()
total_loss = 0.0
total_samples = 0
for samples, labels in tqdm.tqdm(data_loader, total = len(data_loader)):
samples = samples.to(device)
labels = labels.to(device)
outputs = student_model(samples)
loss = criterion(outputs, labels)
total_loss += loss.item()
total_samples += samples.size(0)
return total_loss/total_samples
def train_one_epoch(model,
dataloader,
criterion,
optimizer,
device):
model.train()
running_loss = 0.0
total_samples = 0
for samples, targets in tqdm.tqdm(dataloader, total = len(dataloader)):
samples = samples.to(device)
targets = targets.to(device)
optimizer.zero_grad()
outputs = model(samples)
loss = criterion(outputs, targets)
running_loss += loss.item()
loss.backward()
optimizer.step()
total_samples += samples.size(0)
return running_loss/total_samples
def eval(model, dataloader, criterion, device):
model.eval()
total_loss = 0
total_samples = 0
with torch.no_grad():
for samples, targets in tqdm.tqdm(dataloader, total = len(dataloader)):
samples = samples.to(device)
targets = targets.to(device)
outputs = model(samples)
loss = criterion(outputs, targets)
total_loss += loss.item()
total_samples += samples.size(0)
return total_loss / total_samples