Round Tensor to x decimal places


(Barry Plunkett) #1

Hi,

I’m looking for a function to round the values in a tensor to a specific number of decimal places, i.e. some kind of functionality similar to round(…) in base python. Does something like this exist in PyTorch now? What is the best way to do this without losing all gradient history?


#2

The following should round to 3 digits:

n_digits = 3
arr = torch.rand((3,3))
rounded = torch.round(arr * 10^n_digits) / (10^n_digits)