Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add option to nn.Linear to have no bias #583

Merged
merged 1 commit into from
Jan 26, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 22 additions & 15 deletions Linear.lua
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
local Linear, parent = torch.class('nn.Linear', 'nn.Module')

function Linear:__init(inputSize, outputSize)
function Linear:__init(inputSize, outputSize, bias)
parent.__init(self)

local bias = ((bias == nil) and true) or bias
self.weight = torch.Tensor(outputSize, inputSize)
self.bias = torch.Tensor(outputSize)
self.gradWeight = torch.Tensor(outputSize, inputSize)
self.gradBias = torch.Tensor(outputSize)

if bias then
self.bias = torch.Tensor(outputSize)
self.gradBias = torch.Tensor(outputSize)
end
self:reset()
end

Expand All @@ -22,25 +23,28 @@ function Linear:reset(stdv)
self.weight:select(1, i):apply(function()
return torch.uniform(-stdv, stdv)
end)
self.bias[i] = torch.uniform(-stdv, stdv)
end
if self.bias then
for i=1,self.bias:nElement() do
self.bias[i] = torch.uniform(-stdv, stdv)
end
end
else
self.weight:uniform(-stdv, stdv)
self.bias:uniform(-stdv, stdv)
if self.bias then self.bias:uniform(-stdv, stdv) end
end

return self
end

function Linear:updateOutput(input)
if input:dim() == 1 then
self.output:resize(self.bias:size(1))
self.output:copy(self.bias)
self.output:resize(self.weight:size(1))
if self.bias then self.output:copy(self.bias) else self.output:zero() end
self.output:addmv(1, self.weight, input)
elseif input:dim() == 2 then
local nframe = input:size(1)
local nElement = self.output:nElement()
self.output:resize(nframe, self.bias:size(1))
self.output:resize(nframe, self.weight:size(1))
if self.output:nElement() ~= nElement then
self.output:zero()
end
Expand All @@ -49,7 +53,7 @@ function Linear:updateOutput(input)
self.addBuffer:resize(nframe):fill(1)
end
self.output:addmm(0, self.output, 1, input, self.weight:t())
self.output:addr(1, self.addBuffer, self.bias)
if self.bias then self.output:addr(1, self.addBuffer, self.bias) end
else
error('input must be vector or matrix')
end
Expand Down Expand Up @@ -79,10 +83,12 @@ function Linear:accGradParameters(input, gradOutput, scale)
scale = scale or 1
if input:dim() == 1 then
self.gradWeight:addr(scale, gradOutput, input)
self.gradBias:add(scale, gradOutput)
if self.bias then self.gradBias:add(scale, gradOutput) end
elseif input:dim() == 2 then
self.gradWeight:addmm(scale, gradOutput:t(), input)
self.gradBias:addmv(scale, gradOutput:t(), self.addBuffer)
if self.bias then
self.gradBias:addmv(scale, gradOutput:t(), self.addBuffer)
end
end
end

Expand All @@ -92,5 +98,6 @@ Linear.sharedAccUpdateGradParameters = Linear.accUpdateGradParameters

function Linear:__tostring__()
return torch.type(self) ..
string.format('(%d -> %d)', self.weight:size(2), self.weight:size(1))
string.format('(%d -> %d)', self.weight:size(2), self.weight:size(1)) ..
(self.bias == nil and ' without bias' or '')
end
4 changes: 2 additions & 2 deletions doc/simple.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@ Simple Modules are used for various tasks like adapting Tensor methods and provi
## Linear ##

```lua
module = nn.Linear(inputDimension, outputDimension)
module = nn.Linear(inputDimension, outputDimension, [bias = true])
```

Applies a linear transformation to the incoming data, i.e. `y = Ax + b`. The `input` tensor given in `forward(input)` must be either a vector (1D tensor) or matrix (2D tensor). If the input is a matrix, then each row is assumed to be an input sample of given batch.
Applies a linear transformation to the incoming data, i.e. `y = Ax + b`. The `input` tensor given in `forward(input)` must be either a vector (1D tensor) or matrix (2D tensor). If the input is a matrix, then each row is assumed to be an input sample of given batch. The layer can be used without bias by setting `bias = false`.

You can create a layer in the following way:

Expand Down