Repository: OpenNMT/OpenNMT-py

Issue:
A small mistake in translator.py
This came when running the German->English model:
iwslt-brnn2.s131_acc_62.71_ppl_7.74_e20.pt

/content/OpenNMT-py/onmt/translate/translation.py:48: UserWarning: invalid index of a 0-dim tensor. This will be an error in PyTorch 0.5. Use tensor.item() to convert a 0-dim tensor to a Python number
tokens[i] = src_raw[max_index[0]]



Discussion:

Resolving patch:
diff --git a/onmt/modules/global_attention.py b/onmt/modules/global_attention.py
--- a/onmt/modules/global_attention.py
+++ b/onmt/modules/global_attention.py
@@ -131,7 +131,7 @@ def score(self, h_t, h_s):
             uh = uh.expand(src_batch, tgt_len, src_len, dim)
 
             # (batch, t_len, s_len, d)
-            wquh = F.tanh(wq + uh)
+            wquh = torch.tanh(wq + uh)
 
             return self.v(wquh.view(-1, dim)).view(tgt_batch, tgt_len, src_len)
 
@@ -172,7 +172,7 @@ def forward(self, source, memory_bank, memory_lengths=None, coverage=None):
         if coverage is not None:
             cover = coverage.view(-1).unsqueeze(1)
             memory_bank += self.linear_cover(cover).view_as(memory_bank)
-            memory_bank = F.tanh(memory_bank)
+            memory_bank = torch.tanh(memory_bank)
 
         # compute attention scores, as in Luong et al.
         align = self.score(source, memory_bank)
@@ -197,7 +197,7 @@ def forward(self, source, memory_bank, memory_lengths=None, coverage=None):
         concat_c = torch.cat([c, source], 2).view(batch*target_l, dim*2)
         attn_h = self.linear_out(concat_c).view(batch, target_l, dim)
         if self.attn_type in ["general", "dot"]:
-            attn_h = F.tanh(attn_h)
+            attn_h = torch.tanh(attn_h)
 
         if one_step:
             attn_h = attn_h.squeeze(1)
diff --git a/onmt/translate/translation.py b/onmt/translate/translation.py
--- a/onmt/translate/translation.py
+++ b/onmt/translate/translation.py
@@ -45,7 +45,7 @@ def _build_target_tokens(self, src, src_vocab, src_raw, pred, attn):
             for i in range(len(tokens)):
                 if tokens[i] == vocab.itos[inputters.UNK]:
                     _, max_index = attn[i].max(0)
-                    tokens[i] = src_raw[max_index[0]]
+                    tokens[i] = src_raw[max_index.item()]
         return tokens
 
     def from_batch(self, translation_batch):
diff --git a/onmt/utils/loss.py b/onmt/utils/loss.py
--- a/onmt/utils/loss.py
+++ b/onmt/utils/loss.py
@@ -201,7 +201,7 @@ def forward(self, output, target):
         model_prob.scatter_(1, target.unsqueeze(1), self.confidence)
         model_prob.masked_fill_((target == self.padding_idx).unsqueeze(1), 0)
 
-        return F.kl_div(output, model_prob, size_average=False)
+        return F.kl_div(output, model_prob, reduction='sum')
 
 
 class NMTLossCompute(LossComputeBase):
@@ -223,7 +223,7 @@ def __init__(self, generator, tgt_vocab, normalization="sents",
             )
         else:
             self.criterion = nn.NLLLoss(
-                ignore_index=self.padding_idx, size_average=False
+                ignore_index=self.padding_idx, reduction='sum'
             )
 
     def _make_shard_state(self, batch, output, range_, attns=None):


Test patch: