From 9ad0716cbe2e3472c5a86f7076071f8e4ae1588a Mon Sep 17 00:00:00 2001 From: Moony Chou Date: Sat, 26 Jan 2019 23:21:03 +0800 Subject: [PATCH] Fix non-matching tensor with odd embedding size. --- bert_pytorch/model/embedding/position.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/bert_pytorch/model/embedding/position.py b/bert_pytorch/model/embedding/position.py index d55c224..8f550e7 100644 --- a/bert_pytorch/model/embedding/position.py +++ b/bert_pytorch/model/embedding/position.py @@ -14,9 +14,10 @@ def __init__(self, d_model, max_len=512): position = torch.arange(0, max_len).float().unsqueeze(1) div_term = (torch.arange(0, d_model, 2).float() * -(math.log(10000.0) / d_model)).exp() + odd_len = d_model - div_term.size(-1) pe[:, 0::2] = torch.sin(position * div_term) - pe[:, 1::2] = torch.cos(position * div_term) + pe[:, 1::2] = torch.cos(position * div_term[:odd_len]) pe = pe.unsqueeze(0) self.register_buffer('pe', pe)