Torch.nn.Transformer Example Code Throwing Tensor Shape Errors
I was trying to implement a Transformer model with Pytorch and was experimenting with the example from this GitHub repo, which was linked from here in the documentation, and ran into a problem within the PositionalEncoding class, found within model.py.

The code for the class's __init__() function is as follows:

def __init__(self, d_model, dropout=0.1, max_len=5000):
    super(PositionalEncoding, self).__init__()
    self.dropout = nn.Dropout(p=dropout)

    pe = torch.zeros(max_len, d_model)
    position = torch.arange(0, max_len, dtype=torch.float).unsqueeze(1)
    div_term = torch.exp(torch.arange(0, d_model, 2).float() * (-math.log(10000.0) / d_model))
    pe[:, 0::2] = torch.sin(position * div_term)
    pe[:, 1::2] = torch.cos(position * div_term)
    pe = pe.unsqueeze(0).transpose(0, 1)
    self.register_buffer('pe', pe)


This code, ran with d_model = 103, threw the following error on the forth last line (pe[:, 0::2] =...):

RuntimeError: The expanded size of the tensor (52) must match the existing size (51) at non-singleton dimension 1.  Target sizes: [5000, 52].  Tensor sizes: [5000, 51]

I've found this error fairly impenetrable and haven't had much success writing my own similarly effective implementation. 
My first guess would be that this is a problem with version changes within Python/PyTorch, but it could of course be something else I'm missing.

Try

0:51:2 instead of 0::2



  0::2 will generate this -> [0,2,4,...,until the end of Elements]

