diff --git a/python/paddle/fluid/initializer.py b/python/paddle/fluid/initializer.py index 8560d5ac1395a133f991645661d68e8925e2506f..3763b72846a1b6f5a2ef6df7a24cb179bef19874 100644 --- a/python/paddle/fluid/initializer.py +++ b/python/paddle/fluid/initializer.py @@ -783,6 +783,16 @@ class MSRAInitializer(Initializer): # If fan_in is passed, use it fan_in = f_in if self._fan_in is None else self._fan_in + if fan_in == 0: + if self._fan_in is None: + raise ValueError( + "The in_features of the Tensor contain zero, can not initialize the Tensor." + ) + else: + raise ValueError( + "fan_in should not be zero, can not initialize the Tensor." + ) + if self._seed == 0: self._seed = block.program.random_seed diff --git a/python/paddle/fluid/tests/unittests/test_initializer.py b/python/paddle/fluid/tests/unittests/test_initializer.py index bae17622e2aec97c4f3bebc8bc26c9c59111d2c4..07d9d7b48c29f6b777c428dc9e92a6d87de868e0 100644 --- a/python/paddle/fluid/tests/unittests/test_initializer.py +++ b/python/paddle/fluid/tests/unittests/test_initializer.py @@ -1176,6 +1176,22 @@ class TestDiracInitializer3(TestDiracInitializer1): paddle.nn.Conv2D(5, 9, (3, 3), weight_attr=self.weight_attr) +class TestKaimingUniform(unittest.TestCase): + def func_kaiminguniform_initializer_fan_in_zero(self): + paddle.enable_static() + x = paddle.static.data(name='x', shape=[1, 0, 0], dtype='float32') + + kaiming = paddle.nn.initializer.KaimingUniform(0) + param_attr = paddle.ParamAttr(initializer=kaiming) + + paddle.static.nn.prelu(x, 'all', param_attr=param_attr) + + def test_type_error(self): + self.assertRaises( + ValueError, self.func_kaiminguniform_initializer_fan_in_zero + ) + + if __name__ == '__main__': paddle.enable_static() unittest.main()