r"""Applies general normalization to the input. Refer to :class:`~.GeneralNorm` for more information. Args: inp: input tensor. normalized_axis: the axis that you want to be normalizated See :attr:`normalized_axis` in :class:`~.GeneralNorm`. affine: wheth
(
inp: Tensor,
normalized_axis: tuple,
affine: bool = True,
weight: Optional[Tensor] = None,
bias: Optional[Tensor] = None,
eps: float = 1e-5,
)
| 1154 | |
| 1155 | |
| 1156 | def general_norm( |
| 1157 | inp: Tensor, |
| 1158 | normalized_axis: tuple, |
| 1159 | affine: bool = True, |
| 1160 | weight: Optional[Tensor] = None, |
| 1161 | bias: Optional[Tensor] = None, |
| 1162 | eps: float = 1e-5, |
| 1163 | ): |
| 1164 | r"""Applies general normalization to the input. |
| 1165 | |
| 1166 | Refer to :class:`~.GeneralNorm` for more information. |
| 1167 | |
| 1168 | Args: |
| 1169 | inp: input tensor. |
| 1170 | normalized_axis: the axis that you want to be normalizated |
| 1171 | See :attr:`normalized_axis` in :class:`~.GeneralNorm`. |
| 1172 | affine: whether to use learnable affine parameters (weight, bias) |
| 1173 | weight: scaling tensor in the learnable affine parameters. |
| 1174 | See :math:`\gamma` in :class:`~.GeneralNorm`. |
| 1175 | bias: bias tensor in the learnable affine parameters. |
| 1176 | See :math:`\beta` in :class:`~.GeneralNorm`. |
| 1177 | eps: a value added to the denominator for numerical stability. Default: 1e-5 |
| 1178 | """ |
| 1179 | if not isinstance(normalized_axis, Sequence): |
| 1180 | normalized_axis = [normalized_axis] |
| 1181 | assert isinstance(normalized_axis, (list, tuple)) |
| 1182 | |
| 1183 | assert len(normalized_axis) > 0, "normalization axis not specified" |
| 1184 | |
| 1185 | normalized_axis = [num + inp.ndim if num < 0 else num for num in normalized_axis] |
| 1186 | assert normalized_axis == sorted( |
| 1187 | normalized_axis |
| 1188 | ), "The order of normalized_axis is incorrect, should be {}, but got {}. Please specify the values of axis in the correct order in normalized_axis".format( |
| 1189 | sorted(normalized_axis), normalized_axis |
| 1190 | ) |
| 1191 | assert ( |
| 1192 | normalized_axis[-1] < inp.ndim |
| 1193 | ), "the maximum axis in normalized_axis is greater than inp_shape.ndim" |
| 1194 | assert len(set(normalized_axis)) == len( |
| 1195 | normalized_axis |
| 1196 | ), "there are duplicate axis in normalized_axis" |
| 1197 | |
| 1198 | _reshape = [] |
| 1199 | _rereshape = [] |
| 1200 | _need_reshape = ( |
| 1201 | _builtins_max(normalized_axis) - _builtins_min(normalized_axis) |
| 1202 | ) != (len(normalized_axis) - 1) |
| 1203 | if _need_reshape: |
| 1204 | get_logger().warning( |
| 1205 | "normalized_axis is discontinuous, and performance may be poor" |
| 1206 | ) |
| 1207 | unnormalized_axis = list(set(range(inp.ndim)) - set(normalized_axis)) |
| 1208 | unnormalized_axis.sort() |
| 1209 | _reshape = unnormalized_axis + normalized_axis |
| 1210 | inp = transpose(inp, _reshape) |
| 1211 | |
| 1212 | for i in range(inp.ndim): |
| 1213 | _rereshape.append(_reshape.index(i)) |