C++ API parity: Threshold

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

Test Plan: Imported from OSS

Differential Revision: D17835415

Pulled By: pbelevich

fbshipit-source-id: 2a887704655be79ee458081c46a7eea31eca51dc
This commit is contained in:
Pavel Belevich
2019-10-13 09:36:18 -07:00
committed by Facebook Github Bot
parent cbdd55c669
commit 446a79b959
6 changed files with 118 additions and 0 deletions
+22
View File
@@ -787,3 +787,25 @@ TEST_F(FunctionalTest, Tanhshrink) {
ASSERT_TRUE(torch::allclose(y, y_exp));
}
TEST_F(FunctionalTest, Threshold) {
const auto size = 3;
for (const auto threshold : {0.5, 1.0, 2.0}) {
for (const auto value : {0.5, 1.0, 2.0}) {
for (const auto inplace : {false, true}) {
auto x = torch::linspace(-3.0, 3.0, 61);
x.resize_({size, size, size});
auto y_exp = (x <= threshold) * value + (x > threshold) * x;
auto y = F::threshold(x,
ThresholdOptions(threshold, value).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));
}
}
}
}
}