r""" Fills the input `Tensor` with values according to the method described in `Understanding the difficulty of training deep feedforward neural networks` - Glorot, X. & Bengio, Y. (2010), using a normal distribution. The resulting tensor will have values sampled from :math:`\mat
(tensor, gain=1.0, *, data_format="NCHW")
| 115 | |
| 116 | |
| 117 | def xavier_normal_(tensor, gain=1.0, *, data_format="NCHW"): |
| 118 | r""" |
| 119 | Fills the input `Tensor` with values according to the method |
| 120 | described in `Understanding the difficulty of training deep feedforward |
| 121 | neural networks` - Glorot, X. & Bengio, Y. (2010), using a normal |
| 122 | distribution. The resulting tensor will have values sampled from |
| 123 | :math:`\mathcal{N}(0, \text{std}^2)` where |
| 124 | |
| 125 | .. math:: |
| 126 | \text{std} = \text{gain} \times \sqrt{\frac{2}{\text{fan_in} + \text{fan_out}}} |
| 127 | |
| 128 | The interface is consistent with PyTorch. |
| 129 | The documentation is referenced from: https://pytorch.org/docs/1.10/nn.init.html. |
| 130 | |
| 131 | Also known as Glorot initialization. |
| 132 | |
| 133 | Args: |
| 134 | tensor: an n-dimensional `oneflow.Tensor` |
| 135 | gain: an optional scaling factor |
| 136 | |
| 137 | Examples: |
| 138 | >>> w = flow.empty(3, 5) |
| 139 | >>> nn.init.xavier_normal_(w) |
| 140 | """ |
| 141 | if os.getenv("ONEFLOW_ENABLE_NHWC") == "1": |
| 142 | data_format = "NHWC" |
| 143 | fan = calc_fan(tensor.shape, "fan_sum", get_data_format(data_format)) |
| 144 | std = gain * math.sqrt(2.0 / fan) |
| 145 | return normal_(tensor, 0.0, std) |
| 146 | |
| 147 | |
| 148 | def orthogonal_(tensor, gain=1.0): |
nothing calls this directly
no test coverage detected