-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
47 lines (38 loc) · 1.54 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
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
from torchvision import datasets, transforms
import matplotlib.pyplot as plt
from train import train_large,train_distil
from evaluate import eval
from model import large,distil
def main():
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
batch_size = 100
train_loader = torch.utils.data.DataLoader(
datasets.MNIST('../data', train=True, download=False,
transform=transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.1307,), (0.3081,))
])),
batch_size = batch_size, shuffle=True)
test_loader = torch.utils.data.DataLoader(
datasets.MNIST('../data', train=False, transform=transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.1307,), (0.3081,))
])),
batch_size=batch_size, shuffle=True)
large_model = large().to(device)
optimizer = optim.SGD(large_model.parameters(), lr=0.01, momentum=0.9)
epochs = 20
train_large(large_model,train_loader,optimizer,epochs,device)
eval(large_model,test_loader)
distil_model = distil().to(device)
loss_fn = nn.MSELoss()
distil_weight = 0.7
temp = 20
train_distil(large_model,distil_model,train_loader,optimizer,loss_fn,epochs,temp,distil_weight)
eval(distil_model,test_loader)
if __name__ == '__main__':
main()