Created
November 20, 2014 04:24
-
-
Save soumith/0f95facad88cbea68c6d to your computer and use it in GitHub Desktop.
linear with no bias
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.NoBiasLinear', 'nn.Linear') | |
function Linear:__init(inputSize, outputSize) | |
parent.__init(self, inputSize, outputSize) | |
self.bias:fill(0) | |
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 | |
local nframe = input:size(1) | |
local nunit = self.bias:size(1) | |
if nunit == 1 then | |
-- Special case to fix output size of 1 bug: | |
self.gradWeight:select(1,1):addmv(scale, input:t(), gradOutput:select(2,1)) | |
else | |
self.gradWeight:addmm(scale, gradOutput:t(), input) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment