summaryrefslogtreecommitdiff
path: root/torch/legacy/nn/WeightedEuclidean.py
blob: 38256f113772dbe2d7a1e97d04621ee99f401f96 (plain)
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
import math
import torch
from .Module import Module

class WeightedEuclidean(Module):

    def __init__(self, inputSize, outputSize):
        super(WeightedEuclidean, self).__init__()

        self.weight = torch.Tensor(inputSize, outputSize)
        self.gradWeight = torch.Tensor(inputSize, outputSize)

        # each template (output dim) has its own diagonal covariance matrix
        self.diagCov = torch.Tensor(inputSize, outputSize)
        self.gradDiagCov = torch.Tensor(inputSize, outputSize)

        self.reset()
        self._diagCov = self.output.new()

        # TODO: confirm
        self.fastBackward = False

        self._input = None
        self._weight = None
        self._expand = None
        self._expand2 = None
        self._expand3 = None
        self._repeat = None
        self._repeat2 = None
        self._repeat3 = None
        self._div = None
        self._output = None
        self._expand4 = None
        self._gradOutput = None
        self._sum = None

    def reset(self, stdv=None):
        if stdv is not None:
           stdv = stdv * math.sqrt(3)
        else:
           stdv = 1. / math.sqrt(self.weight.size(1))

        self.weight.uniform_(-stdv, stdv)
        self.diagCov.fill_(1)

    def _view(self, res, src, *args):
        if src.is_contiguous():
           res.set_(src.view(*args))
        else:
           res.set_(src.contiguous().view(*args))

    def updateOutput(self, input):
        # lazy-initialize
        if self._diagCov is None:
              self._diagCov = self.output.new()

        if self._input is None:
              self._input = input.new()
        if self._weight is None:
              self._weight = self.weight.new()
        if self._expand is None:
              self._expand = self.output.new()
        if self._expand2 is None:
              self._expand2 = self.output.new()
        if self._expand3 is None:
              self._expand3 = self.output.new()
        if self._repeat is None:
              self._repeat = self.output.new()
        if self._repeat2 is None:
              self._repeat2 = self.output.new()
        if self._repeat3 is None:
              self._repeat3 = self.output.new()

        inputSize, outputSize = self.weight.size(0), self.weight.size(1)

        # y_j = || c_j * (w_j - x) ||
        if input.dim() == 1:
            self._view(self._input, input, inputSize, 1)
            self._expand.expand_as(self._input, self.weight)
            self._repeat.resize_as_(self._expand).copy_(self._expand)
            self._repeat.add_(-1, self.weight)
            self._repeat.mul_(self.diagCov)
            torch.norm(self._repeat, 2, 0, out=self.output)
            self.output.resize_(outputSize)
        elif input.dim() == 2:
            batchSize = input.size(0)

            self._view(self._input, input, batchSize, inputSize, 1)
            self._expand = self._input.expand(batchSize, inputSize, outputSize)
            # make the expanded tensor contiguous (requires lots of memory)
            self._repeat.resize_as_(self._expand).copy_(self._expand)

            self._weight = self.weight.view(1, inputSize, outputSize)
            self._expand2 = self._weight.expand_as(self._repeat)

            self._diagCov = self.diagCov.view(1, inputSize, outputSize)
            self._expand3 = self._diagCov.expand_as(self._repeat)
            if input.type() == 'torch.cuda.FloatTensor':
                # TODO: this can be fixed with a custom allocator
                # requires lots of memory, but minimizes cudaMallocs and loops
                self._repeat2.resize_as_(self._expand2).copy_(self._expand2)
                self._repeat.add_(-1, self._repeat2)
                self._repeat3.resize_as_(self._expand3).copy_(self._expand3)
                self._repeat.mul_(self._repeat3)
            else:
                self._repeat.add_(-1, self._expand2)
                self._repeat.mul_(self._expand3)


            torch.norm(self._repeat, 2, 1, out=self.output)
            self.output.resize_(batchSize, outputSize)
        else:
           raise RuntimeError("1D or 2D input expected")

        return self.output

    def updateGradInput(self, input, gradOutput):
        if self.gradInput is None:
           return

        if self._div is None:
              self._div = input.new()
        if self._output is None:
              self._output = self.output.new()
        if self._expand4 is None:
              self._expand4 = input.new()
        if self._gradOutput is None:
              self._gradOutput = input.new()

        if not self.fastBackward:
           self.updateOutput(input)

        inputSize, outputSize = self.weight.size(0), self.weight.size(1)

        """
        dy_j   -2 * c_j * c_j * (w_j - x)   c_j * c_j * (x - w_j)
        ---- = -------------------------- = ---------------------
         dx     2 || c_j * (w_j - x) ||              y_j
        """

        # to prevent div by zero (NaN) bugs
        self._output.resize_as_(self.output).copy_(self.output).add_(1e-7)
        self._view(self._gradOutput, gradOutput, gradOutput.size())
        torch.div(gradOutput, self._output, out=self._div)
        if input.dim() == 1:
            self._div.resize_(1, outputSize)
            self._expand4 = self._div.expand_as(self.weight)

            if torch.type(input) == 'torch.cuda.FloatTensor':
                self._repeat2.resize_as_(self._expand4).copy_(self._expand4)
                self._repeat2.mul_(self._repeat)
            else:
                self._repeat2.mul_(self._repeat, self._expand4)

            self._repeat2.mul_(self.diagCov)
            torch.sum(self._repeat2, 1, out=self.gradInput)
            self.gradInput.resize_as_(input)
        elif input.dim() == 2:
            batchSize = input.size(0)

            self._div.resize_(batchSize, 1, outputSize)
            self._expand4 = self._div.expand(batchSize, inputSize, outputSize)

            if input.type() == 'torch.cuda.FloatTensor':
                self._repeat2.resize_as_(self._expand4).copy_(self._expand4)
                self._repeat2.mul_(self._repeat)
                self._repeat2.mul_(self._repeat3)
            else:
                torch.mul(self._repeat, self._expand4, out=self._repeat2)
                self._repeat2.mul_(self._expand3)


            torch.sum(self._repeat2, 2, out=self.gradInput)
            self.gradInput.resize_as_(input)
        else:
            raise RuntimeError("1D or 2D input expected")

        return self.gradInput

    def accGradParameters(self, input, gradOutput, scale=1):
        inputSize, outputSize = self.weight.size(0), self.weight.size(1)

        """
        dy_j   2 * c_j * c_j * (w_j - x)    c_j * c_j * (w_j - x)
        ---- = -------------------------- = ---------------------
        dw_j    2 || c_j * (w_j - x) ||             y_j

        dy_j    2 * c_j * (w_j - x)^2    c_j * (w_j - x)^2
        ---- = ----------------------- = -----------------
        dc_j   2 || c_j * (w_j - x) ||         y_j
        #"""
        # assumes a preceding call to updateGradInput
        if input.dim() == 1:
            self.gradWeight.add_(-scale, self._repeat2)

            self._repeat.div_(self.diagCov)
            self._repeat.mul_(self._repeat)
            self._repeat.mul_(self.diagCov)

            if torch.type(input) == 'torch.cuda.FloatTensor':
                self._repeat2.resize_as_(self._expand4).copy_(self._expand4)
                self._repeat2.mul_(self._repeat)
            else:
                torch.mul(self._repeat, self._expand4, out=self._repeat2)


            self.gradDiagCov.add_(self._repeat2)
        elif input.dim() == 2:
            if self._sum is None:
                  self._sum = input.new()
            torch.sum(self._repeat2, 0, out=self._sum)
            self._sum.resize_(inputSize, outputSize)
            self.gradWeight.add_(-scale, self._sum)

            if input.type() == 'torch.cuda.FloatTensor':
                # requires lots of memory, but minimizes cudaMallocs and loops
                self._repeat.div_(self._repeat3)
                self._repeat.mul_(self._repeat)
                self._repeat.mul_(self._repeat3)
                self._repeat2.resize_as_(self._expand4).copy_(self._expand4)
                self._repeat.mul_(self._repeat2)
            else:
                self._repeat.div_(self._expand3)
                self._repeat.mul_(self._repeat)
                self._repeat.mul_(self._expand3)
                self._repeat.mul_(self._expand4)


            torch.sum(self._repeat, 0, out=self._sum)
            self._sum.resize_(inputSize, outputSize)
            self.gradDiagCov.add_(scale, self._sum)
        else:
            raise RuntimeError("1D or 2D input expected")

    def type(self, type=None, tensorCache=None):
        if type:
            # prevent premature memory allocations
            self._input = None
            self._output = None
            self._gradOutput = None
            self._weight = None
            self._div = None
            self._sum = None
            self._expand = None
            self._expand2 = None
            self._expand3 = None
            self._expand4 = None
            self._repeat = None
            self._repeat2 = None
            self._repeat3 = None
        return super(WeightedEuclidean, self).type(type, tensorCache)

    def parameters(self):
        return [self.weight, self.diagCov], [self.gradWeight, self.gradDiagCov]

    def accUpdateGradParameters(self, input, gradOutput, lr):
        gradWeight = self.gradWeight
        gradDiagCov = self.gradDiagCov
        self.gradWeight = self.weight
        self.gradDiagCov = self.diagCov
        self.accGradParameters(input, gradOutput, -lr)
        self.gradWeight = gradWeight
        self.gradDiagCov = gradDiagCov