Pytorch errors when given numpy integer types only in python 3 (not in python 2)
For example, the torch.randn function, among others, gets mad when given a numpy.int64 type:

Python 3.5.5 |Anaconda custom (64-bit)| (default, Mar 12 2018, 23:12:44) 
[GCC 7.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
&gt;&gt;&gt; import torch
&gt;&gt;&gt; import numpy
&gt;&gt;&gt; torch.randn(numpy.int64(4))
Traceback (most recent call last):
  File "&lt;stdin&gt;", line 1, in &lt;module&gt;
TypeError: torch.randn received an invalid combination of arguments - got (numpy.int64), but expected one of:
 * (int ... size)
      didn't match because some of the arguments have invalid types: (numpy.int64)
 * (torch.Size size)
      didn't match because some of the arguments have invalid types: (numpy.int64)
 * (torch.Generator generator, int ... size)
 * (torch.Generator generator, torch.Size size)


But in python 2, this works just fine:

Python 2.7.14 |Anaconda, Inc.| (default, Dec  7 2017, 17:05:42) 
[GCC 7.2.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
&gt;&gt;&gt; import torch
&gt;&gt;&gt; import numpy
&gt;&gt;&gt; torch.randn(numpy.int64(3))

-2.0513
 0.5409
-0.0814
[torch.FloatTensor of size 3]


I couldn't find anyone else running into this issue. Is this known? Is this something about my setup? Is there any way to work around this without completely dropping numpy?

I'm using version 0.3.1 of pytorch and version 1.14.2 of numpy.

On Python 2, on an OS where a C long is 64-bit, numpy.int64 is a subclass of int, so most things that want ints will accept numpy.int64 even if they're not written to handle int-like types.

On Python 3, that doesn't happen any more. If you need to use a library that wants real ints, call int:

torch.randn(int(some_numpy_integer))

