-
Notifications
You must be signed in to change notification settings - Fork 19
/
models.py
71 lines (64 loc) · 2.23 KB
/
models.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
"""A module containing the models described in the SNAIL paper
"""
import math
import torch.nn as nn
import snail
class OmniglotEmbedding(nn.Module):
"""A CNN which transforms a 1x28x28 image to a 64-dimensional vector
"""
def __init__(self):
super(OmniglotEmbedding, self).__init__()
self.cnn1 = nn.Sequential(
nn.Conv2d(1, 64, 3, padding=1),
nn.BatchNorm2d(64),
nn.ReLU(inplace=True),
nn.MaxPool2d(2, ceil_mode=True)
)
self.cnn2 = nn.Sequential(
nn.Conv2d(64, 64, 3, padding=1),
nn.BatchNorm2d(64),
nn.ReLU(inplace=True),
nn.MaxPool2d(2, ceil_mode=True)
)
self.cnn3 = nn.Sequential(
nn.Conv2d(64, 64, 3, padding=1),
nn.BatchNorm2d(64),
nn.ReLU(inplace=True),
nn.MaxPool2d(2, ceil_mode=True)
)
self.cnn4 = nn.Sequential(
nn.Conv2d(64, 64, 3, padding=1),
nn.BatchNorm2d(64),
nn.ReLU(inplace=True),
nn.MaxPool2d(2, ceil_mode=True)
)
self.fc = nn.Linear(256, 64)
def forward(self, minibatch):
out = self.cnn1(minibatch)
out = self.cnn2(out)
out = self.cnn3(out)
out = self.cnn4(out)
return self.fc(out.view(minibatch.size(0), -1))
class OmniglotModel(nn.Module):
"""
Arguments:
N (int): number of classes
K (int): k-shot. i.e. number of examples
"""
def __init__(self, N, K):
super(OmniglotModel, self).__init__()
T = N * K + 1
layer_count = math.ceil(math.log(T)/math.log(2))
self.mod0 = snail.AttentionBlock(65, 64, 32)
self.mod1 = snail.TCBlock(65+32, T, 128)
self.mod2 = snail.AttentionBlock(65+32+128*layer_count, 256, 128)
self.mod3 = snail.TCBlock(65+32+128*layer_count+128, T, 128)
self.mod4 = snail.AttentionBlock(65+32+2*128*layer_count+128, 512, 256)
self.out_layer = nn.Conv1d(65+32+2*128*layer_count+128+256, N, 1)
def forward(self, minibatch):
out = self.mod0(minibatch)
out = self.mod1(out)
out = self.mod2(out)
out = self.mod3(out)
out = self.mod4(out)
return self.out_layer(out)