Variable's sum return Variable, is it normal?

 (e) .env  In [1]   from torch.autograd import Variable                                                 │                                                                                                          
                                                                                                          │                                                                                                          
 (e) .env  In [2]   import torch                                                                        │                                                                                                          
                                                                                                          │                                                                                                          
 (e) .env  In [3]   a = Variable(torch.Tensor(1))                                                       │                                                                                                          
                                                                                                                                                                                                                     
 (e) .env  In [4]   a.ne(0)                                                                             │                                                                                                          
            Out[4]                                                                                                                                                                                                  
Variable containing:                                                                                                                                                                                                 
 1                                                                                                        │                                                                                                          
[torch.ByteTensor of size 1]                                                                              │                                                                                                          
                                                                                                          │                                                                                                          
 (e) .env  In [5]   a.ne(0).sum()                                                                       │                                                                                                          
            Out[5]                                                                                       │                                                                                                          
Variable containing:                                                                                      │                                                                                                          
 1                                                                                                        │                                                                                                          
[torch.ByteTensor of size 1]                                                                              │                                                                                                          
                                                                                                                                                                                                                     
 (e) .env  In [6]   a.data.ne(0).sum()                                                                                                                                                                             
            Out[6]   1                                                                                   │                                                                                                          
                                                                                                                                                                                                                     
 (e) .env  In [7]     

However, the document about Tensor says Tensor’s sum return float.

The sum of a Tensor is a float, but a Variable containing a Tensor is not a Tensor. Think of Variable like a wrapper that can hold most numeric objects. Operations performed on a Variable produce the expected result, but wrapped in a Variable.

3 Likes