codertimo / BERT-pytorch

Google AI 2018 BERT pytorch implementation
Apache License 2.0
6.18k stars 1.3k forks source link

embedding/position.py with RuntimeError if d_model is odd #78

Open bookong22 opened 4 years ago

bookong22 commented 4 years ago

such as if set d_model = 29, max_len = 10000 RuntimeError: The expanded size of the tensor (14) must match the existing size (15) at non-singleton dimension 1. Target sizes: [10000, 14]. Tensor sizes: [10000, 15] code in this line may be fixed (line 30): pe[:, 1::2] = torch.cos(position * div_term)

bookong22 commented 4 years ago

modify pe[:, 1::2] = torch.cos(position div_term) to pe[:, 1::2] = torch.cos(position div_term)[:, :d_model//2]

cheng940929 commented 2 years ago

d_model should be a even number