C++ API parity: CELU

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

Test Plan: Imported from OSS

Differential Revision: D17835406

Pulled By: pbelevich

fbshipit-source-id: a8282ae65d8996efcc8b8d846cfa637c3f89eda6
This commit is contained in:
Pavel Belevich
2019-10-11 06:21:06 -07:00
committed by Facebook Github Bot
parent cddc147267
commit 795c913636
6 changed files with 119 additions and 0 deletions
+34
View File
@@ -656,3 +656,37 @@ TEST_F(FunctionalTest, RReLUDefaultOptions) {
ASSERT_EQ(y.sizes(), torch::IntArrayRef({size, size, size}));
ASSERT_TRUE(torch::allclose(z, torch::ones_like(z)));
}
TEST_F(FunctionalTest, CELU) {
const auto size = 3;
for (const auto inplace : {false, true}) {
for (const auto alpha : {0.42, 1.0, 4.2, 42.42}) {
auto x = torch::linspace(-10.0, 10.0, size * size * size);
x.resize_({size, size, size});
auto y_exp = torch::max(torch::zeros_like(x), x) +
torch::min(torch::zeros_like(x), alpha * (torch::exp(x / alpha) - 1.0));
auto y = F::celu(x, CELUOptions().alpha(alpha).inplace(inplace));
ASSERT_EQ(y.ndimension(), 3);
ASSERT_EQ(y.sizes(), torch::IntArrayRef({size, size, size}));
ASSERT_TRUE(torch::allclose(y, y_exp));
if (inplace) {
ASSERT_TRUE(torch::allclose(x, y_exp));
}
}
}
}
TEST_F(FunctionalTest, CELUDefaultOptions) {
const auto size = 3;
const auto alpha = 1.0;
auto x = torch::linspace(-10.0, 10.0, size * size * size);
x.resize_({size, size, size});
auto y_exp = torch::max(torch::zeros_like(x), x) +
torch::min(torch::zeros_like(x), alpha * (torch::exp(x / alpha) - 1.0));
auto y = F::celu(x);
ASSERT_EQ(y.ndimension(), 3);
ASSERT_EQ(y.sizes(), torch::IntArrayRef({size, size, size}));
ASSERT_TRUE(torch::allclose(y, y_exp));
}