-
Notifications
You must be signed in to change notification settings - Fork 958
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1227 from nicholas-leonard/Collapse
nn.Collapse
- Loading branch information
Showing
4 changed files
with
69 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
local Collapse, parent = torch.class('nn.Collapse', 'nn.Module') | ||
|
||
-- collapses non-batch dims | ||
function Collapse:__init(nInputDim) | ||
parent.__init(self) | ||
self.nInputDim = nInputDim | ||
end | ||
|
||
function Collapse:updateOutput(input) | ||
if not input:isContiguous() then | ||
self._input = self._input or input.new() | ||
self._input:resize(input:size()):copy(input) | ||
input = self._input | ||
end | ||
if input:dim() > self.nInputDim then | ||
self.output:view(input,input:size(1),-1) | ||
else | ||
self.output:view(input,-1) | ||
end | ||
return self.output | ||
end | ||
|
||
function Collapse:updateGradInput(input, gradOutput) | ||
self.gradInput:view(gradOutput, input:size()) | ||
return self.gradInput | ||
end | ||
|
||
function Collapse:clearState() | ||
self._input = nil | ||
end |
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
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