-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WIP: Add multi-batchnorm and bias feature
- Loading branch information
Showing
4 changed files
with
95 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
from typing import List, Optional | ||
import copy | ||
import torch | ||
from torch.nn.modules.batchnorm import _NormBase | ||
|
||
|
||
class MultiBatchNorm(torch.nn.Module): | ||
def __init__(self, batch_norm: _NormBase, size: int): | ||
super().__init__() | ||
assert size >= 1, "Need at least one forward dimension" | ||
self._shadow_norms: List[_NormBase] = [ | ||
copy.deepcopy(batch_norm) for _ in range(size) | ||
] | ||
self._mul_idx: int = 0 | ||
self.mul_idx = 0 | ||
self.fwd_norm: Optional[_NormBase] | ||
|
||
def forward(self, x): | ||
return self.fwd_norm(x) | ||
|
||
@property | ||
def mul_idx(self): | ||
return self._mul_idx | ||
|
||
@mul_idx.setter | ||
def mul_idx(self, new_idx: int): | ||
assert new_idx < len( | ||
self._shadow_norms | ||
), f"Bad Index {new_idx} for size {len(self._shadow_norms)}" | ||
self._mul_idx = new_idx | ||
self.fwd_norm = self._shadow_norms[self._mul_idx] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters