How do I install PyTorch v1.0.0+ on Google Colab? - pytorch

PyTorch v1.0.0 stable was released on 8 December 2018 after being announced 7 months earlier.
I want get a version optimised for the hardware that my IPython kernel is running on.
How do I get this version on Google Colab?

try the following code snippet (it works equally for the runtime with or without gpu)
!pip install -q torch==1.0.0 torchvision
to check the version
import torch
print(torch.__version__)
here you have the version 1.0.0
UPDATE
!pip install torch
Works fine now, as the most stable version is 1.0.0

With version 1.0.0, PyTorch changed the download URL format from:
https://download.pytorch.org/whl/cu92/torch-1.0.0-cp36-cp36m-linux_x86_64.whl
to
https://download.pytorch.org/whl/cu90/torch-1.0.0-cp36-cp36m-linux_x86_64.whl
(The change is in the CUDA version part, where cu92 changes to cu90.)
To programmatically generate that URL, I used the following code:
from os.path import exists
from wheel.pep425tags import get_abbr_impl, get_impl_ver, get_abi_tag
platform = '{}{}-{}'.format(get_abbr_impl(), get_impl_ver(), get_abi_tag())
cuda_output = !ldconfig -p|grep cudart.so|sed -e 's/.*\.\([0-9]*\)\.\([0-9]*\)$/cu\10/'
accelerator = cuda_output[0] if exists('/dev/nvidia0') else 'cpu'
torch_url=f"http://download.pytorch.org/whl/{accelerator}/torch-{version}-{platform}-linux_x86_64.whl"
version='1.0.0'
!pip install -U {torch_url} torchvision
You can then change the version variable as desired as newer versions are released.

You can now just
import torch
No need for additional installation.

For version 1.1.0, this works
!pip install -q torch==1.1.0 torchvision

It worked for me you can try
!pip install torch
!pip install torchvision
!pip install mxnet-cu101

Here is a code to install pytorch. You can change it to whatever version you want.
!pip3 install http://download.pytorch.org/whl/cu92/torch-1.0.0-cp36-cp36m-linux_x86_64.whl

Related

the imageai module requires Pytorch to work in Python3.10

I am trying to install imageai in Python3.10. It is installed but when I import the module imageai, it requires installing pytorch and torchvision:
Dependency error!!! PyTorch and TorchVision are not installed. Please see installation instructions in the documentation https://imageai.readthedocs.io/
I installed Pytorch and Torchvision using the below commands in CMD:
pip3.10 install torch pip3.10 install torchvision
Yet I receive the above dependency error when I import imageai. Any idea?
I had the same problem. Try to use the exact versions of the dependencies listed in the instructions of imageai.
I have uninstalled all modules first, then installed with the exact versions and the problem is solved.
I am using python 3.8.5.
I hope this works with you also.

Can't import ToTensorV2 in Colab

from albumentations.pytorch.transforms import ToTensorV2
I used the above code, and it doesn't work.
Just add a code block with the line
! pip install albumentations==0.4.6
above the block where you do the import. I tried installing it without the specific version and it failed.
When i did not specify the version number in pip install, version 0.1.12 was installed which does not contain ToTensorV2.
Ensure that you have the latest version
!pip install --upgrade --force-reinstall --no-deps albumentations
Get albumentations from Github.
Usage on Colab Example
!pip install -U git+https://github.com/albu/albumentations > /dev/null

How to specify pytorch / cuda version in pipenv

