xavier_uniform_(tensor, gain=1.0)[source]

Initialize weights of the tensor similarly to Glorot/Xavier initialization.

Proceed as if it was a linear layer with fan_in of zero, fan_out of prod(tensor.shape[1:]) and Xavier uniform initialization is used, i.e. fill the weight of input tensor with values sampled from \(\mathcal{U}(-a, a)\) where

\[a = \text{gain} \times \sqrt{\frac{6}{\text{fan_out}}}\]

Example usage:

>>> import torch, pykeen.nn.init
>>> w = torch.empty(3, 5)
>>> pykeen.nn.init.xavier_uniform_(w, gain=torch.nn.init.calculate_gain("relu"))
  • tensor (Tensor) – a tensor to initialize

  • gain (float) – an optional scaling factor, defaults to 1.0.

Return type



tensor with weights by this initializer.