-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathDecoder.py
61 lines (49 loc) · 1.85 KB
/
Decoder.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
import torch
import torch.nn as nn
import torch.nn.functional as F
class Decoder(nn.Module):
'''
The decoder network consists of 3 fully connected layers. For each
[10, 16] output, we mask out the incorrect predictions, and send
the [16,] vector to the decoder network to reconstruct a [784,] size
image.
Reference: Section 4.1, Fig. 2
'''
def __init__(self, opt):
'''
The decoder network consists of 3 fully connected layers, with
512, 1024, 784 neurons each.
'''
super(Decoder, self).__init__()
self.opt = opt
self.fc1 = nn.Linear(16, 512)
self.fc2 = nn.Linear(512, 1024)
self.fc3 = nn.Linear(1024, 784)
def forward(self, v, target):
'''
Args:
`v`: [batch_size, 10, 16]
`target`: [batch_size, 10]
Return:
`reconstruction`: [batch_size, 784]
We send the outputs of the `DigitCaps` layer, which is a
[batch_size, 10, 16] size tensor into the decoder network, and
reconstruct a [batch_size, 784] size tensor representing the image.
'''
batch_size = target.size(0)
target = target.type(torch.FloatTensor)
# mask: [batch_size, 10, 16]
mask = torch.stack([target for i in range(16)], dim=2)
assert mask.size() == torch.Size([batch_size, 10, 16])
if self.opt.use_cuda & torch.cuda.is_available():
mask = mask.cuda()
# v: [bath_size, 10, 16]
v_masked = mask * v
v_masked = torch.sum(v_masked, dim=1)
assert v_masked.size() == torch.Size([batch_size, 16])
# Forward
v = F.relu(self.fc1(v_masked))
v = F.relu(self.fc2(v))
reconstruction = torch.sigmoid(self.fc3(v))
assert reconstruction.size() == torch.Size([batch_size, 784])
return reconstruction