Files
pytorch/torch/legacy/nn/JoinTable.py
Luke Yeager e7c1e6a8e3 [pep8] Fix most lint automatically with autopep8
Here's the command I used to invoke autopep8 (in parallel!):

    git ls-files | grep '\.py$' | xargs -n1 -P`nproc` autopep8 -i

Several rules are ignored in setup.cfg. The goal is to let autopep8
handle everything which it can handle safely, and to disable any rules
which are tricky or controversial to address. We may want to come back
and re-enable some of these rules later, but I'm trying to make this
patch as safe as possible.

Also configures flake8 to match pep8's behavior.

Also configures TravisCI to check the whole project for lint.
2017-01-28 01:15:51 +01:00

63 lines
1.8 KiB
Python

import torch
from .Module import Module
class JoinTable(Module):
def __init__(self, dimension):
super(JoinTable, self).__init__()
self.size = torch.Size()
self.dimension = dimension
self.gradInput = []
def _getPositiveDimension(self, input):
dimension = self.dimension
if dimension < 0:
dimension = input[0].dim() + dimension
return dimension
def updateOutput(self, input):
dim = self._getPositiveDimension(input)
for i in range(len(input)):
currentOutput = input[i]
if i == 0:
size = list(currentOutput.size())
else:
size[dim] += currentOutput.size(dim)
self.size = torch.Size(size)
self.output.resize_(self.size)
# TODO: use cat?
offset = 0
for i in range(len(input)):
currentOutput = input[i]
self.output.narrow(dim, offset, currentOutput.size(dim)).copy_(currentOutput)
offset += currentOutput.size(dim)
return self.output
def updateGradInput(self, input, gradOutput):
dim = self._getPositiveDimension(input)
for i in range(len(input)):
if len(self.gradInput) < i + 1:
self.gradInput.append(input[i].new())
self.gradInput[i].resize_as_(input[i])
self.gradInput = self.gradInput[:len(input)]
offset = 0
for i in range(len(input)):
currentOutput = input[i]
currentGradInput = gradOutput.narrow(dim, offset, currentOutput.size(dim))
self.gradInput[i].copy_(currentGradInput)
offset = offset + currentOutput.size(dim)
return self.gradInput
def type(self, type=None, tensorCache=None):
self.gradInput = []
return super(JoinTable, self).type(type, tensorCache)