-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5204469
commit aef9ec5
Showing
2 changed files
with
70 additions
and
18 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
local MinDim, parent = torch.class('nn.MinDim', 'nn.Module') | ||
|
||
local function _assertTensor(t) | ||
assert(torch.isTensor(t), "This module only works on tensor") | ||
end | ||
|
||
function MinDim:__init(pos, minInputDims) | ||
parent.__init(self) | ||
self.pos = pos or error('the position to insert singleton dim not specified') | ||
self:setMinInputDims(minInputDims) | ||
end | ||
|
||
function MinDim:setMinInputDims(numInputDims) | ||
self.numInputDims = numInputDims | ||
return self | ||
end | ||
|
||
function MinDim:updateOutput(input) | ||
_assertTensor(input) | ||
self.output = input | ||
if input:dim() < self.numInputDims then | ||
nn.utils.addSingletonDimension(self.output, input, self.pos) | ||
end | ||
return self.output | ||
end | ||
|
||
function MinDim:updateGradInput(input, gradOutput) | ||
_assertTensor(input) | ||
_assertTensor(gradOutput) | ||
assert(input:nElement() == gradOutput:nElement()) | ||
self.gradInput:view(gradOutput, input:size()) | ||
return self.gradInput | ||
end |