LoginSignup
1

More than 3 years have passed since last update.

「動かしながら学ぶPyTorchプログラミング入門」のGoogle Colaboratory環境構築について

Last updated at Posted at 2020-12-05

ごきげんよう、ちゃまおです。

最近、Pythonと機械学習について、色々と勉強中です。機械学習ライブラリはPyTorchにしようと思って、以下の書籍を買ったのですが、Google Colaboratoryで環境構築をする際にエラーが出たので、その内容と原因についてまとめました。

書籍紹介

書籍名:動かしながら学ぶPyTorchプログラミング入門
著者:斎藤 勇哉氏
出版社:オーム社
発売日:2020/11/21

Google Colaboratoryによる環境構築について

(注)2020年12月4日時点での話。

書籍に載っている"著者によるサポートページのコード"を、Google Colaboratoryで実行した所、PyTorchについて、「そのバージョンは見つからないよ」と言う内容のエラーが出ました。

以下は、打ち込んだコードと、その実行結果です。

GoogleColaboratoryに打ち込んだコード
!pip3 install torch==1.6.0+cu101
!pip3 install torchvision==0.7.0+cu101
!pip3 install numpy==1.18.5
!pip3 install matplotlib==3.2.2
!pip3 install scikit-learn==0.23.1
!pip3 install seaborn==0.11.0
実行結果
ERROR: Could not find a version that satisfies the requirement torch==1.6.0+cu101 (from versions: 0.1.2, 0.1.2.post1, 0.1.2.post2, 0.3.1, 0.4.0, 0.4.1, 1.0.0, 1.0.1, 1.0.1.post2, 1.1.0, 1.2.0, 1.3.0, 1.3.1, 1.4.0, 1.5.0, 1.5.1, 1.6.0, 1.7.0)
ERROR: No matching distribution found for torch==1.6.0+cu101
ERROR: Could not find a version that satisfies the requirement torchvision==0.7.0+cu101 (from versions: 0.1.6, 0.1.7, 0.1.8, 0.1.9, 0.2.0, 0.2.1, 0.2.2, 0.2.2.post2, 0.2.2.post3, 0.3.0, 0.4.0, 0.4.1, 0.4.2, 0.5.0, 0.6.0, 0.6.1, 0.7.0, 0.8.0, 0.8.1)
ERROR: No matching distribution found for torchvision==0.7.0+cu101
Requirement already satisfied: numpy==1.18.5 in /usr/local/lib/python3.6/dist-packages (1.18.5)
Requirement already satisfied: matplotlib==3.2.2 in /usr/local/lib/python3.6/dist-packages (3.2.2)
Requirement already satisfied: python-dateutil>=2.1 in /usr/local/lib/python3.6/dist-packages (from matplotlib==3.2.2) (2.8.1)
Requirement already satisfied: numpy>=1.11 in /usr/local/lib/python3.6/dist-packages (from matplotlib==3.2.2) (1.18.5)
Requirement already satisfied: pyparsing!=2.0.4,!=2.1.2,!=2.1.6,>=2.0.1 in /usr/local/lib/python3.6/dist-packages (from matplotlib==3.2.2) (2.4.7)
Requirement already satisfied: kiwisolver>=1.0.1 in /usr/local/lib/python3.6/dist-packages (from matplotlib==3.2.2) (1.3.1)
Requirement already satisfied: cycler>=0.10 in /usr/local/lib/python3.6/dist-packages (from matplotlib==3.2.2) (0.10.0)
Requirement already satisfied: six>=1.5 in /usr/local/lib/python3.6/dist-packages (from python-dateutil>=2.1->matplotlib==3.2.2) (1.15.0)
Collecting scikit-learn==0.23.1
  Downloading https://files.pythonhosted.org/packages/d9/3a/eb8d7bbe28f4787d140bb9df685b7d5bf6115c0e2a969def4027144e98b6/scikit_learn-0.23.1-cp36-cp36m-manylinux1_x86_64.whl (6.8MB)
     |████████████████████████████████| 6.9MB 11.0MB/s 
