mirror of
https://github.com/pytorch/pytorch.git
synced 2026-11-20 09:04:25 +08:00
Smooth L1 loss (#27661)
Summary: In accordance with https://github.com/pytorch/pytorch/issues/25883, I added the `SmoothL1Loss` module and `smooth_l1_loss` functional. Pull Request resolved: https://github.com/pytorch/pytorch/pull/27661 Differential Revision: D18002332 Pulled By: yf225 fbshipit-source-id: b382df8becb0de14986ec16ee0dc953d7b10e917
This commit is contained in:
committed by
Facebook Github Bot
parent
1c8ef29ac5
commit
72b9bda9e5
@@ -101,6 +101,30 @@ TEST_F(FunctionalTest, CosineSimilarity) {
|
||||
ASSERT_TRUE(output.allclose(expected, 1e-04));
|
||||
}
|
||||
|
||||
TEST_F(FunctionalTest, SmoothL1LossDefaultOptions) {
|
||||
auto input = torch::tensor({0.1, 1.2, 4.7}, torch::dtype(torch::kFloat).requires_grad(true));
|
||||
auto target = torch::tensor({0., 1., 5.}, torch::kFloat);
|
||||
auto output =
|
||||
F::smooth_l1_loss(input, target);
|
||||
auto expected = torch::tensor(0.0233335, torch::kFloat);
|
||||
auto s = output.sum();
|
||||
s.backward();
|
||||
ASSERT_TRUE(output.allclose(expected));
|
||||
ASSERT_TRUE(input.sizes() == input.grad().sizes());
|
||||
}
|
||||
|
||||
TEST_F(FunctionalTest, SmoothL1LossNoReduction) {
|
||||
auto input = torch::tensor({0.1, 1.2, 4.7}, torch::dtype(torch::kFloat).requires_grad(true));
|
||||
auto target = torch::tensor({0., 1., 5.}, torch::kFloat);
|
||||
auto output =
|
||||
F::smooth_l1_loss(input, target, /*reduction=*/torch::Reduction::None);
|
||||
auto expected = torch::tensor({0.005, 0.02, 0.045}, torch::kFloat);
|
||||
auto s = output.sum();
|
||||
s.backward();
|
||||
ASSERT_TRUE(output.allclose(expected));
|
||||
ASSERT_TRUE(input.sizes() == input.grad().sizes());
|
||||
}
|
||||
|
||||
TEST_F(FunctionalTest, SoftMarginLossDefaultOptions) {
|
||||
auto input = torch::tensor({2., 4., 1., 3.}, torch::dtype(torch::kFloat).requires_grad(true));
|
||||
auto target = torch::tensor({-1., 1., 1., -1.}, torch::kFloat);
|
||||
|
||||
Reference in New Issue
Block a user