-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ReverseTable+SeqReverseSequence -> ReverseSequnece
- Loading branch information
Nicholas Leonard
committed
May 17, 2017
1 parent
6ca4c57
commit 72091ff
Showing
6 changed files
with
106 additions
and
49 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,75 @@ | ||
local ReverseSequence, parent = torch.class("nn.ReverseSequence", "nn.Module") | ||
|
||
function ReverseSequence:updateOutput(input) | ||
local seqlen | ||
if torch.isTensor(input) then | ||
seqlen = input:size(1) | ||
self.output = torch.isTensor(self.output) and self.output or input.new() | ||
self.output:resizeAs(input) | ||
|
||
self._range = self._range or torch.isCudaTensor(input) and torch.CudaLongTensor() or torch.LongTensor() | ||
if self._range:nElement() ~= seqlen then | ||
self._range:range(seqlen,1,-1) | ||
end | ||
self.output:index(input, 1, self._range) | ||
else | ||
seqlen = #input | ||
self.output = torch.type(self.output) == 'table' and self.output or {} | ||
assert(torch.type(input) == 'table', "Expecting table or tensor at arg 1") | ||
|
||
-- empty output table | ||
for k,v in ipairs(self.output) do | ||
self.output[k] = nil | ||
end | ||
|
||
-- reverse input | ||
local k = 1 | ||
for i=seqlen,1,-1 do | ||
self.output[k] = input[i] | ||
k = k + 1 | ||
end | ||
end | ||
|
||
return self.output | ||
end | ||
|
||
function ReverseSequence:updateGradInput(input, gradOutput) | ||
local seqlen | ||
if torch.isTensor(input) then | ||
seqlen = input:size(1) | ||
self.gradInput = torch.isTensor(self.gradInput) and self.gradInput or input.new() | ||
self.gradInput:resizeAs(input) | ||
|
||
self.gradInput:index(gradOutput, 1, self._range) | ||
else | ||
seqlen = #input | ||
self.gradInput = torch.type(self.gradInput) == 'table' and self.gradInput or {} | ||
assert(torch.type(gradOutput) == 'table', "Expecting table or tensor at arg 2") | ||
|
||
-- empty gradInput table | ||
for k,v in ipairs(self.gradInput) do | ||
self.gradInput[k] = nil | ||
end | ||
|
||
-- reverse gradOutput | ||
local k = 1 | ||
for i=seqlen,1,-1 do | ||
self.gradInput[k] = gradOutput[i] | ||
k = k + 1 | ||
end | ||
end | ||
|
||
return self.gradInput | ||
end | ||
|
||
function ReverseSequence:clearState() | ||
self.gradInput = torch.Tensor() | ||
self.output = torch.Tensor() | ||
self._range = nil | ||
end | ||
|
||
function ReverseSequence:type(...) | ||
self:clearState() | ||
return parent.type(self, ...) | ||
end | ||
|
This file was deleted.
Oops, something went wrong.
File renamed without changes.
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