如何在Colab上降级Python和Torch?

huangapple go评论74阅读模式
英文:

How to downgrade python and torch on Colab?

问题

我正在尝试在Colab上复现https://github.com/sanghyun-son/EDSR-PyTorch上的研究。为了做到这一点,需要使用Python 3.6和torch 1.2.0。

我已成功将Python降级为3.6。

!python --version
#首先安装Python 3.6
!sudo apt-get update -y
!sudo apt-get install python3.6
#更改替代版本
!sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.6 1
#选择Python版本
!sudo update-alternatives --config python3
#检查Python版本
!python --version
#为新的Python安装pip
!sudo apt-get install python3.6-distutils
!wget https://bootstrap.pypa.io/get-pip.py
!python get-pip.py
#升级pip
!sudo apt install python3-pip
!python -m pip install --upgrade pip
!python --version
!pip --version

我已卸载了PyTorch并下载安装了1.2版本的torch。

!wget https://download.pytorch.org/whl/cpu/torch-1.2.0%2Bcpu-cp36-cp36m-manylinux1_x86_64.whl
!pip uninstall torch -y
!pip install torch-1.2.0+cpu-cp36-cp36m-manylinux1_x86_64.whl

然而,当我尝试导入torch时,出现了“No module named 'torch'”错误。

import torch
print(torch.__version__)

如何在Colab上降级Python和Torch?

为什么会发生这种情况有任何想法吗?

英文:

I'm trying to reproduce the study from https://github.com/sanghyun-son/EDSR-PyTorch on colab. To do that, it is necessary to use Python 3.6 and torch 1.2.0

I've successfully downgraded Python to 3.6.

!python --version
# first install python 3.6
!sudo apt-get update -y
!sudo apt-get install python3.6
# change alternatives
!sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.6 1
# select python version
!sudo update-alternatives --config python3
# check python version
!python --version
# install pip for new python
!sudo apt-get install python3.6-distutils
!wget https://bootstrap.pypa.io/get-pip.py
!python get-pip.py
# upgrade pip
!sudo apt install python3-pip
!python -m pip install --upgrade pip
!python --version
!pip --version

I've uninstall pytorch and download and install the 1.2 torch version

!wget https://download.pytorch.org/whl/cpu/torch-1.2.0%2Bcpu-cp36-cp36m-manylinux1_x86_64.whl
!pip uninstall torch -y
!pip install torch-1.2.0+cpu-cp36-cp36m-manylinux1_x86_64.whl

However, when I try to import torch, the "No module named 'torch'" error occurred

import torch
print(torch.__version__)

如何在Colab上降级Python和Torch?

Any idea why this is happening?

答案1

得分: 1

TLDR: 这是不可能的,但有一种解决方法。

无法降级/升级Colab的Python版本:
即使你在虚拟机上安装了不同版本的Python,运行这个Colab的进程仍然由原始的Python处理。重新启动后端将使虚拟机使用新的Python,但将缺少运行Colab后端所需的所有依赖项。你可能会尝试手动安装它们,但这相当不实际,而且可能会对运行时产生持久性的副作用。

解决方法: Colab可以连接到你的本地运行时环境。创建一个虚拟环境,从中运行一个本地的Jupyter后端,然后将其与Colab连接起来。这样,你就可以完全控制环境,而且还可以享受Colab的所有优点(除了免费的GPU/TPU)。

英文:

TLDR: it is not possible, but there is a workaround.

It is not possible to downgrade/upgrade the colab Python:
Even though you installed a different python on the VM, the process running this colab is still handled by the original Python. Restarting the backend will make the VM to use the new python, but it will miss all the dependencies necessary to run the colab backend. You might have some luck installing them by hands, but it's rather impractical and is likely to have lasting side effects on the runtime.

Workaround: Colab can connect your local runtime. Create a virtual environment, run a local jupyter backend from it, connect it with colab. This way, you have full control over the environment plus all the perks of a Colab (except free GPU/TPU).

huangapple
  • 本文由 发表于 2023年7月13日 22:44:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/76680683.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定