Introduction & Basic Usage
This is supposed to be a concise tutorial and you should not spend longer than 10 minutes to go through it.
What is Google Colab?
Google Colab is a free Jupyter notebook that requires no setup to use. It works with most major browsers. As written in the faq of Colaboratory, it has been tested with the latest versions of Chrome, Firefox, and Safari.
Where are my notebooks stored? Can they be shared?
All Colab notebooks are stored in Google Drive. They can be shared just as you would do in Google Docs, etc. -- simply click the Share button on any Colab notebooks.
How can I get a Colab notebook for use?
- Since a Colab notebook is working on your own Google Drive, you need to specify a folder to store your notebooks. Here, you can either create a new folder or use the default Colab Notebooks folder.
- You create a new notebook via Right click > More > Colaboratory.
- You may rename the notebook by clicking on the file name.
If a notebook is shared, what will be shared?
If a notebook is shared, the whole contents will be shared i.e. text, code, and outputs. However, you can omit to share outputs by selecting Edit > Notebook settings > Omit code cell output when saving this notebook. The virtual machine you are using, including any custom files and installed libraries, will not be shared. Hence, it is always a good idea to include cells which can install what the notebook needs.
Can I import an existing Jupyter/IPython notebook into Colab?
Yes, you can import it by choosing Upload notebook from the File menu.
How can I get my data out?
You can download any Colab notebook that you have created from Google Drive or from Colab's File menu. All Colab notebooks are stored in the Jupyter notebook format (.ipynb).
How to use a free GPU or TPU?
Simply click Edit > Notebook settings or Runtime > Change runtime type. Then, select either GPU or TPU as Hardware accelerator.
Can I mount a Google Drive?
- Run the code as depicted on the left hand side, click the link, and enter the verification code into the text box.
- After completion of the authorization process, you can try to reach your Google Drive with the following command:
!ls "/content/drive/My Drive/"
Assume that I want to download a file and display its content. How should I do?
- You can download a file using wget command as follows:
!wget https://raw.githubusercontent.com/vincentarelbundock/Rdatasets/master/csv/datasets/Titanic.csv -P "/content/drive/My Drive/app"
2. To read the file and display first 5 rows, you may use pandas.
import pandas as pd
titanic = pd.read_csv(“/content/drive/My Drive/app/Titanic.csv”)
titanic.head(5)
I want to clone a Github repo into my Colab
- Find any Github repo you want to clone and copy its checkout URL (e.g. https://github.com/wxs/keras-mnist-tutorial.git).
- Run the following git command:
How to install libraries?
Simply use !pip install
or !apt-get install
.
I want to see if GPU is currently working
To see if you are using GPU in Colab, you can run the following commands. See this Colab notebook as an example for TPU checking.
import tensorflow as tf
tf.test.gpu_device_name()
To see which GPU I am using?
Simply run the following command:
from tensorflow.python.client import device_lib device_lib.list_local_devices()
What's about RAM?
!cat /proc/meminfo
What's about CPU?
!cat /proc/cpuinfo
I want to change the working directory
Run the following commands:
import os
os.chdir("drive/app")
Here, the working directory will be set to "drive/app" and you don't need to add "drive/app" anymore to access files.
Solutions for "No backend with GPU available"
If you encounter the following error:
Failed to assign a backend
No backend with GPU available.
Would you like to use a runtime with no accelerator?
This means a lot of people are using GPUs now. You should try using it a bit later.
How to clear outputs of all cells?
Follow Tools > Command Palette > Clear All Outputs
How to add Form to Colab?
Form makes tuning hyperparameters easier from directly changing them in the code. To add a Form in a cell, simply click +form on that cell (as shown on the figure).
How to open documentation?
To see function arguments, simply add question mark (?) after the function name. For instance, the figure illustrates to display TensorFlow's documentation without opening its website.
References
This tutorial is summarized and is revised from:
Other blogged posts >> Tutorials