Spaces:
Sleeping
Sleeping
Download DI-engine/ding/torch_utils/network/normalization.py from OpenDILabCommunity/gomoku: direct link, hf CLI and curl.
- Browser
- Download file 1.36 kB
-
https://huggingface.co/spaces/OpenDILabCommunity/gomoku/resolve/main/DI-engine/ding/torch_utils/network/normalization.py
- Command line
-
hf download hf://spaces/OpenDILabCommunity/gomoku/DI-engine/ding/torch_utils/network/normalization.py
-
curl -L -o normalization.py https://huggingface.co/spaces/OpenDILabCommunity/gomoku/resolve/main/DI-engine/ding/torch_utils/network/normalization.py
1.36 kB
| from typing import Optional | |
| import torch.nn as nn | |
| def build_normalization(norm_type: str, dim: Optional[int] = None) -> nn.Module: | |
| """ | |
| Overview: | |
| Construct the corresponding normalization module. For beginners, | |
| refer to [this article](https://zhuanlan.zhihu.com/p/34879333) to learn more about batch normalization. | |
| Arguments: | |
| - norm_type (:obj:`str`): Type of the normalization. Currently supports ['BN', 'LN', 'IN', 'SyncBN']. | |
| - dim (:obj:`Optional[int]`): Dimension of the normalization, applicable when norm_type is in ['BN', 'IN']. | |
| Returns: | |
| - norm_func (:obj:`nn.Module`): The corresponding batch normalization function. | |
| """ | |
| if dim is None: | |
| key = norm_type | |
| else: | |
| if norm_type in ['BN', 'IN']: | |
| key = norm_type + str(dim) | |
| elif norm_type in ['LN', 'SyncBN']: | |
| key = norm_type | |
| else: | |
| raise NotImplementedError("not support indicated dim when creates {}".format(norm_type)) | |
| norm_func = { | |
| 'BN1': nn.BatchNorm1d, | |
| 'BN2': nn.BatchNorm2d, | |
| 'LN': nn.LayerNorm, | |
| 'IN1': nn.InstanceNorm1d, | |
| 'IN2': nn.InstanceNorm2d, | |
| 'SyncBN': nn.SyncBatchNorm, | |
| } | |
| if key in norm_func.keys(): | |
| return norm_func[key] | |
| else: | |
| raise KeyError("invalid norm type: {}".format(key)) | |