Numpy operations on PyTorch Tensor

Dear fellow members,

I have accidentally discovered that some numpy operations, e.g., exp and argmax, work normally on PyTorch tensor.
For example:

>>> a = torch.randn((3,3))
>>> a
tensor([[ 1.1106,  1.3285, -1.7963],
        [ 0.1306,  0.1929, -0.6298],
        [-0.2770,  0.4762, -0.9054]])
>>> np.exp(a)
tensor([[ 3.0361,  3.7752,  0.1659],
        [ 1.1395,  1.2127,  0.5327],
        [ 0.7581,  1.6100,  0.4044]])
>>> np.argmax(a, axis=1)
tensor([ 1,  1,  1])
>>> np.argmax(a, axis=0)
tensor([ 0,  0,  1])

Of course they cannot be used to track their computational graph, but Is it expected behavior that I did not know?

Thanks!

It is sort of expected:

  • Numpy is very liberal in accepting inputs (you can pass lists to most functions, too),
  • PyTorch tries to play nicely by providing inferfaces like __array__ (e.g. sometimes - but not always - you can pass Tensors to matplotlib functions).

Best regards

Thomas

Thank you so much!
It makes me easy to do some post-processing like calculating performance metrics, plotting, etc.
How did I miss it! :slight_smile: