Skip to content

Instantly share code, notes, and snippets.

@soumith
Created November 20, 2014 04:24
Show Gist options
  • Save soumith/0f95facad88cbea68c6d to your computer and use it in GitHub Desktop.
Save soumith/0f95facad88cbea68c6d to your computer and use it in GitHub Desktop.
linear with no bias
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