14
8

More than 1 year has passed since last update.

google colabのpythonとcudaのバージョンを変更する

Last updated at Posted at 2023-01-11

はじめに

tensorflow-gpu 1.13.2環境でモデルを動かすためにgoogle colabのpythonとcudaのバージョンを変更した時のメモです。

変更前
python: 3.8.16
cuda: 11.2

変更後
python: 3.6.9
cuda: 10.0

pythonのバージョンの変更

以下のコマンドで現在のバージョンを確認する。

!python3 -V
Python 3.8.16

以下のコマンドを実行する。

!sudo update-alternatives --config python3

このような出力が出てくるので、python3.6に変更したい場合は、「Press to keep the current choice[*], or type selection number:」の後に1を入力する。

There are 2 choices for the alternative python3 (providing /usr/bin/python3).

  Selection    Path                Priority   Status
------------------------------------------------------------
* 0            /usr/bin/python3.8   2         auto mode
  1            /usr/bin/python3.6   1         manual mode
  2            /usr/bin/python3.8   2         manual mode

Press <enter> to keep the current choice[*], or type selection number: 1

バージョンが変更されたことを確認する。

!python3 -V
Python 3.6.9

pipをインストールする。

!sudo apt install python3-pip
!wget https://bootstrap.pypa.io/pip/3.6/get-pip.py
!python3 get-pip.py

cudaのバージョンの変更

以下のコマンドで現在のバージョンを確認する。

!nvcc -V
nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2021 NVIDIA Corporation
Built on Sun_Feb_14_21:12:58_PST_2021
Cuda compilation tools, release 11.2, V11.2.152
Build cuda_11.2.r11.2/compiler.29618528_0

以下のページを参考にしてcuda 10.0をインストールする。
https://qiita.com/jun40vn/items/0cba2cde743f3f21e2db

以下のコマンドを実行し、cuda 10.0がインストールされたことを確認する。

!ls -d /usr/local/cuda-*
/usr/local/cuda-10.0  /usr/local/cuda-11  /usr/local/cuda-11.2

以下のコマンドを実行し、バージョンを変更する。

import os
p = os.getenv('PATH')
ld = os.getenv('LD_LIBRARY_PATH')
os.environ['PATH'] = f"/usr/local/cuda-10.0/bin:{p}"
os.environ['LD_LIBRARY_PATH'] = f"/usr/local/cuda-10.0/lib64:{ld}"

バージョンが変更されたことを確認する。

!nvcc -V
nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2018 NVIDIA Corporation
Built on Sat_Aug_25_21:08:01_CDT_2018
Cuda compilation tools, release 10.0, V10.0.130

tensorflow-gpu 1.13.2のインストール

!pip3 install tensorflow-gpu==1.13.2
!pip3 install numpy==1.18.4
!pip3 install pandas==1.0.3

バージョンを確認する。

!pip3 show tensorflow-gpu
Name: tensorflow-gpu
Version: 1.13.2
Summary: TensorFlow is an open source machine learning framework for everyone.
Home-page: https://www.tensorflow.org/
Author: Google Inc.
Author-email: opensource@google.com
License: Apache 2.0
Location: /usr/local/lib/python3.6/dist-packages
Requires: absl-py, astor, gast, grpcio, keras-applications, keras-preprocessing, numpy, protobuf, six, tensorboard, tensorflow-estimator, termcolor, wheel
Required-by: 

パスを追加する。

import sys
sys.path.insert(0,'/usr/local/lib/python3.6/dist-packages')
print(sys.path)
['/usr/local/lib/python3.6/dist-packages', '/content', '/env/python', '/usr/lib/python38.zip', '/usr/lib/python3.8', '/usr/lib/python3.8/lib-dynload', '', '/usr/local/lib/python3.8/dist-packages', '/usr/lib/python3/dist-packages', '/usr/local/lib/python3.8/dist-packages/IPython/extensions', '/root/.ipython']

tensorflowをインポートする。

import tensorflow

参考記事

14
8
0

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
14
8