Google Colab Tutorial

A free GPU testing environment

Date: Dec. 8th, 2019

Lecturer: Chia

Outline

  • Introduction To Google Colab
  • Setting for using GPU
  • Connect Google Colab with Google Drive
  • Running with Google Colab
  • Sharing my Colab notebook

Introduction To Google Colab

  • Google Colab (Colaboratory)
    • 雲端的 Jupyter Notebook 開發環境
    • 特點:提供免費的GPU (型號為Tesla K80 GPU)
    • 限制:每次開啟有12小時的連續使用時間限制

Setting for using GPU

  • Open your Google Drive

Setting for using GPU

import tensorflow as tf
tf.test.gpu_device_name()

#Output: ''
  • Check if CPU is working or not!

Setting for using GPU

  • Change Hardware accelerator to GPU

Setting for using GPU

import tensorflow as tf
tf.test.gpu_device_name()

#Output: '/device:GPU:0'
  • Check if GPU is working or not!
from tensorflow.python.client import device_lib
device_lib.list_local_devices()
  • Which GPU Am I Using?

Connect Google Colab with Google Drive

from google.colab import drive
drive.mount('/content/drive/')
  • Enable authorization to link with Google Drive.
!ls "/content/drive/My Drive/"
  • Now, you can reach your Google Drive.

Running with Google Colab

!python3 "/content/drive/My Drive/Colab Notebooks/print_abc.py"
  • Upload print_abc.py to Google Drive & Run with Google Colab
from google.colab import files

files.download('/content/drive/My Drive/Colab Notebooks/print_abc.py')
  • Run with Google Colab to Download print_abc.py from Google Drive

Running with Google Colab

import os
os.chdir("/content/drive/My Drive/Colab Notebooks")
!ls

from google.colab import files
uploaded = files.upload()
for fn in uploaded.keys():
  print('User uploaded file "{name}" with length {length} bytes'.format(name=fn, length=len(uploaded[fn])))
  • Select the print_xyz.py from computer and upload it to the Google Colab virtual machine.

Running with Google Colab

# -P  => 指定存放路徑
!wget https://raw.githubusercontent.com/vincentarelbundock/Rdatasets/master/csv/datasets/Titanic.csv \
  -P "/content/drive/My Drive/Colab Notebooks"
  • Display First 5 Rows
import pandas as pd
titanic = pd.read_csv("/content/drive/My Drive/Colab Notebooks/Titanic.csv")
titanic.head(5)

Running with Google Colab

!git clone https://github.com/wxs/keras-mnist-tutorial.git
  • Changing Working Directory
import os
os.chdir("/content/drive/My Drive/Colab Notebooks")
!ls

# 到上一層目錄
#os.chdir("../")
#!ls
  • Cloning Github Repo to Google Colab

Running with Google Colab

%%bash
pip freeze
  • List all packages had already been installed.
  • The virtual machine you’re using, including any custom files and libraries that you’ve setup, will not be shared.
  • So it’s a good idea to include cells which install and load any custom libraries or files that your notebook needs.

Running with Google Colab

  • Install Keras
# -q, --quiet  =>  Give less output.

!pip install -q keras 
import keras

Running with Google Colab

%%bash

# For a specific version:
pip install -q tensorflow==1.2
# To determine which version you're using:
pip show tensorflow
  • Install / Show package 
%%bash

# For the current version: 
pip install --upgrade -q tensorflow
# To determine which version you're using:
pip show tensorflow

Sharing my Colab notebook

Sharing my Colab notebook

Sharing my Colab notebook

Thanks for listening.

Google Colab Tutorial

By BessyHuang

Google Colab Tutorial

  • 2,637