AWS Torch Import Error on Jupyter

I’m running an instance on AWS of the type p2.xlarge.

Once I have the instance running I connect to it from the command line as follows:

ssh -i YourKeyName.pem ubuntu@X.X.X.X

I configure Jupyter with this:

jupyter notebook --generate-config

Change the IP address config setting for notebooks:

sed -ie "s/#c.NotebookApp.ip = 'localhost'/#c.NotebookApp.ip = '*'/g" ~/.jupyter/jupyter_notebook_config.py

Clone GitHub repo:

git clone https://github.com/NadimKawwa/styletransfer

enter the repo:

https://github.com/NadimKawwa/styletransfer

start jupiter notebook:

jupyter notebook --ip=0.0.0.0 --no-browser

Once I am in i want to import my packages:

# import resources
%matplotlib inline

from PIL import Image
import matplotlib.pyplot as plt
import numpy as np

import torch
import torch.optim as optim
from torchvision import transforms, models

the error i get is:

RuntimeError                              Traceback (most recent call last)
RuntimeError: module compiled against API version 0xb but this version of numpy is 0xa

---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-1-18d2f489897d> in <module>()
      6 import numpy as np
      7 
----> 8 import torch
      9 import torch.optim as optim
     10 from torchvision import transforms, models

/usr/local/lib/python3.5/dist-packages/torch/__init__.py in <module>()
     51 sys.setdlopenflags(_dl_flags.RTLD_GLOBAL | _dl_flags.RTLD_NOW)
     52 
---> 53 from torch._C import *
     54 
     55 __all__ += [name for name in dir(_C)

ImportError: numpy.core.multiarray failed to import

My numpy version is 1.12.2.
I tried to upgrade bumpy but it seems to be already up to date:

~$ pip install numpy --upgrade
Requirement already up-to-date: numpy in /usr/local/lib/python2.7/dist-packages

I have seen a solution from this other post:

However when I attempt to update i get the following error:

ubuntu@ip-X-X-X-X:~$ pip install numpy -I
Collecting numpy
  Using cached https://files.pythonhosted.org/packages/de/37/fe7db552f4507f379d81dcb78e58e05030a8941757b1f664517d581b5553/numpy-1.15.4-cp27-cp27mu-manylinux1_x86_64.whl
Installing collected packages: numpy
Exception:
Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/pip/basecommand.py", line 215, in main
    status = self.run(options, args)
  File "/usr/local/lib/python2.7/dist-packages/pip/commands/install.py", line 342, in run
    prefix=options.prefix_path,
  File "/usr/local/lib/python2.7/dist-packages/pip/req/req_set.py", line 784, in install
    **kwargs
  File "/usr/local/lib/python2.7/dist-packages/pip/req/req_install.py", line 851, in install
    self.move_wheel_files(self.source_dir, root=root, prefix=prefix)
  File "/usr/local/lib/python2.7/dist-packages/pip/req/req_install.py", line 1064, in move_wheel_files
    isolated=self.isolated,
  File "/usr/local/lib/python2.7/dist-packages/pip/wheel.py", line 345, in move_wheel_files
    clobber(source, lib_dir, True)
  File "/usr/local/lib/python2.7/dist-packages/pip/wheel.py", line 323, in clobber
    shutil.copyfile(srcfile, destfile)
  File "/usr/lib/python2.7/shutil.py", line 83, in copyfile
    with open(dst, 'wb') as fdst:
IOError: [Errno 13] Permission denied: '/usr/local/lib/python2.7/dist-packages/numpy/add_newdocs.pyc'

I attempted a sudo install for numpy but the version is unchanged.
Any directions on how to proceed?

EDIT:

I realized i need to update my requirements.txt file to get the most recent numpy version.

opencv-python==3.2.0.6
h5py==2.6.0
matplotlib==2.0.0
numpy==1.15.4
scipy==0.18.1
tqdm==4.11.2
keras==2.0.2
scikit-learn==0.18.1
pillow==4.0.0
ipykernel==4.6.1
tensorflow==1.0.0
torchvision=0.2.1

however i am still getting errors such as:

AttributeError: ‘FloatTensor’ object has no attribute ‘requires_grad’

I guess my main issue with torch right now is figuring out what stable version I can use and how to update my EC2 instance accordingly.

About the numpy issue, check what pip --version says. It has to match to the root of where your which python or python --version is pointing. On some machines pip3 will correspond to the python you are using, rather than pip.

About the 2nd issue, i.e. AttributeError: ‘FloatTensor’ object has no attribute ‘requires_grad’, I think you are on PyTorch v0.3.1 or something old. Can you print out print(torch.__version__) and make sure you are on 1.0.0?

1 Like