|
| 1 | +local Normalize, parent = torch.class('nn.Normalize', 'nn.Module') |
| 2 | + |
| 3 | +function Normalize:__init(p,eps) |
| 4 | + parent.__init(self) |
| 5 | + assert(p,'p-norm not provided') |
| 6 | + assert(p > 0, p..'-norm not supported') |
| 7 | + self.p = p |
| 8 | + self.eps = eps or 1e-10 |
| 9 | +end |
| 10 | + |
| 11 | +function Normalize:updateOutput(input) |
| 12 | + assert(input:dim() <= 2, 'only 1d layer supported') |
| 13 | + local is_batch = true |
| 14 | + if input:dim() == 1 then |
| 15 | + input = input:view(1,-1) |
| 16 | + is_batch = false |
| 17 | + end |
| 18 | + |
| 19 | + self.output:resizeAs(input) |
| 20 | + |
| 21 | + self.norm = self.norm or input.new() |
| 22 | + self.normp = self.normp or input.new() |
| 23 | + self.buffer = self.buffer or input.new() |
| 24 | + |
| 25 | + if self.p % 2 ~= 0 then |
| 26 | + self.buffer:abs(input):pow(self.p) |
| 27 | + else |
| 28 | + self.buffer:pow(input,self.p) |
| 29 | + end |
| 30 | + self.normp:sum(self.buffer,2):add(self.eps) |
| 31 | + self.norm:pow(self.normp,1/self.p) |
| 32 | + self.output:cdiv(input,self.norm:view(-1,1):expandAs(self.output)) |
| 33 | + |
| 34 | + if not is_batch then |
| 35 | + self.output = self.output[1] |
| 36 | + end |
| 37 | + return self.output |
| 38 | +end |
| 39 | + |
| 40 | +function Normalize:updateGradInput(input, gradOutput) |
| 41 | + assert(input:dim() <= 2, 'only 1d layer supported') |
| 42 | + assert(gradOutput:dim() <= 2, 'only 1d layer supported') |
| 43 | + |
| 44 | + local is_batch = true |
| 45 | + if input:dim() == 1 then |
| 46 | + input = input:view(1,-1) |
| 47 | + is_batch = false |
| 48 | + end |
| 49 | + |
| 50 | + local n = input:size(1) -- batch size |
| 51 | + local d = input:size(2) -- dimensionality of vectors |
| 52 | + -- compute diagonal term |
| 53 | + self.eye = self.eye or torch.eye(d):typeAs(input):view(1,d,d) |
| 54 | + local eyeExpand = self.eye:expand(n,d,d) |
| 55 | + self.diag = self.diag or self.eye.new() |
| 56 | + self.diag:cmul(eyeExpand, self.normp:view(n,1,1):expand(n,d,d)) |
| 57 | + -- compute cross term |
| 58 | + self.buffer:abs(input):pow(self.p-2):cmul(input) |
| 59 | + local b1 = self.buffer:view(n,d,1) |
| 60 | + local b2 = input:view(n,1,d) |
| 61 | + |
| 62 | + self.diag:baddbmm(-1,b1,b2) |
| 63 | + -- compute the local gradient of the Lp transformation |
| 64 | + self.buffer:cmul(self.normp,self.norm) |
| 65 | + self.diag:cdiv(self.buffer:view(n,1,1):expand(n,d,d)) |
| 66 | + -- chain the gradient |
| 67 | + self.gradInput:resize(n,d,1) |
| 68 | + self.gradInput:bmm(self.diag, gradOutput:view(n,d,1)) |
| 69 | + self.gradInput = self.gradInput:view(n,d) |
| 70 | + |
| 71 | + if not is_batch then |
| 72 | + self.gradInput = self.gradInput[1] |
| 73 | + end |
| 74 | + |
| 75 | + return self.gradInput |
| 76 | +end |
| 77 | + |
| 78 | +function Normalize:__tostring__() |
| 79 | + local s |
| 80 | + -- different prints if the norm is integer |
| 81 | + if self.p % 1 == 0 then |
| 82 | + s = '%s(%d)' |
| 83 | + else |
| 84 | + s = '%s(%f)' |
| 85 | + end |
| 86 | + return string.format(s,torch.type(self),self.p) |
| 87 | +end |
0 commit comments