To change the printing precision of numpy array x, I've been using this:
with np.printoptions(precision=2, suppress=True):
print(x)
I want to do the same for torch tensor aTensor, but the below doesn't work (I get AttributeError: __enter__ ):
with torch.set_printoptions(precision=2):
print(aTensor)
I'm new to python and looked around and read that for a function to be used in a context manager, it needs to have __enter__ and __exit__. But when I tried to check, I get that neither np.printoptions nor torch.set_printoptions has __enter__ as an attribute: hasattr(np.printoptions, "__enter__") returns False and the same for hasattr(torch.set_printoptions, "__enter__").
But the former can be used in the context manager but the latter not. Why is this?
The direct problem of torch tensors print precision I can deal with by changing precision, printing and then changing precision back. I'm more interested in learning about the basics of the context manager. Thanks in advance.