From 2c55568c4014605af60033fc5420132307a372c2 Mon Sep 17 00:00:00 2001 From: VictorSanh Date: Sat, 3 Nov 2018 10:27:38 -0400 Subject: [PATCH] `scatter_` and `scatter` --- modeling_pytorch.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modeling_pytorch.py b/modeling_pytorch.py index 1e25a978b3..9c79ebd60e 100644 --- a/modeling_pytorch.py +++ b/modeling_pytorch.py @@ -502,7 +502,7 @@ class BertForQuestionAnswering(nn.Module): def compute_loss(logits, positions): max_position = positions.max().item() one_hot = torch.FloatTensor(batch_size, max(max_position, seq_length) +1).zero_() - one_hot = one_hot.scatter(1, positions.cpu(), 1) # Second argument need to be LongTensor and not cuda.LongTensor + one_hot = one_hot.scatter_(1, positions.cpu(), 1) # Second argument need to be LongTensor and not cuda.LongTensor one_hot = one_hot[:, :seq_length].to(input_ids.device) log_probs = nn.functional.log_softmax(logits, dim = -1).view(batch_size, seq_length) loss = -torch.mean(torch.sum(one_hot*log_probs), dim = -1)