mirror of
https://github.com/pytorch/pytorch.git
synced 2025-10-21 05:30:26 +08:00
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.
47 lines
1.2 KiB
Python
47 lines
1.2 KiB
Python
import torch
|
|
from .Criterion import Criterion
|
|
|
|
|
|
class ClassNLLCriterion(Criterion):
|
|
|
|
def __init__(self, weights=None, sizeAverage=True):
|
|
super(ClassNLLCriterion, self).__init__()
|
|
self.sizeAverage = sizeAverage
|
|
|
|
if weights is not None:
|
|
assert weights.dim() == 1
|
|
self.weights = weights
|
|
|
|
self.output_tensor = torch.zeros(1)
|
|
self.total_weight_tensor = torch.ones(1)
|
|
|
|
def updateOutput(self, input, target):
|
|
target = target.long()
|
|
self._backend.ClassNLLCriterion_updateOutput(
|
|
self._backend.library_state,
|
|
input,
|
|
target,
|
|
self.output_tensor,
|
|
self.sizeAverage,
|
|
self.weights,
|
|
self.total_weight_tensor
|
|
)
|
|
self.output = self.output_tensor[0]
|
|
return self.output
|
|
|
|
def updateGradInput(self, input, target):
|
|
self.gradInput.resize_as_(input).zero_()
|
|
target = target.long()
|
|
|
|
self._backend.ClassNLLCriterion_updateGradInput(
|
|
self._backend.library_state,
|
|
input,
|
|
target,
|
|
self.gradInput,
|
|
self.sizeAverage,
|
|
self.weights,
|
|
self.total_weight_tensor
|
|
)
|
|
|
|
return self.gradInput
|