-
Notifications
You must be signed in to change notification settings - Fork 0
/
cifar_shallow.py
55 lines (47 loc) · 1.62 KB
/
cifar_shallow.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
import torch
import torch.nn as nn
F = nn.functional
__all__ = ['cifar10_shallow', 'cifar100_shallow']
class AlexNet(nn.Module):
def __init__(self, num_classes=10):
super(AlexNet, self).__init__()
self.features = nn.Sequential(
nn.Conv2d(3, 64, kernel_size=5, padding=2,
bias=False),
nn.MaxPool2d(kernel_size=3, stride=2),
nn.BatchNorm2d(64),
nn.ReLU(inplace=True),
nn.Conv2d(64, 64, kernel_size=5, padding=2, bias=False),
nn.MaxPool2d(kernel_size=3, stride=2),
nn.BatchNorm2d(64),
nn.ReLU(inplace=True),
)
self.classifier = nn.Sequential(
nn.Linear(64 * 7 * 7, 384, bias=False),
nn.BatchNorm1d(384),
nn.ReLU(inplace=True),
nn.Dropout(0.5),
nn.Linear(384, 192, bias=False),
nn.BatchNorm1d(192),
nn.ReLU(inplace=True),
nn.Dropout(0.5),
nn.Linear(192, num_classes)
)
self.regime = {
0: {'optimizer': 'SGD', 'lr': 1e-3,
'weight_decay': 5e-4, 'momentum': 0.9},
60: {'lr': 1e-2},
120: {'lr': 1e-3},
180: {'lr': 1e-4}
}
def forward(self, x):
x = self.features(x)
x = x.view(-1, 64 * 7 * 7)
x = self.classifier(x)
return F.log_softmax(x)
def cifar10_shallow(**kwargs):
num_classes = getattr(kwargs, 'num_classes', 10)
return AlexNet(num_classes)
def cifar100_shallow(**kwargs):
num_classes = getattr(kwargs, 'num_classes', 100)
return AlexNet(num_classes)