mirror of
https://github.com/pytorch/pytorch.git
synced 2026-11-20 09:04:25 +08:00
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:
committed by
Facebook Github Bot
parent
9d448099fd
commit
c79d3a4a98
@@ -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));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user