forked from laubonghaudoi/CapsNet_guide_PyTorch
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDecoder.py
More file actions
60 lines (48 loc) · 1.82 KB
/
Copy pathDecoder.py
File metadata and controls
60 lines (48 loc) · 1.82 KB
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
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. Then send
it 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(10 * 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, 16, 10]
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 = v_masked.view(batch_size, -1)
assert v_masked.size() == torch.Size([batch_size, 160])
# Forward
v = self.fc1(v_masked)
v = self.fc2(v)
reconstruction = F.sigmoid(self.fc3(v))
assert reconstruction.size() == torch.Size([batch_size, 784])
return reconstruction