Skip to content
Closed
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
6 changes: 6 additions & 0 deletions test/test_nn.py
Original file line number Diff line number Diff line change
Expand Up @@ -4812,6 +4812,12 @@ def test_triplet_margin_loss_swap_no_reduce(self):
self.assertEqual(F.triplet_margin_loss(input1, input2, input3, swap=True, reduction='none'),
loss_reference_fns['TripletMarginLoss'](input1, input2, input3, swap=True, reduction='none'))

def test_pointwise_loss_target_grad_none_reduction(self):
i = torch.randn(5, 10)
t = torch.randn(5, 10, requires_grad=True)
self.assertEqual(F.mse_loss(i, t, reduction='none').size(), t.size())
self.assertEqual(F.l1_loss(i, t, reduction='none').size(), t.size())

def test_cosine_similarity(self):
input1 = torch.randn(4, 4, requires_grad=True)
input2 = torch.randn(4, 4, requires_grad=True)
Expand Down
10 changes: 3 additions & 7 deletions torch/nn/functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -1671,7 +1671,7 @@ def _pointwise_loss(lambd, lambd_optimized, input, target, reduction='elementwis
return d
return torch.mean(d) if reduction == 'elementwise_mean' else torch.sum(d)
else:
return lambd_optimized(input, target, reduction)
return lambd_optimized(input, target, _Reduction.get_enum(reduction))


def smooth_l1_loss(input, target, size_average=None, reduce=None, reduction='elementwise_mean'):
Expand All @@ -1695,9 +1695,7 @@ def l1_loss(input, target, size_average=None, reduce=None, reduction='elementwis
See :class:`~torch.nn.L1Loss` for details.
"""
if size_average is not None or reduce is not None:
reduction = _Reduction.legacy_get_enum(size_average, reduce)
else:
reduction = _Reduction.get_enum(reduction)
reduction = _Reduction.legacy_get_string(size_average, reduce)
return _pointwise_loss(lambda a, b: torch.abs(a - b), torch._C._nn.l1_loss,
input, target, reduction)

Expand All @@ -1710,9 +1708,7 @@ def mse_loss(input, target, size_average=None, reduce=None, reduction='elementwi
See :class:`~torch.nn.MSELoss` for details.
"""
if size_average is not None or reduce is not None:
reduction = _Reduction.legacy_get_enum(size_average, reduce)
else:
reduction = _Reduction.get_enum(reduction)
reduction = _Reduction.legacy_get_string(size_average, reduce)
return _pointwise_loss(lambda a, b: (a - b) ** 2, torch._C._nn.mse_loss, input, target, reduction)


Expand Down