Fix torch::nn::init::orthogonal_ with CNNs (#18915)

Summary:
Fixes #18518

I changed the C++ API torch::nn::init::orthogonal_ implementation to match the Python implementation.
Pull Request resolved: https://github.com/pytorch/pytorch/pull/18915

Differential Revision: D14851833

Pulled By: ezyang

fbshipit-source-id: 45b5e9741582777c203e9ebed564ab3ac1f94baf
This commit is contained in:
Omegastick
2019-04-09 10:36:13 -07:00
committed by Facebook Github Bot
parent 25bd28c3a0
commit 31ff0ecd2b
3 changed files with 8 additions and 2 deletions
+6
View File
@@ -2,6 +2,7 @@
#include <torch/nn/init.h>
#include <torch/nn/modules/linear.h>
#include <torch/nn/modules/conv.h>
#include <test/cpp/api/init_baseline.h>
#include <test/cpp/api/support.h>
@@ -123,4 +124,9 @@ TEST(InitTest, CalculateGainWithLeakyRelu) {
double gain =
torch::nn::init::calculate_gain(torch::nn::init::Nonlinearity::LeakyReLU);
ASSERT_DOUBLE_EQ(gain, std::sqrt(2.0 / (1 + pow(0.01, 2))));
}
TEST(InitTest, CanInitializeCnnWithOrthogonal) {
torch::nn::Conv2d conv_layer(torch::nn::Conv2dOptions(3, 2, 3).stride(2));
torch::nn::init::orthogonal_(conv_layer->named_parameters()["weight"]);
}