Try   HackMD

Google Colaboratoryの使い方

Google Colaboratoryについて説明します。

Google Colaboratory(別名:Colab(コラボ))はGoogleがインストール不要でかつ、すぐにJupyter Notebook互換の環境でPythonが利用できるサービスです。利用にはGoogleアカウントが必要です。
GPUが使用出来ることで、機械学習で利用することも多いです。

Google Colaboratoryは、無料版と有料版のColab Pro、Colab Pro+があります。

有料版のコスト

  • Colab Pro:1072円/月
  • Colab Pro+:5343円/月

最適な Colab のプランを選択する

GPUの性能とメモリ量、バックグラウンド実行などのオプションの違いで、無料版、Colab Pro、Colab Pro+があります。「Colab Pro+」は、ブラウザを閉じた後も処理は継続されるプランです。

無料版では、Colabの連続使用時間が最大12時間までに制限されます。
また、90分ほど操作しない状態が続く場合、仮想マシンは停止し、ダウンロードしたデータやライブラリは初期化されますが、無料版でも十分に利用することはできます

Binder

Colaboratoryの他に、github上のJupyter notebookファイルをリポジトリから、実行可能なサービスにBinderという無料で使えるサービスもあります。


Colaboratoryの立ち上げ

Googleアカウントが必要なので、作成をしてログインをしてください

直接作成

Googleドライブで直接作成をします。

Googleドライブでの「作成ボタン」をクリックする

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

そのあとメニューで「Google Colaboratory」を選択する

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

または、右マウスをクリックして「Google Colaboratory」を選択する

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

新規で「Google Colaboratory」が作成されます

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Google Colaboratoryの選択肢にない場合

Google Colaboratoryの選択肢にない場合は、アプリ追加をしてください

メニューを開いてアプリ追加を選択

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Google Colaboratoryを選択
Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Google Colaboratoryのインストールをクリック
Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

続行をクリック
Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

既存のノートブックを使用する場合

ブラウザにGoogleアカウントでログインした後に、以下のURLを開いてください。
https://colab.research.google.com/

ノートブックのアップロード

Colaboratoryを立ち上げると、以下のようにノートブックを指定することが求められるので「Googleドライブ」「GitHub」「(PCから)アップロード」のタブを選択して、ファイルをアップロードします

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

また、ノートブックを新たに作成するには、Colaboratoryのファイルメニューで
「ノートブックを新規作成」を選択してください。
Untitled0.ipynb という名前のノートブックが作られます。

タイトルをクリックすれば名前を変更することができます。

TPUとGPU

Colabでは、計算用にハードウェアアクセラレータの選択が可能です

  • TPU(Tensor Processing Unit)
    • テンソル計算処理に最適化されたプロセッサ
  • GPU(Graphics Processing Unit)
    • 画像処理に特化したプロセッサ。機械学習演算などでは、GPUを利用して演算をすることが可能

設定を変更するには、メニューからランタイム -> ランタイムのタイプを変更を選択して変更をします。

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

コードとテキスト

Colabでは、Jupyter Notebookと同じように「コード」と「テキスト」のエリアがあり、文章とPythonのコードが混在することができます。

テキスト

上部の「+テキスト」を押すと、テキストのセルが表示され、Markdown形式で文書の作成が可能です

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

コード

上部の「+コード」を押すと、コードのセルが表示されPythonのコードを記述することができます。

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

セルという単位でプログラムを実行することができます。実行にはランタイムメニュー、ショートカットキー、三角の実行ボタンを押すことで実行できます

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

設定

ツール -> 設定でColabの設定の変更なども可能です。

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Google ドライブをマウントする

Colabは、Googleドライブのファイルにアクセスすることが可能です。
アクセスするためには、マウントする必要があります。

マウントするためには、Colabで下記のコードを実行します。

from google.colab import drive
drive.mount('/content/drive')

マウントに成功すると、アクセスすることが可能になり、左側のフォルダーアイコンクリックするとツリー上にファイルが閲覧することができます。
プログラムからアクセスする場合は、Googleドライブのパスを使ってアクセスをします

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →


最後に

この他の部分については、下記のリンクを参考にしてみてください。

Goolge

参考

CQ出版