# Round Tensor to x decimal places

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?

1 Like

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)
``````
1 Like

Works, but you have to replace ^ with **:

``````n_digits = 3
arr = torch.rand((3,3))
rounded = torch.round(arr * 10**n_digits) / (10**n_digits)
``````
1 Like

`torch.Tensor` objects have a `round()` method that does not take any argument. So you could do this:

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

Hi,

As I understand this rounding function is undifferentiable, is this correct? and if so is there a way to use the round function and still allow for backprop?

Thanks

No, the rounding function is a step function and almost everywhere differentiable as explained in this post.
However, since it’s a step function the gradient will be zero almost everywhere, so it’s not really usable in training the model.