Requirement already satisfied: numpy>=1.13.3 in /usr/local/lib/python3.6/dist-packages (from scikit-learn==0.23.1) (1.18.5)
Requirement already satisfied: scipy>=0.19.1 in /usr/local/lib/python3.6/dist-packages (from scikit-learn==0.23.1) (1.4.1)
Collecting threadpoolctl>=2.0.0
  Downloading https://files.pythonhosted.org/packages/f7/12/ec3f2e203afa394a149911729357aa48affc59c20e2c1c8297a60f33f133/threadpoolctl-2.1.0-py3-none-any.whl
Requirement already satisfied: joblib>=0.11 in /usr/local/lib/python3.6/dist-packages (from scikit-learn==0.23.1) (0.17.0)
Installing collected packages: threadpoolctl, scikit-learn
  Found existing installation: scikit-learn 0.22.2.post1
    Uninstalling scikit-learn-0.22.2.post1:
      Successfully uninstalled scikit-learn-0.22.2.post1
Successfully installed scikit-learn-0.23.1 threadpoolctl-2.1.0
Requirement already satisfied: seaborn==0.11.0 in /usr/local/lib/python3.6/dist-packages (0.11.0)
Requirement already satisfied: pandas>=0.23 in /usr/local/lib/python3.6/dist-packages (from seaborn==0.11.0) (1.1.4)
Requirement already satisfied: matplotlib>=2.2 in /usr/local/lib/python3.6/dist-packages (from seaborn==0.11.0) (3.2.2)
Requirement already satisfied: scipy>=1.0 in /usr/local/lib/python3.6/dist-packages (from seaborn==0.11.0) (1.4.1)
Requirement already satisfied: numpy>=1.15 in /usr/local/lib/python3.6/dist-packages (from seaborn==0.11.0) (1.18.5)
Requirement already satisfied: pytz>=2017.2 in /usr/local/lib/python3.6/dist-packages (from pandas>=0.23->seaborn==0.11.0) (2018.9)
Requirement already satisfied: python-dateutil>=2.7.3 in /usr/local/lib/python3.6/dist-packages (from pandas>=0.23->seaborn==0.11.0) (2.8.1)
Requirement already satisfied: kiwisolver>=1.0.1 in /usr/local/lib/python3.6/dist-packages (from matplotlib>=2.2->seaborn==0.11.0) (1.3.1)
Requirement already satisfied: pyparsing!=2.0.4,!=2.1.2,!=2.1.6,>=2.0.1 in /usr/local/lib/python3.6/dist-packages (from matplotlib>=2.2->seaborn==0.11.0) (2.4.7)
Requirement already satisfied: cycler>=0.10 in /usr/local/lib/python3.6/dist-packages (from matplotlib>=2.2->seaborn==0.11.0) (0.10.0)
Requirement already satisfied: six>=1.5 in /usr/local/lib/python3.6/dist-packages (from python-dateutil>=2.7.3->pandas>=0.23->seaborn==0.11.0) (1.15.0)

エラー原因について

(結論)Google ColaboratoryにインストールされているPyTorchのバージョンが1.7.0になっていた。

Google Colaboratoryにインストールされているライブラリ一覧から、書籍で用意しようとしているライブラリのバージョンについて、grepコマンドで抜き出してみました。

grepコマンドによるライブラリのバージョン確認コマンド
!pip3 list | grep -e torch -e torchvision -e numpy -e matplotlib -e scikit-learn -e seaborn
grepコマンドによるライブラリのバージョン確認結果
matplotlib                    3.2.2          
matplotlib-venn               0.11.6         
numpy                         1.18.5         
scikit-learn                  0.22.2.post1   
seaborn                       0.11.0         
torch                         1.7.0+cu101    
torchsummary                  1.5.1          
torchtext                     0.3.1          
torchvision                   0.8.1+cu101    

