-
Notifications
You must be signed in to change notification settings - Fork 270
/
nst.py
64 lines (48 loc) · 1.52 KB
/
nst.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
from __future__ import absolute_import
from __future__ import print_function
from __future__ import division
import torch
import torch.nn as nn
import torch.nn.functional as F
# '''
# NST with Polynomial Kernel, where d=2 and c=0
# It can be treated as matching the Gram matrix of two vectorized feature map.
# '''
# class NST(nn.Module):
# def __init__(self):
# super(NST, self).__init__()
# def forward(self, fm_s, fm_t):
# fm_s = fm_s.view(fm_s.size(0), fm_s.size(1), -1)
# fm_s = F.normalize(fm_s, dim=2)
# fm_t = fm_t.view(fm_t.size(0), fm_t.size(1), -1)
# fm_t = F.normalize(fm_t, dim=2)
# gram_s = self.gram_matrix(fm_s)
# gram_t = self.gram_matrix(fm_t)
# loss = F.mse_loss(gram_s, gram_t)
# return loss
# def gram_matrix(self, fm):
# return torch.bmm(fm, fm.transpose(1,2))
'''
NST with Polynomial Kernel, where d=2 and c=0
'''
class NST(nn.Module):
'''
Like What You Like: Knowledge Distill via Neuron Selectivity Transfer
https://arxiv.org/pdf/1707.01219.pdf
'''
def __init__(self):
super(NST, self).__init__()
def forward(self, fm_s, fm_t):
fm_s = fm_s.view(fm_s.size(0), fm_s.size(1), -1)
fm_s = F.normalize(fm_s, dim=2)
fm_t = fm_t.view(fm_t.size(0), fm_t.size(1), -1)
fm_t = F.normalize(fm_t, dim=2)
loss = self.poly_kernel(fm_t, fm_t).mean() \
+ self.poly_kernel(fm_s, fm_s).mean() \
- 2 * self.poly_kernel(fm_s, fm_t).mean()
return loss
def poly_kernel(self, fm1, fm2):
fm1 = fm1.unsqueeze(1)
fm2 = fm2.unsqueeze(2)
out = (fm1 * fm2).sum(-1).pow(2)
return out