diff options
Diffstat (limited to 'Narrow.lua')
-rw-r--r-- | Narrow.lua | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/Narrow.lua b/Narrow.lua new file mode 100644 index 0000000..4445983 --- /dev/null +++ b/Narrow.lua @@ -0,0 +1,24 @@ +local Narrow, parent = torch.class('nn.Narrow', 'nn.Module') + +function Narrow:__init(dimension,offset,length) + parent.__init(self) + self.dimension=dimension + self.index=offset + self.length=length or 1 + if not dimension or not offset then + error('nn.Narrow(dimension, offset, length)') + end +end + +function Narrow:updateOutput(input) + local output=input:narrow(self.dimension,self.index,self.length); + self.output:resizeAs(output) + return self.output:copy(output) +end + +function Narrow:updateGradInput(input, gradOutput) + self.gradInput:resizeAs(input) + self.gradInput:zero(); + self.gradInput:narrow(self.dimension,self.index,self.length):copy(gradOutput) + return self.gradInput +end |