C++ API parity: Softplus

Summary: Pull Request resolved: https://github.com/pytorch/pytorch/pull/27489

Test Plan: Imported from OSS

Differential Revision: D17835410

Pulled By: pbelevich

fbshipit-source-id: 51a8c4ab2ff4b860c96eda1ed8f073017b8cf9ae
This commit is contained in:
Pavel Belevich
2019-10-11 08:58:40 -07:00
committed by Facebook Github Bot
parent 9d448099fd
commit c79d3a4a98
6 changed files with 116 additions and 0 deletions
+35
View File
@@ -690,3 +690,38 @@ TEST_F(FunctionalTest, CELUDefaultOptions) {
ASSERT_EQ(y.sizes(), torch::IntArrayRef({size, size, size}));
ASSERT_TRUE(torch::allclose(y, y_exp));
}
TEST_F(FunctionalTest, Softplus) {
const auto size = 3;
for (const auto beta : {0.5, 1.0, 2.0}) {
for (const auto threshold : {1.0, 3.0, 5.0}) {
auto x = torch::linspace(-3.0, 3.0, 61);
x.resize_({size, size, size});
auto y_exp =
(x <= threshold) * torch::log(1 + torch::exp(x * beta)) / beta +
(x > threshold) * x;
auto y = F::softplus(x,
SoftplusOptions().beta(beta).threshold(threshold));
ASSERT_EQ(y.ndimension(), 3);
ASSERT_EQ(y.sizes(), torch::IntArrayRef({size, size, size}));
ASSERT_TRUE(torch::allclose(y, y_exp));
}
}
}
TEST_F(FunctionalTest, SoftplusDefaultOptions) {
const auto size = 3;
const auto beta = 1.0;
const auto threshold = 20.0;
auto x = torch::linspace(-3.0, 3.0, 61);
x.resize_({size, size, size});
auto y_exp =
(x <= threshold) * torch::log(1 + torch::exp(x * beta)) / beta +
(x > threshold) * x;
auto y = F::softplus(x);
ASSERT_EQ(y.ndimension(), 3);
ASSERT_EQ(y.sizes(), torch::IntArrayRef({size, size, size}));
ASSERT_TRUE(torch::allclose(y, y_exp));
}