Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion test/common_nn.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ def kldivloss_reference(input, target, size_average=True, reduce=True):

def nlllossNd_reference(input, target, weight=None, ignore_index=-100,
size_average=True, reduce=True):
assert input.dim() >= 4
assert input.dim() >= 3
N = input.size(0)
C = input.size(1)
out_size = (N,) + input.size()[2:]
Expand Down
9 changes: 9 additions & 0 deletions test/test_nn.py
Original file line number Diff line number Diff line change
Expand Up @@ -4266,6 +4266,15 @@ def forward(self, *args):
check_no_size_average=True,
desc='higher_dim'
),
dict(
module_name='NLLLoss',
input_size=(2, 3, 5),
target_fn=lambda: torch.rand(2, 5).mul(3).floor().long(),
reference_fn=lambda i, t, m:
loss_reference_fns['NLLLossNd'](i, t, size_average=get_size_average(m)),
check_no_size_average=True,
desc='dim_is_3'
),
dict(
module_name='PoissonNLLLoss',
input_size=(2, 3, 4, 5),
Expand Down
8 changes: 4 additions & 4 deletions torch/nn/functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -1162,10 +1162,10 @@ def nll_loss(input, target, weight=None, size_average=True, ignore_index=-100, r

Args:
input: :math:`(N, C)` where `C = number of classes` or :math:`(N, C, H, W)`
in case of 2D Loss, or :math:`(N, C, d_1, d_2, ..., d_K)` where :math:`K > 2`
in case of 2D Loss, or :math:`(N, C, d_1, d_2, ..., d_K)` where :math:`K > 1`
in the case of K-dimensional loss.
target: :math:`(N)` where each value is `0 <= targets[i] <= C-1`,
or :math:`(N, C, d_1, d_2, ..., d_K)` where :math:`K >= 2` for
or :math:`(N, C, d_1, d_2, ..., d_K)` where :math:`K >= 1` for
K-dimensional loss.
weight (Tensor, optional): a manual rescaling weight given to each
class. If given, has to be a Tensor of size `C`
Expand All @@ -1192,7 +1192,7 @@ def nll_loss(input, target, weight=None, size_average=True, ignore_index=-100, r
return torch._C._nn.nll_loss(input, target, weight, size_average, ignore_index, reduce)
elif dim == 4:
return torch._C._nn.nll_loss2d(input, target, weight, size_average, ignore_index, reduce)
elif dim > 4:
elif dim == 3 or dim > 4:
n = input.size(0)
c = input.size(1)
out_size = (n,) + input.size()[2:]
Expand All @@ -1206,7 +1206,7 @@ def nll_loss(input, target, weight=None, size_average=True, ignore_index=-100, r
out = torch._C._nn.nll_loss2d(input, target, weight, size_average, ignore_index, reduce)
return out.view(out_size)
else:
raise ValueError('Expected 2, 4, or more than 4 dimensions (got {})'.format(dim))
raise ValueError('Expected 2 or more dimensions (got {})'.format(dim))


def poisson_nll_loss(input, target, log_input=True, full=False, size_average=True, eps=1e-8, reduce=True):
Expand Down