I get the same results when using either the python sum or torch.sum so why did torch implement a sum function? Is there a difference between them?

2 Answers

Separius On Best Solutions

nothing, torch.sum calls tensor.sum and python's sum calls __add__ (or __radd__ when needed) which calls tensor.sum again

so the only difference is in the number of function calls, and tensor.sum() should be the fastest (when you have small tensors and the function call's overhead is considerable)

Community On

It appears python's sum can take generators as input, whereas torch.sum cannot:

import torch
print( sum( torch.ones(1)*k for k in torch.arange(10)))
returns tensor([45.]), whereas:

print( torch.sum( torch.ones(1)*k for k in torch.arange(10)))
raises TypeError: sum(): argument 'input' (position 1) must be Tensor, not generator

I'm assuming that pyTorch's backpropagation would get in trouble with lazy evaluation of the generator, but not sure about that, yet.