前提・実現したいこと
こちらの解説記事のソースコードを実行しようと試みましたができない
発生している問題・エラーメッセージ
positional encodingの部分でエラーが起きているっぽい
Traceback (most recent call last): File "C:../..", line 245, in <module> tmp_model = make_model(10, 10, 2) File "C:../..", line 228, in make_model position = PositionalEncoding(d_model, dropout) File "C:../..", line 212, in __init__ pe[:, 0::2] = torch.sin(position * div_term) RuntimeError: expected device cpu and dtype Float but got device cpu and dtype Long
該当のソースコード
python
1class PositionalEncoding(nn.Module): 2 3 def __init__(self, d_model, dropout, max_len=5000): 4 super(PositionalEncoding, self).__init__() 5 self.dropout = nn.Dropout(p=dropout) 6 7 8 pe = torch.zeros(max_len, d_model) 9 position = torch.arange(0, max_len).unsqueeze(1) 10 div_term = torch.exp(torch.arange(0., d_model, 2) * 11 -(math.log(10000.0) / d_model)) 12 pe[:, 0::2] = torch.sin(position * div_term) 13 pe[:, 1::2] = torch.cos(position * div_term) 14 pe = pe.unsqueeze(0) 15 self.register_buffer('pe', pe) 16 17 def forward(self, x): 18 x = x + Variable(self.pe[:, :x.size(1)], 19 requires_grad=False) 20 return self.dropout(x) 21 22def make_model(src_vocab, tgt_vocab, N=6, 23 d_model=512, d_ff=2048, h=8, dropout=0.1): 24 "Helper: Construct a model from hyperparameters." 25 c = copy.deepcopy 26 attn = MultiHeadedAttention(h, d_model) 27 ff = PositionwiseFeedForward(d_model, d_ff, dropout) 28 position = PositionalEncoding(d_model, dropout) 29 model = EncoderDecoder( 30 Encoder(EncoderLayer(d_model, c(attn), c(ff), dropout), N), 31 Decoder(DecoderLayer(d_model, c(attn), c(attn), 32 c(ff), dropout), N), 33 nn.Sequential(Embeddings(d_model, src_vocab), c(position)), 34 nn.Sequential(Embeddings(d_model, tgt_vocab), c(position)), 35 Generator(d_model, tgt_vocab)) 36 37 # This was important from their code. 38 # Initialize parameters with Glorot / fan_avg. 39 for p in model.parameters(): 40 if p.dim() > 1: 41 nn.init.xavier_uniform(p) 42 return model 43 44# Small example model. 45tmp_model = make_model(10, 10, 2)
試したこと
解説記事のプログラムをそのまま実行すると別のエラー(RuntimeError: exp_vml_cpu not implemented for 'Long')が出ていたので
Traceback (most recent call last): File "C:../..", line 245, in <module> tmp_model = make_model(10, 10, 2) File "C:../..", line 228, in make_model position = PositionalEncoding(d_model, dropout) File "C:../..", line 211, in __init__ -(math.log(10000.0) / d_model)) RuntimeError: exp_vml_cpu not implemented for 'Long'
こちらを参考に0に小数点を付けました
補足情報
python 3.5.4
torch 1.1.0