Appendix B — Introduction to Colab
B.1 Setup
You can lookup the resources first:
import sys
# Is this notebook running on Colab or Kaggle?
IS_COLAB = "google.colab" in sys.modules
IS_KAGGLE = "kaggle_secrets" in sys.modules
gpu_info = !nvidia-smi
gpu_info = '\n'.join(gpu_info)
if gpu_info.find('failed') >= 0:
if IS_COLAB:
print("Go to Runtime > Change runtime and select a GPU hardware accelerator.")
if IS_KAGGLE:
print("Go to Settings > Accelerator and select GPU.")
else:
from tensorflow.python.client import device_lib
print(device_lib.list_local_devices())
V100 > P100 > T4 > K80 (but most of the time you get K80 or T4 using the free Colab)
B.2 Cells
A notebook is a list of cells. Cells contain either explanatory text or executable code and its output. Click a cell to select it.
B.2.1 Code cells
Below is a code cell. Once the toolbar button indicates CONNECTED, click in the cell to select it and execute the contents in the following ways:
- Click the Play icon in the left gutter of the cell;
- Type Cmd/Ctrl+Enter to run the cell in place;
- Type Shift+Enter to run the cell and move focus to the next cell (adding one if none exists); or
- Type Alt+Enter to run the cell and insert a new code cell immediately below it.
There are additional options for running some or all cells in the Runtime menu.
B.2.2 Text cells
This is a text cell. You can double-click to edit this cell. Text cells use markdown syntax. To learn more, see our markdown guide.
You can also add math to text cells using LaTeX to be rendered by MathJax. Just place the statement within a pair of $ signs. For example $\sqrt{3x-1}+(1+x)^2$
becomes \(\sqrt{3x-1}+(1+x)^2.\)
Table generator also works here.
B.2.3 Adding and moving cells
You can add new cells by using the + CODE and + TEXT buttons that show when you hover between cells. These buttons are also in the toolbar above the notebook where they can be used to add a cell below the currently selected cell.
You can move a cell by selecting it and clicking Cell Up or Cell Down in the top toolbar.
Consecutive cells can be selected by “lasso selection” by dragging from outside one cell and through the group. Non-adjacent cells can be selected concurrently by clicking one and then holding down Ctrl while clicking another. Similarly, using Shift instead of Ctrl will select all intermediate cells.
B.3 Working with Bash
B.4 Working with python
Colaboratory is built on top of Jupyter Notebook. Below are some examples of convenience functions provided.
Long running python processes can be interrupted. Run the following cell and select Runtime -> Interrupt execution (hotkey: Cmd/Ctrl-M I) to stop execution.
B.4.1 System aliases
Jupyter includes shortcuts for common operations, such as ls:
!
calls out to a shell (in a new process), while %
affects the process associated with the notebook
That !ls
probably generated a large output. You can select the cell and clear the output by either:
- Clicking on the clear output button (x) in the toolbar above the cell; or
- Right clicking the left gutter of the output area and selecting “Clear output” from the context menu.
Execute any other process using !
with string interpolation from python variables, and note the result can be assigned to a variable:
B.4.2 Magics
Colaboratory shares the notion of magics from Jupyter. There are shorthand annotations that change how a cell’s text is executed. To learn more, see Jupyter’s magics page.
B.4.3 Automatic completions and exploring code
Colab provides automatic completions to explore attributes of Python objects, as well as to quickly view documentation strings. As an example, first run the following cell to import the numpy
module.
If you now insert your cursor after np
and press Period(.
), you will see the list of available completions within the np
module.
If you type an open parenthesis after any function or class in the module, you will see a pop-up of its documentation string:
When hovering over the method name the Open in tab
link will open the documentation in a persistent pane. The View source
link will navigate to the source code for the method.
B.5 Integration with Drive
Colaboratory is integrated with Google Drive. It allows you to share, comment, and collaborate on the same document with multiple people:
The SHARE button (top-right of the toolbar) allows you to share the notebook and control permissions set on it.
File->Make a Copy creates a copy of the notebook in Drive.
File->Save saves the File to Drive. File->Save and checkpoint pins the version so it doesn’t get deleted from the revision history.
File->Revision history shows the notebook’s revision history.
B.5.1 Uploading files from your local file system
files.upload
returns a dictionary of the files which were uploaded. The dictionary is keyed by the file name and values are the data which were uploaded.
Files are temporarily stored, and will be removed once you end your session.
B.5.2 Downloading files to your local file system
files.download
will invoke a browser download of the file to your local computer.
B.5.3 Mounting Google Drive locally
The example below shows how to mount your Google Drive on your runtime using an authorization code, and how to write and read files there. Once executed, you will be able to see the new file (foo.txt
) at https://drive.google.com/.
This only supports reading, writing, and moving files; to programmatically modify sharing settings or other metadata, use one of the other options below.
Note: When using the ‘Mount Drive’ button in the file browser, no authentication codes are necessary for notebooks that have only been edited by the current user.
Remember DO NOT store input data in your drive and load from there. The input/output is very slow (store at
./
instead). Your output data should be stored in your google drive so that it can be accessed next time.
B.5.4 Loading Public Notebooks Directly from GitHub
Colab can load public github notebooks directly, with no required authorization step.
For example, consider the notebook at this address: https://github.com/googlecolab/colabtools/blob/master/notebooks/colab-github-demo.ipynb.
The direct colab link to this notebook is: https://colab.research.google.com/github/googlecolab/colabtools/blob/master/notebooks/colab-github-demo.ipynb.
To generate such links in one click, you can use the Open in Colab Chrome extension.
B.6 Run Flask or other web app
B.7 Use different version of python/environment or lanaguage
Refer to here and condacolab for more information.
Jupyter is named after Julia, Python and R. You can change the kernel to R or Julia.
B.8 Downloading data from Kaggle
The Dogs vs. Cats dataset that we will use isn’t packaged with Keras. It was made available by Kaggle as part of a computer vision competition in late 2013, back when convnets weren’t mainstream. You can download the original dataset from www.kaggle.com/c/dogs-vs-cats/data.
But you can also use Kaggle API. First, you need to create a Kaggle API key and download it to your local machine. Just navigate to the Kaggle website in a web browser, log in, and go to the My Account page. In your account settings, you’ll find an API section. Clicking the Create New API Token button will generate a kaggle.json
key file and will download it to your machine.
Finally, create a ~/.kaggle
folder, and copy the key file to it. As a security best practice, you should also make sure that the file is only readable by the current user, yourself:
The first time you try to download the data, you may get a “403 Forbidden” error. That’s because you need to accept the terms associated with the dataset before you download it—you’ll have to go to www.kaggle.com/c/dogs-vs-cats/rules (while logged into your Kaggle account) and click the I Understand and Accept button. You only need to do this once.
More information about Kaggle API