-
-
Save soumith/4d0273f592956199739b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local Linear, parent = torch.class('nn.NormalizedLinearNoBias', 'nn.Linear') | |
--[[ | |
This module creates a Linear layer, but with no bias component. | |
In training mode, it constantly self-normalizes it's weights to | |
be of unit norm. | |
Authors: Mark Tygert, Soumith Chintala | |
]]-- | |
function Linear:__init(inputSize, outputSize) | |
parent.__init(self, inputSize, outputSize) | |
self.bias:zero() | |
end | |
function Linear:updateOutput(input) | |
if self.train then | |
-- in training mode, renormalize the weights | |
-- before every forward call | |
self.weight:div(self.weight:norm()) | |
local scale = math.sqrt(self.weight:size(1)) | |
self.weight:mul(scale) | |
end | |
return parent.updateOutput(self, input) | |
end | |
function Linear:accGradParameters(input, gradOutput, scale) | |
scale = scale or 1 | |
if input:dim() == 1 then | |
self.gradWeight:addr(scale, gradOutput, input) | |
elseif input:dim() == 2 then | |
self.gradWeight:addmm(scale, gradOutput:t(), input) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment