Np.pi equivalent in Pytorch

Hi all,

Could someone tell the np.pi equivalent in Pytorch? Is there any built-in way to access pi value from torch library? Thanks in advance.

4 Likes

No there isn’t a pretty built-in way.

There are however a lot of ugly ways of doing so. You can, for instance, define torch.pi at run-time:

import torch
torch.pi = torch.acos(torch.zeros(1)).item() * 2 # which is 3.1415927410125732

You can even modify the __init__.py file (which is located in print(torch.__file__)) in your environment by adding a line with pi = 3.14 etc, so that you will always be able to use the torch.pi constant.

8 Likes

Thank for your response.

2 Likes

Hi! Just stumbled across this. Why would you do this instead of torch.tensor(math.pi). Is it because OP said “built-in”? Is there any other practical advantage to your approach?

3 Likes