param_init.py 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. # Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. import paddle.nn as nn
  15. def constant_init(param, **kwargs):
  16. """
  17. Initialize the `param` with constants.
  18. Args:
  19. param (Tensor): Tensor that needs to be initialized.
  20. Examples:
  21. from paddlex.paddleseg.cvlibs import param_init
  22. import paddle.nn as nn
  23. linear = nn.Linear(2, 4)
  24. param_init.constant_init(linear.weight, value=2.0)
  25. print(linear.weight.numpy())
  26. # result is [[2. 2. 2. 2.], [2. 2. 2. 2.]]
  27. """
  28. initializer = nn.initializer.Constant(**kwargs)
  29. initializer(param, param.block)
  30. def normal_init(param, **kwargs):
  31. """
  32. Initialize the `param` with a Normal distribution.
  33. Args:
  34. param (Tensor): Tensor that needs to be initialized.
  35. Examples:
  36. from paddlex.paddleseg.cvlibs import param_init
  37. import paddle.nn as nn
  38. linear = nn.Linear(2, 4)
  39. param_init.normal_init(linear.weight, loc=0.0, scale=1.0)
  40. """
  41. initializer = nn.initializer.Normal(**kwargs)
  42. initializer(param, param.block)
  43. def kaiming_normal_init(param, **kwargs):
  44. r"""
  45. Initialize the input tensor with Kaiming Normal initialization.
  46. This function implements the `param` initialization from the paper
  47. `Delving Deep into Rectifiers: Surpassing Human-Level Performance on
  48. ImageNet Classification <https://arxiv.org/abs/1502.01852>`
  49. by Kaiming He, Xiangyu Zhang, Shaoqing Ren and Jian Sun. This is a
  50. robust initialization method that particularly considers the rectifier
  51. nonlinearities. In case of Uniform distribution, the range is [-x, x], where
  52. .. math::
  53. x = \sqrt{\\frac{6.0}{fan\_in}}
  54. In case of Normal distribution, the mean is 0 and the standard deviation
  55. is
  56. .. math::
  57. \sqrt{\\frac{2.0}{fan\_in}}
  58. Args:
  59. param (Tensor): Tensor that needs to be initialized.
  60. Examples:
  61. from paddlex.paddleseg.cvlibs import param_init
  62. import paddle.nn as nn
  63. linear = nn.Linear(2, 4)
  64. # uniform is used to decide whether to use uniform or normal distribution
  65. param_init.kaiming_normal_init(linear.weight)
  66. """
  67. initializer = nn.initializer.KaimingNormal(**kwargs)
  68. initializer(param, param.block)