Sometimes it’s necessary to downgrade packages in google colab to restore compatibility with an older version (e.g. pytorch). One way of doing this is using conda within colab and a few tricks make this work. Here is the colab notebook:
https://colab.research.google.com/drive/1W5UnflB8m1zo6lP1y11V78QczMRBI0FE?usp=sharing
Here a quick code gist:
#Check Python version of colab - we need to match this in conda:
!python --version
%%bash
MINICONDA_INSTALLER_SCRIPT=Miniconda3-py37_4.10.3-Linux-x86_64.sh
MINICONDA_PREFIX=/usr/local
wget https://repo.continuum.io/miniconda/$MINICONDA_INSTALLER_SCRIPT
chmod +x $MINICONDA_INSTALLER_SCRIPT
./$MINICONDA_INSTALLER_SCRIPT -b -f -p $MINICONDA_PREFIX
!which conda # should return /usr/local/bin/conda
!conda --version # should return 4.10.3
!python --version # should return 3.7.10 but should actually match the one installed in colab, which is currently: 3.7.11
%%bash
conda install --channel defaults conda python=3.7 --yes
conda update --channel defaults --all --yes
!conda --version # now returns 4.11.0
!python --version # now matches with colab :) 3.7.11
!yes | conda install pytorch==1.6.0 torchvision==0.7.0 -c pytorch
import sys
sys.path # dist-packages is colab and site-packages is conda -> problem is that colab packages get loaded before conda
sys.path.insert(0,"/usr/local/lib/python3.7/site-packages")
# So this will now use conda's torch with cuda 10
import torch
print(torch.version.cuda)
0 Comments