Why Transformers applies Dropout after Positional Encoding?
Not sure what is the benefit of removing 10% of tokens in a sequence by default. Read Why use dropout in positional encoding layer but not clear.
Also does the PyTorch implementation of PositionalEncoding with Dropout may drop [CLS]
or [SEP]
?
Pytorch class PositionalEncoding(nn.Module):
class PositionalEncoding(nn.Module):
def __init__(self, d_model: int, dropout: float = 0.1, max_len: int = 5000):
super().__init__()
self.dropout = nn.Dropout(p=dropout)
position = torch.arange(max_len).unsqueeze(1)
div_term = torch.exp(torch.arange(0, d_model, 2) * (-math.log(10000.0) / d_model))
pe = torch.zeros(max_len, 1, d_model)
pe[:, 0, 0::2] = torch.sin(position * div_term)
pe[:, 0, 1::2] = torch.cos(position * div_term)
self.register_buffer('pe', pe)
def forward(self, x: Tensor) -> Tensor:
"""
Arguments:
x: Tensor, shape ``[seq_len, batch_size, embedding_dim]``
"""
x = x + self.pe[:x.size(0)]
return self.dropout(x) # <---- May drop [CLS] or [SEP]?