-
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 #1226 from nicholas-leonard/ZipTable
nn.ZipTable
- Loading branch information
Showing
5 changed files
with
157 additions
and
4 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,34 @@ | ||
local ZipTable, parent = torch.class('nn.ZipTable', 'nn.Module') | ||
|
||
-- input : { {a1,a2}, {b1,b2}, {c1,c2} } | ||
-- output : { {a1,b1,c1}, {a2,b2,c2} } | ||
function ZipTable:__init() | ||
parent.__init(self) | ||
self.output = {} | ||
self.gradInput = {} | ||
end | ||
|
||
function ZipTable:updateOutput(inputTable) | ||
self.output = {} | ||
for i,inTable in ipairs(inputTable) do | ||
for j,input in ipairs(inTable) do | ||
local output = self.output[j] or {} | ||
output[i] = input | ||
self.output[j] = output | ||
end | ||
end | ||
return self.output | ||
end | ||
|
||
function ZipTable:updateGradInput(inputTable, gradOutputTable) | ||
self.gradInput = {} | ||
for i,gradOutTable in ipairs(gradOutputTable) do | ||
for j,gradOutput in ipairs(gradOutTable) do | ||
local gradInput = self.gradInput[j] or {} | ||
gradInput[i] = gradOutput | ||
self.gradInput[j] = gradInput | ||
end | ||
end | ||
return self.gradInput | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
local ZipTableOneToMany, parent = torch.class('nn.ZipTableOneToMany', 'nn.Module') | ||
|
||
-- based on ZipTable in dpnn | ||
|
||
-- input : { v, {a, b, c} } | ||
-- output : { {v,a}, {v,b}, {v,c} } | ||
function ZipTableOneToMany:__init() | ||
parent.__init(self) | ||
self.output = {} | ||
self.gradInput = {} | ||
-- make buffer to update during forward/backward | ||
self.gradInputEl = torch.Tensor() | ||
end | ||
|
||
function ZipTableOneToMany:updateOutput(input) | ||
assert(#input == 2, "input must be table of element and table") | ||
local inputEl, inputTable = input[1], input[2] | ||
self.output = {} | ||
for i,v in ipairs(inputTable) do | ||
self.output[i] = {inputEl, v} | ||
end | ||
return self.output | ||
end | ||
|
||
function ZipTableOneToMany:updateGradInput(input, gradOutput) | ||
assert(#input == 2, "input must be table of element and table") | ||
local inputEl, inputTable = input[1], input[2] | ||
self.gradInputEl:resizeAs(inputEl):zero() | ||
local gradInputTable = {} | ||
for i,gradV in ipairs(gradOutput) do | ||
self.gradInputEl:add(gradV[1]) | ||
gradInputTable[i] = gradV[2] | ||
end | ||
self.gradInput = {self.gradInputEl, gradInputTable} | ||
return self.gradInput | ||
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