Equivalent of x.sum().item() in torch 0.3


I am working on someone’s code in torch 0.3.
I want to count number of padding so I wrote:

            ys = valid_decoder_inputs[1:,:].view(-1) #true target 
            not_padding = ys.ne(PAD_IDX)
            no_not_padding = not_padding.sum().item()

However torch 0.3 doesn’t support item(). Can you help of how to write this line of code in torch 0.3?

You could replace it with x.sum().data[0].
The probably better approach would be to port the code to the latest PyTorch version, as you might encounter already solved bugs etc.