mirror of
https://github.com/pytorch/pytorch.git
synced 2026-11-20 09:04:25 +08:00
C++ API: torch::nn::BatchNorm{2,3}d (#28936)
Summary:
Add torch::nn::BatchNorm{2,3}d module and functional support for the C++ API.
Related Issue: https://github.com/pytorch/pytorch/issues/25883 #28176
Reviewer: yf225
Pull Request resolved: https://github.com/pytorch/pytorch/pull/28936
Differential Revision: D18274584
Pulled By: yf225
fbshipit-source-id: 3784eee9f8947f6c7c9f1699544a3d36a1a019b7
This commit is contained in:
committed by
Facebook Github Bot
parent
23193c155f
commit
a68c1e109e
@@ -1362,6 +1362,66 @@ TEST_F(FunctionalTest, BatchNorm1dDefaultOptions) {
|
||||
ASSERT_TRUE(output.allclose(expected));
|
||||
}
|
||||
|
||||
TEST_F(FunctionalTest, BatchNorm2d) {
|
||||
int num_features = 5;
|
||||
double eps = 1e-05;
|
||||
double momentum = 0.1;
|
||||
|
||||
auto input = torch::randn({2, num_features, 4, 4});
|
||||
auto mean = torch::randn(num_features);
|
||||
auto variance = torch::rand(num_features);
|
||||
auto weight = torch::ones({num_features});
|
||||
auto bias = torch::zeros({num_features});
|
||||
auto output = F::batch_norm(
|
||||
input, mean, variance,
|
||||
BatchNormOptions().weight(weight).bias(bias).momentum(momentum).eps(eps),
|
||||
/*training=*/false);
|
||||
auto expected = torch::transpose((torch::transpose(input, 1, 3) - mean) / torch::sqrt(variance + eps), 1, 3);
|
||||
ASSERT_TRUE(output.allclose(expected));
|
||||
}
|
||||
|
||||
TEST_F(FunctionalTest, BatchNorm2dDefaultOptions) {
|
||||
int num_features = 5;
|
||||
double eps = 1e-05;
|
||||
|
||||
auto input = torch::randn({2, num_features, 4, 4});
|
||||
auto mean = torch::randn(num_features);
|
||||
auto variance = torch::rand(num_features);
|
||||
auto output = F::batch_norm(input, mean, variance);
|
||||
auto expected = torch::transpose((torch::transpose(input, 1, 3) - mean) / torch::sqrt(variance + eps), 1, 3);
|
||||
ASSERT_TRUE(output.allclose(expected));
|
||||
}
|
||||
|
||||
TEST_F(FunctionalTest, BatchNorm3d) {
|
||||
int num_features = 5;
|
||||
double eps = 1e-05;
|
||||
double momentum = 0.1;
|
||||
|
||||
auto input = torch::randn({2, num_features, 2, 2, 2});
|
||||
auto mean = torch::randn(num_features);
|
||||
auto variance = torch::rand(num_features);
|
||||
auto weight = torch::ones({num_features});
|
||||
auto bias = torch::zeros({num_features});
|
||||
auto output = F::batch_norm(
|
||||
input, mean, variance,
|
||||
BatchNormOptions().weight(weight).bias(bias).momentum(momentum).eps(eps),
|
||||
/*training=*/false);
|
||||
auto expected = torch::transpose((torch::transpose(input, 1, 4) - mean) / torch::sqrt(variance + eps), 1, 4);
|
||||
ASSERT_TRUE(output.allclose(expected));
|
||||
}
|
||||
|
||||
TEST_F(FunctionalTest, BatchNorm3dDefaultOptions) {
|
||||
int num_features = 5;
|
||||
double eps = 1e-05;
|
||||
|
||||
auto input = torch::randn({2, num_features, 2, 2, 2});
|
||||
auto mean = torch::randn(num_features);
|
||||
auto variance = torch::rand(num_features);
|
||||
auto output = F::batch_norm(input, mean, variance);
|
||||
auto expected = torch::transpose((torch::transpose(input, 1, 4) - mean) / torch::sqrt(variance + eps), 1, 4);
|
||||
ASSERT_TRUE(output.allclose(expected));
|
||||
}
|
||||
|
||||
TEST_F(FunctionalTest, Interpolate) {
|
||||
{
|
||||
// 1D interpolation
|
||||
|
||||
Reference in New Issue
Block a user