MCPcopy Create free account
hub / github.com/MegEngine/MegEngine / calculate_correct_fan

Function calculate_correct_fan

imperative/python/megengine/module/init.py:156–174  ·  view source on GitHub ↗

r"""Calculates fan_in / fan_out value for given weight tensor, depending on given ``mode``. See :func:`calculate_fan_in_and_fan_out` for details. Args: tensor: weight tensor in ``NCHW`` format. mode: fan_in" or "fan_out".

(tensor: Tensor, mode: str)

Source from the content-addressed store, hash-verified

154
155
156def calculate_correct_fan(tensor: Tensor, mode: str) -> float:
157 r"""Calculates fan_in / fan_out value for given weight tensor, depending on given
158 ``mode``.
159
160 See :func:`calculate_fan_in_and_fan_out` for details.
161
162 Args:
163 tensor: weight tensor in ``NCHW`` format.
164 mode: fan_in" or "fan_out".
165 """
166 mode = mode.lower()
167 valid_modes = ["fan_in", "fan_out"]
168 if mode not in valid_modes:
169 raise ValueError(
170 "Mode {} not supported, please use one of {}".format(mode, valid_modes)
171 )
172
173 fan_in, fan_out = calculate_fan_in_and_fan_out(tensor)
174 return fan_in if mode == "fan_in" else fan_out
175
176
177def xavier_uniform_(tensor: Tensor, gain: float = 1.0) -> None:

Callers 2

msra_uniform_Function · 0.85
msra_normal_Function · 0.85

Calls 2

formatMethod · 0.45

Tested by

no test coverage detected