Google Colaboratoryにインストールされているライブラリと、書籍で用意しようとしているライブラリのバージョンと比較すると、PyTorchとscikit-lernのバージョンが違う事が分かりました。

エラー回避方法

  • 公式ドキュメントの指示に従う(2020年12月17日追加。)

PyTorch公式にpipでのインストール方法が書かれており、その通りにコマンドを打ち込む事で、書籍で用意しようとしているライブラリのバージョンをインストール出来ます。

  • scikit-lernのみインストールする。

Google Colaboratoryにインストールされているライブラリのうち、scikit-learn以外は、書籍で用意しようとしているライブラリのバージョンと同じか、新しいものです。その為、scikit-learnのみ書籍に載っているバージョンをインストールするのが良いでしょう。

- "+cu101"を外してPyTorchをインストールする。 (2020年12月17日削除。)

PyTorchのバージョンが1.7.0でも書籍を読み進めるのに問題無いと思いますが、どうしても1.6.0の環境を用意したい場合は、以下のように"+cu101"を外して実行すればインストール出来ます。

"+cu101"を外したPyTorchのインストールコマンド
!pip3 install torch==1.6.0
!pip3 install torchvision==0.7.0
"+cu101"を外したPyTorchのインストール実行結果
Collecting torch==1.6.0
  Downloading https://files.pythonhosted.org/packages/38/53/914885a93a44b96c0dd1c36f36ff10afe341f091230aad68f7228d61db1e/torch-1.6.0-cp36-cp36m-manylinux1_x86_64.whl (748.8MB)
     |████████████████████████████████| 748.8MB 14kB/s 
Requirement already satisfied: future in /usr/local/lib/python3.6/dist-packages (from torch==1.6.0) (0.16.0)
Requirement already satisfied: numpy in /usr/local/lib/python3.6/dist-packages (from torch==1.6.0) (1.18.5)
ERROR: torchvision 0.8.1+cu101 has requirement torch==1.7.0, but you'll have torch 1.6.0 which is incompatible.
Installing collected packages: torch
  Found existing installation: torch 1.7.0+cu101
    Uninstalling torch-1.7.0+cu101:
      Successfully uninstalled torch-1.7.0+cu101
Successfully installed torch-1.6.0
Collecting torchvision==0.7.0
  Downloading https://files.pythonhosted.org/packages/8e/dc/4a939cfbd38398f4765f712576df21425241020bfccc200af76d19088533/torchvision-0.7.0-cp36-cp36m-manylinux1_x86_64.whl (5.9MB)
     |████████████████████████████████| 5.9MB 9.6MB/s 
Requirement already satisfied: torch==1.6.0 in /usr/local/lib/python3.6/dist-packages (from torchvision==0.7.0) (1.6.0)
Requirement already satisfied: numpy in /usr/local/lib/python3.6/dist-packages (from torchvision==0.7.0) (1.18.5)
Requirement already satisfied: pillow>=4.1.1 in /usr/local/lib/python3.6/dist-packages (from torchvision==0.7.0) (7.0.0)
Requirement already satisfied: future in /usr/local/lib/python3.6/dist-packages (from torch==1.6.0->torchvision==0.7.0) (0.16.0)
Installing collected packages: torchvision
  Found existing installation: torchvision 0.8.1+cu101
    Uninstalling torchvision-0.8.1+cu101:
      Successfully uninstalled torchvision-0.8.1+cu101
Successfully installed torchvision-0.7.0

PyTorch公式によると、"+cu101"とは、CUDA 10.1の環境にインストールする時に用いるコマンドで、"+cu101"を外すとCUDA 10.2の環境にインストールする時に用いるコマンドの事です。

最後に

"+cu101"を外す事で、Google Colaboratory上にPyTorch 1.6.0のインストールは出来ますが、インストール後の動作については、一切の保証が出来ません。

初期投稿では、"+cu101"を外す事を書きましたが、そのような事をしなくても、公式ドキュメント通りにコマンドを打ち込む方が良いです。

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1