I am trying to install a specific version of pytorch that is compatible with a specific cuda driver version with pipenv. The pytorch website shows how to to this with pip:
pip3 install torch==1.3.1+cu92 torchvision==0.4.2+cu92 -f https://download.pytorch.org/whl/torch_stable.html
I tried to convert this into an entry in my Pipfile like this:
[[source]]
name = "pytorch"
url = "https://download.pytorch.org/whl/torch_stable.html"
verify_ssl = false
pytorch = {version="==1.3.1+cu92", index="pytorch"}
torchvision = {version="==0.4.2+cu92", index="pytorch"}
However, this does not work. The dependency with this version can not be resolved. I am not sure if the url that is listed with the -f parameter in the pip3 command is even a valid source for pipenv.
I could install both libraries by just passing the command through to pip like this:
pipenv run pip install torch==1.3.1+cu92 torchvision==0.4.2+cu92 -f https://download.pytorch.org/whl/torch_stable.html
but I am not really satisfied with that solution since the dependencies are not in the Pipfile and I have to manually document the usage of this command.
The problem with the approach above lies in the structure of https://download.pytorch.org/whl/torch_stable.html. Pipenv can only find torch versions 0.1 to 0.4.1 because all others have the cuda (or cpu) version as a prefix e.g. cu92/torch-0.4.1-cp27-cp27m-linux_x86_64.whl.
But the cuda version is a subdirectory. So if you change the url of the source to the cuda version and only specify the torch version in the dependencies it works.
[[source]]
name = "pytorch"
url = "https://download.pytorch.org/whl/cu92"
verify_ssl = false
[packages]
torch = {index = "pytorch",version = "==1.4.0"}
The only problem I encountered is that numpy is not recognized as a dependency of pytoch 1.4.0. But this seems to be a problem of the specific pytorch wheel. With version 1.3.1 or 1.5.1 and a recent pipenv version it works.
So if after the installation with pipenv install, the command pipenv run python -c "import torch" throws an error, numpy must be added manually.
Solved: This is how to install Torch CORRECTLY in Pipenv. Most people do this incorrectly even in various "solutions" I've seen from other people... I've researched to the bottom of this problem and found out the real, intended solution that everyone should use.
This technique is the correct one and it's documented here in the pipenv repository:
https://github.com/pypa/pipenv/issues/4961#issuecomment-1045679643
I've just run into this issue right now. Take whichever URL the Torch website tells you to use and do this:
pipenv install torch --index https://download.pytorch.org/whl/cu113
Of course, replace https://download.pytorch.org/whl/cu113 with the URL indicated for you by Torch. This updates the Pipfile correctly and worked in my system. Installed pytorch and enabled correct CUDA support.

How to update Tensorflow on mac?

My current Tensorflow version is 0.10.0. I need to update it to latest version.Latest one is 1.0.0.Is there any method that I can use in
terminal?
This is the python code which I used to find version
import tensorflow as tf;
print(tf.__version__);
If you are using anaconda;
>conda update tensorflow
If you are using pip;
>pip install tensorflow --upgrade
If you want to force install a specific version on conda;
>conda install tensorflow=1.0.0
If you want to force install a specific version on pip;
>pip install 'tensorflow==1.0.0' --force-reinstall
In anaconda cloud, first pick a channel which has the latest version of tensorflow binary. The latest versions are usually available at the channel conda-forge
conda update -f -c conda-forge tensorflow
This will upgrade your installation to the very latest version available. As of this writing version 1.4.0-py36_0

Installing Blocks on Linux

I'm trying to install Blocks on my Linux machine and I have some troubles. How can I fix this? I'm doing the following steps:
Installing Anaconda for python2.
Theano depends on numpy 1.10.1 version. If at this step I will run import theano in python shell then it will work perfectly.
Okay, it's time for Blocks. I'm installing stable version using pip install --user git+git://github.com/mila-udem/blocks.git \
-r https://raw.githubusercontent.com/mila-udem/blocks/master/requirements.txt
After this step if I import theano it gives the following: RuntimeError: module compiled against API version a but this version of numpy is 9.
I looked at requirements.txt and see that Blocks depends on numpy 1.9.3.
I uninstalled Theano, Blocks, downgraded numpy using conda install numpy=1.9.3, then just run again pip install --user git+git://github.com/mila-udem/blocks.git \
-r https://raw.githubusercontent.com/mila-udem/blocks/master/requirements.txt and still while importing theano it gives me RuntimeError: module compiled against API version a but this version of numpy is 9.
How can I overcome this problem?
This could be a problem with the Anaconda distribution. You could try updating all of Anaconda via conda update conda and conda update --all. You could also try changing Blocks requirements.txt to refer to numpy 1.10.1 since it's likely that Blocks won't care about the changes from 1.9 to 1.10.
– Daniel Renshaw

Resources