# tensorplay.nn.init API Source: https://www.tensorplay.cn/docs/api/tensorplay.nn.init.html ## Functions 15 [#](#api-tensorplay.nn.init.calculate_gain) ### calculate_gain function[Full reference ↗](/docs/generated/tensorplay.nn.init.calculate_gain.html) ```python tensorplay.nn.init.calculate_gain(nonlinearity, param=None) ``` Return the recommended gain value for the given nonlinearity. Supported names: linear/conv{1,2,3}d/conv_transpose{1,2,3}d and sigmoid map to 1, tanh to 5/3, relu to sqrt(2), selu to 3/4, and leaky_relu to sqrt(2 / (1 + negative_slope**2)) where the slope is taken from param (default 0.01). [#](#api-tensorplay.nn.init.constant_) ### constant_ function[Full reference ↗](/docs/generated/tensorplay.nn.init.constant_.html) ```python tensorplay.nn.init.constant_(tensor, val) ``` Fill tensor in-place with the scalar val. [#](#api-tensorplay.nn.init.dirac_) ### dirac_ function[Full reference ↗](/docs/generated/tensorplay.nn.init.dirac_.html) ```python tensorplay.nn.init.dirac_(tensor, groups=1) ``` Fill the {3, 4, 5}-D tensor in-place with Dirac delta kernels. Preserves identity of inputs in convolutional layers; with groups > 1 each group of output channels preserves identity independently. [#](#api-tensorplay.nn.init.eye_) ### eye_ function[Full reference ↗](/docs/generated/tensorplay.nn.init.eye_.html) ```python tensorplay.nn.init.eye_(tensor) ``` Fill the 2-D tensor in-place with the identity matrix. As many inputs as possible are preserved through a Linear layer. [#](#api-tensorplay.nn.init.kaiming_normal_) ### kaiming_normal_ function[Full reference ↗](/docs/generated/tensorplay.nn.init.kaiming_normal_.html) ```python tensorplay.nn.init.kaiming_normal_(tensor, a=0, mode='fan_in', nonlinearity='leaky_relu', generator=None) ``` Fill tensor in-place with N(0, std^2) where std = gain / sqrt(fan). [#](#api-tensorplay.nn.init.kaiming_uniform_) ### kaiming_uniform_ function[Full reference ↗](/docs/generated/tensorplay.nn.init.kaiming_uniform_.html) ```python tensorplay.nn.init.kaiming_uniform_(tensor, a=0, mode='fan_in', nonlinearity='leaky_relu', generator=None) ``` Fill tensor in-place with U(-bound, bound) where bound = gain * sqrt(3 / fan). [#](#api-tensorplay.nn.init.normal_) ### normal_ function[Full reference ↗](/docs/generated/tensorplay.nn.init.normal_.html) ```python tensorplay.nn.init.normal_(tensor, mean=0.0, std=1.0, generator=None) ``` Fill tensor in-place with samples from N(mean, std^2). [#](#api-tensorplay.nn.init.ones_) ### ones_ function[Full reference ↗](/docs/generated/tensorplay.nn.init.ones_.html) ```python tensorplay.nn.init.ones_(tensor) ``` Fill tensor in-place with the scalar value 1. [#](#api-tensorplay.nn.init.orthogonal_) ### orthogonal_ function[Full reference ↗](/docs/generated/tensorplay.nn.init.orthogonal_.html) ```python tensorplay.nn.init.orthogonal_(tensor, gain=1, generator=None) ``` Fill tensor in-place with a (semi) orthogonal matrix. The tensor must have at least 2 dimensions; trailing dimensions are flattened. Rows (or columns, when narrower) are orthonormalized with a QR factorization of a standard-normal sample, and Q is rescaled by the diagonal signs of R so its distribution is uniform over the orthogonal group. [#](#api-tensorplay.nn.init.sparse_) ### sparse_ function[Full reference ↗](/docs/generated/tensorplay.nn.init.sparse_.html) ```python tensorplay.nn.init.sparse_(tensor, sparsity, std=0.01, generator=None) ``` Fill the 2-D tensor in-place as a sparse matrix. Each column gets sparsity * rows zeroed entries (a random row subset per column); the remaining entries come from N(0, std^2). [#](#api-tensorplay.nn.init.trunc_normal_) ### trunc_normal_ function[Full reference ↗](/docs/generated/tensorplay.nn.init.trunc_normal_.html) ```python tensorplay.nn.init.trunc_normal_(tensor, mean=0.0, std=1.0, a=-2.0, b=2.0, generator=None) ``` Fills the input Tensor with values drawn from a truncated normal distribution. Method is based on the rejection-sampling scheme in [https://people.sc.fsu.edu/~jburkardt/presentations/truncated_normal.pdf](https://people.sc.fsu.edu/~jburkardt/presentations/truncated_normal.pdf) — [#](#api-tensorplay.nn.init.uniform_) ### uniform_ function[Full reference ↗](/docs/generated/tensorplay.nn.init.uniform_.html) ```python tensorplay.nn.init.uniform_(tensor, a=0.0, b=1.0, generator=None) ``` Fill tensor in-place with samples from U(a, b). [#](#api-tensorplay.nn.init.xavier_normal_) ### xavier_normal_ function[Full reference ↗](/docs/generated/tensorplay.nn.init.xavier_normal_.html) ```python tensorplay.nn.init.xavier_normal_(tensor, gain=1.0, generator=None) ``` Fill tensor in-place with N(0, std^2) where std = gain * sqrt(2 / (fan_in + fan_out)). [#](#api-tensorplay.nn.init.xavier_uniform_) ### xavier_uniform_ function[Full reference ↗](/docs/generated/tensorplay.nn.init.xavier_uniform_.html) ```python tensorplay.nn.init.xavier_uniform_(tensor, gain=1.0, generator=None) ``` Fill tensor in-place with U(-a, a) where a = gain * sqrt(6 / (fan_in + fan_out)). [#](#api-tensorplay.nn.init.zeros_) ### zeros_ function[Full reference ↗](/docs/generated/tensorplay.nn.init.zeros_.html) ```python tensorplay.nn.init.zeros_(tensor) ``` Fill tensor in-place with the scalar value 0.