From https://github.com/pytorch/fairseq/blob/master/fairseq/models/lstm.py#L448
def Embedding(num_embeddings, embedding_dim, padding_idx):
m = nn.Embedding(num_embeddings, embedding_dim, padding_idx=padding_idx)
nn.init.uniform_(m.weight, -0.1, 0.1)
nn.init.constant_(m.weight[padding_idx], 0)
return m
What are the nn.init.*
lines of the function doing?
On the surface it looks like the Embedding
function in Fairseq is trying to override the default nn.Embedding
, but Pythonically it doesn’t look like the m object is changed.
But why is there a need to first initialize with uniform distribution then constant?
Looking at the code, it looks like without the : https://github.com/pytorch/pytorch/blob/master/torch/nn/init.py#L50
def uniform_(tensor, a=0, b=1):
r"""Fills the input Tensor with values drawn from the uniform
distribution :math:`\mathcal{U}(a, b)`.
Args:
tensor: an n-dimensional `torch.Tensor`
a: the lower bound of the uniform distribution
b: the upper bound of the uniform distribution
Examples:
>>> w = torch.empty(3, 5)
>>> nn.init.uniform_(w)
"""
with torch.no_grad():
return tensor.uniform_(a, b)
def constant_(tensor, val):
r"""Fills the input Tensor with the value :math:`\text{val}`.
Args:
tensor: an n-dimensional `torch.Tensor`
val: the value to fill the tensor with
Examples:
>>> w = torch.empty(3, 5)
>>> nn.init.constant_(w, 0.3)
"""
with torch.no_grad():
return tensor.fill_(val)