Python torch.nn.init._calculate_fan_in_and_fan_out() Examples
The following are 15
code examples of torch.nn.init._calculate_fan_in_and_fan_out().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
torch.nn.init
, or try the search function
.
Example #1
Source File: initialization.py From joeynmt with Apache License 2.0 | 6 votes |
def xavier_uniform_n_(w: Tensor, gain: float = 1., n: int = 4) -> None: """ Xavier initializer for parameters that combine multiple matrices in one parameter for efficiency. This is e.g. used for GRU and LSTM parameters, where e.g. all gates are computed at the same time by 1 big matrix. :param w: parameter :param gain: default 1 :param n: default 4 """ with torch.no_grad(): fan_in, fan_out = _calculate_fan_in_and_fan_out(w) assert fan_out % n == 0, "fan_out should be divisible by n" fan_out //= n std = gain * math.sqrt(2.0 / (fan_in + fan_out)) a = math.sqrt(3.0) * std nn.init.uniform_(w, -a, a) # pylint: disable=too-many-branches
Example #2
Source File: util.py From interpretable_predictions with MIT License | 6 votes |
def xavier_uniform_n_(w, gain=1., n=4): """ Xavier initializer for parameters that combine multiple matrices in one parameter for efficiency. This is e.g. used for GRU and LSTM parameters, where e.g. all gates are computed at the same time by 1 big matrix. :param w: :param gain: :param n: :return: """ with torch.no_grad(): fan_in, fan_out = _calculate_fan_in_and_fan_out(w) assert fan_out % n == 0, "fan_out should be divisible by n" fan_out = fan_out // n std = gain * math.sqrt(2.0 / (fan_in + fan_out)) a = math.sqrt(3.0) * std nn.init.uniform_(w, -a, a)
Example #3
Source File: util.py From interpretable_predictions with MIT License | 6 votes |
def xavier_uniform_n_(w, gain=1., n=4): """ Xavier initializer for parameters that combine multiple matrices in one parameter for efficiency. This is e.g. used for GRU and LSTM parameters, where e.g. all gates are computed at the same time by 1 big matrix. :param w: :param gain: :param n: :return: """ with torch.no_grad(): fan_in, fan_out = _calculate_fan_in_and_fan_out(w) assert fan_out % n == 0, "fan_out should be divisible by n" fan_out = fan_out // n std = gain * math.sqrt(2.0 / (fan_in + fan_out)) a = math.sqrt(3.0) * std nn.init.uniform_(w, -a, a)
Example #4
Source File: lipschitz.py From residual-flows with MIT License | 5 votes |
def reset_parameters(self): init.kaiming_uniform_(self.weight, a=math.sqrt(5)) if self.bias is not None: fan_in, _ = init._calculate_fan_in_and_fan_out(self.weight) bound = 1 / math.sqrt(fan_in) init.uniform_(self.bias, -bound, bound)
Example #5
Source File: lipschitz.py From residual-flows with MIT License | 5 votes |
def reset_parameters(self): init.kaiming_uniform_(self.weight, a=math.sqrt(5)) if self.bias is not None: fan_in, _ = init._calculate_fan_in_and_fan_out(self.weight) bound = 1 / math.sqrt(fan_in) init.uniform_(self.bias, -bound, bound)
Example #6
Source File: mixed_lipschitz.py From residual-flows with MIT License | 5 votes |
def reset_parameters(self, zero_init=False): init.kaiming_uniform_(self.weight, a=math.sqrt(5)) if zero_init: # normalize cannot handle zero weight in some cases. self.weight.data.div_(1000) if self.bias is not None: fan_in, _ = init._calculate_fan_in_and_fan_out(self.weight) bound = 1 / math.sqrt(fan_in) init.uniform_(self.bias, -bound, bound)
Example #7
Source File: mixed_lipschitz.py From residual-flows with MIT License | 5 votes |
def reset_parameters(self): init.kaiming_uniform_(self.weight, a=math.sqrt(5)) if self.bias is not None: fan_in, _ = init._calculate_fan_in_and_fan_out(self.weight) bound = 1 / math.sqrt(fan_in) init.uniform_(self.bias, -bound, bound)
Example #8
Source File: deform_conv2d.py From ASFF with GNU General Public License v3.0 | 5 votes |
def reset_parameters(self): n = self.in_channels init.kaiming_uniform_(self.weight, a=math.sqrt(5)) if self.bias is not None: fan_in, _ = init._calculate_fan_in_and_fan_out(self.weight) bound = 1 / math.sqrt(fan_in) init.uniform_(self.bias, -bound, bound)
Example #9
Source File: modulated_deform_conv2d.py From ASFF with GNU General Public License v3.0 | 5 votes |
def reset_parameters(self): n = self.in_channels init.kaiming_uniform_(self.weight, a=math.sqrt(5)) if self.bias is not None: fan_in, _ = init._calculate_fan_in_and_fan_out(self.weight) bound = 1 / math.sqrt(fan_in) init.uniform_(self.bias, -bound, bound)
Example #10
Source File: deform_conv2d_naive.py From ASFF with GNU General Public License v3.0 | 5 votes |
def reset_parameters(self): n = self.in_channels init.kaiming_uniform_(self.weight, a=math.sqrt(5)) if self.bias is not None: fan_in, _ = init._calculate_fan_in_and_fan_out(self.weight) bound = 1 / math.sqrt(fan_in) init.uniform_(self.bias, -bound, bound)
Example #11
Source File: manifold_layers.py From pvae with MIT License | 5 votes |
def reset_parameters(self): init.kaiming_normal_(self._weight, a=math.sqrt(5)) fan_in, _ = init._calculate_fan_in_and_fan_out(self._weight) bound = 4 / math.sqrt(fan_in) init.uniform_(self._bias, -bound, bound) if self.over_param: with torch.no_grad(): self._bias.set_(self.manifold.expmap0(self._bias))
Example #12
Source File: nn.py From hyperbolic-image-embeddings with MIT License | 5 votes |
def reset_parameters(self): init.kaiming_uniform_(self.weight, a=math.sqrt(5)) if self.bias is not None: fan_in, _ = init._calculate_fan_in_and_fan_out(self.weight) bound = 1 / math.sqrt(fan_in) init.uniform_(self.bias, -bound, bound)
Example #13
Source File: utils.py From MAT with MIT License | 5 votes |
def xavier_normal_small_init_(tensor, gain=1.): # type: (Tensor, float) -> Tensor fan_in, fan_out = _calculate_fan_in_and_fan_out(tensor) std = gain * math.sqrt(2.0 / float(fan_in + 4*fan_out)) return _no_grad_normal_(tensor, 0., std)
Example #14
Source File: utils.py From MAT with MIT License | 5 votes |
def xavier_uniform_small_init_(tensor, gain=1.): # type: (Tensor, float) -> Tensor fan_in, fan_out = _calculate_fan_in_and_fan_out(tensor) std = gain * math.sqrt(2.0 / float(fan_in + 4*fan_out)) a = math.sqrt(3.0) * std # Calculate uniform bounds from standard deviation return _no_grad_uniform_(tensor, -a, a)
Example #15
Source File: utils.py From amdim-public with MIT License | 4 votes |
def init_pytorch_defaults(m, version='041'): ''' Apply default inits from pytorch version 0.4.1 or 1.0.0. pytorch 1.0 default inits are wonky :-( ''' if version == '041': # print('init.pt041: {0:s}'.format(str(m.weight.data.size()))) if isinstance(m, nn.Linear): stdv = 1. / math.sqrt(m.weight.size(1)) m.weight.data.uniform_(-stdv, stdv) if m.bias is not None: m.bias.data.uniform_(-stdv, stdv) elif isinstance(m, nn.Conv2d): n = m.in_channels for k in m.kernel_size: n *= k stdv = 1. / math.sqrt(n) m.weight.data.uniform_(-stdv, stdv) if m.bias is not None: m.bias.data.uniform_(-stdv, stdv) elif isinstance(m, (nn.BatchNorm2d, nn.BatchNorm1d)): if m.affine: m.weight.data.uniform_() m.bias.data.zero_() else: assert False elif version == '100': # print('init.pt100: {0:s}'.format(str(m.weight.data.size()))) if isinstance(m, nn.Linear): init.kaiming_uniform_(m.weight, a=math.sqrt(5)) if m.bias is not None: fan_in, _ = init._calculate_fan_in_and_fan_out(m.weight) bound = 1 / math.sqrt(fan_in) init.uniform_(m.bias, -bound, bound) elif isinstance(m, nn.Conv2d): n = m.in_channels init.kaiming_uniform_(m.weight, a=math.sqrt(5)) if m.bias is not None: fan_in, _ = init._calculate_fan_in_and_fan_out(m.weight) bound = 1 / math.sqrt(fan_in) init.uniform_(m.bias, -bound, bound) elif isinstance(m, (nn.BatchNorm2d, nn.BatchNorm1d)): if m.affine: m.weight.data.uniform_() m.bias.data.zero_() else: assert False elif version == 'custom': # print('init.custom: {0:s}'.format(str(m.weight.data.size()))) if isinstance(m, (nn.BatchNorm2d, nn.BatchNorm1d)): init.normal_(m.weight.data, mean=1, std=0.02) init.constant_(m.bias.data, 0) else: assert False else: assert False