Getting Started with Google Colab
- Introduction
- Access Google Colab
- The Notebook Selection Window
- Understanding the Notebook Interface
- Working with Data
- Using Hardware Acceleration
- Create a New Notebook
- Save Your Notebook
- Conclusion
Introduction
In this tutorial, I’ll introduce you to Google Colab, its features, and how to use it to run your code. Google Colab provides a free, cloud-based Jupyter Notebook environment that allows you to write, run, and share Python code in your browser without any setup or installation. A Jupyter Notebook is an interactive web-based tool for creating and sharing documents that contain live code, visualizations, and narrative text, often used in data analysis, visualization, and education.
Access Google Colab
To access Google Colab, follow these steps:
- Go to colab.research.google.com.
- Sign in with your Google account. If you don’t have a Google account, create one here.
The Notebook Selection Window
After signing in, the Notebook Selection window will pop up. This window displays a list of your recent notebooks, allowing you to access and open them. If this is your first time using Google Colab, you will only see the “Welcome to Colaboratory” notebook listed. The Notebook Selection window also allows you to import Jupyter Notebooks from Google Drive and GitHub or upload a notebook from your computer.
The “Welcome to Colaboratory” notebook is already open behind the Notebook Selection window, so we’ll work with that one. Click the cancel button in the bottom right corner of the popup window to view the welcome notebook.
Understanding the Notebook Interface
A notebook consists of a list of cells. Google Colab notebooks have two main types of cells: code cells and text cells. Code cells allow you to write and run Python code, while text cells let you add formatted text, images, and equations using Markdown. The first few cells in the welcome notebook are text cells.
Text Cells
You can view and edit the Markdown source of a text cell by double-clicking it. In editor mode, Google Colab will show the Markdown source and the rendered version side-by-side.
We can edit the Markdown source, and the rendered version will update in real time.
You can exit the editor mode by pressing Shift+Enter
, clicking the Close Markdown Editor
icon in the top-right corner of the text cell, or clicking another cell.
To create a new text cell, click the + Text
button in the toolbar.
Google Colab will add the new Markdown cell below the currently selected cell.
Code Cells
To create a new code cell, click the + Code
button in the toolbar at the top of the notebook or press Ctrl+M B
.
Google Colab will add the new code cell below the currently selected cell.
You can write Python code in the code cell and execute it by pressing Shift + Enter
or clicking the Play
button on the left side of the cell. Any output from the code will appear directly below the code cell.
We can also use code cells to access the command line by adding an exclamation point at the start of the cell. We can use this ability to install Python packages via the pip package installer.
Working with Data
Google Colab allows you to upload and download files to and from your computer and connect notebooks to your Google Drive.
Uploading Files
You can upload files from your local machine to use in your Google Colab notebook by following these steps:
- Click the Files icon in the left sidebar to open the file browser.
- Click the Upload button.
- Go to the file location on your local machine, select it, and click Open to upload it to your Google Colab workspace.
- Colab will display a warning that the runtime’s files get deleted when it terminates. Click OK in the bottom-right corner of the popup window.
- The uploaded file will appear in the file browser, and you can access it in your notebook.
- We can view the file by double-clicking it in the file browser or loading it in the notebook via Python.
Downloading Files
To download a file from your Google Colab workspace to your local machine, follow these steps:
- Locate the file in the file browser.
- Right-click the file and select Download.
The file will download to your local machine.
Connecting to Google Drive
Google Colab notebooks can connect to Google Drive to access, store, and manage your files. To do this, follow these steps:
- Click the Mount Drive button in the file browser.
Google Colab will create a new code cell containing the following code:
from google.colab import drive '/content/drive') drive.mount(
- Run the code cell by pressing
Shift + Enter
or clicking the Play button on the left side of the cell. A popup window will appear, prompting you to authorize access to your Google Drive.
- Click the
Connect to Google Drive
button to open the authorization page.
- Sign in with your Google account, and click Allow to grant access.
- Return to your Google Colab notebook. The code cell should have printed a message indicating your Google Drive is now mounted.
- Click the Refresh button in the file browser to update the contents.
Your Google Drive should now be mounted and accessible from the file browser.
You can read, write, and manage your Google Drive files directly from your Google Colab notebook. To access the files, use the path /content/drive/MyDrive/
followed by the file or folder name.
Using Hardware Acceleration
Google Colab offers free access to GPUs and TPUs to accelerate your code. To enable GPU or TPU acceleration:
- Click the “Runtime” menu at the top of the notebook.
- Select “Change runtime type.”
- Choose “GPU” from the “Hardware accelerator” drop-down menu.
- Click “Save.”
- Changing the hardware accelerator requires loading a new runtime. Loading a new runtime will delete any files we added and disconnect Google Drive. Google Colab will show a popup window asking you to confirm you want to delete the current runtime. Click “OK” to confirm.
Verify the notebook has GPU access by running the following code in a code cell:
!nvidia-smi
Your notebook will now use the selected hardware accelerator. Note that free GPU and TPU usage is time-limited. You can run notebooks on the free tier for at most 12 hours at a time (usually less). If you exceed the time allotment, you must wait until it resets (typically about 12 hours). Therefore, only enable hardware acceleration when needed and disable it when you don’t. To disable hardware acceleration, select None from the Hardware Accelerator drop-down menu.
Create a New Notebook
To create a new notebook:
- Open the
File
menu in the top-left corner and selectNew notebook
.
A new notebook will open in a separate tab. The runtime for the previous notebook is still active.
You can rename the notebook by clicking the notebook name at the top of the page. For now, we can name it “My First Notebook.”
Save Your Notebook
Google Colab automatically saves your notebooks to a “Colab Notebooks” folder in Google Drive.
Note the “Welcome to Colaboratory” notebook is not in the folder. Since we did not create that notebook, we must save our copy manually. Switch to that notebook’s tab and click the “Copy to Drive” button.
Google Colab will open our new copy of the notebook in a separate tab.
If we check the “Colab Notebooks” folder in Google Drive, we should now see our copy of the welcome notebook.
Version Control with GitHub
Google Colab can save and load notebooks from GitHub repositories, enabling seamless collaboration and tracking of changes in your code.
Saving a notebook to a GitHub repository:
- Open the File menu in the top-left corner and select Save a copy in GitHub.
- If you haven’t connected your GitHub account yet, follow the prompts to authorize Google Colab to access your repositories.
- Choose a repository, branch, and file path for your notebook. You can also update the commit message.
- Click OK to save the notebook to the specified GitHub repository.
Loading a notebook from a GitHub repository:
- Go to the Google Colab website.
{fig-align=“center”}
- Click the GitHub tab in the Notebook Selection window.
- Enter the URL for the GitHub repository containing the notebook you want to open. You can also search for one by entering a username or organization and repository name.
- Select the notebook you want to open, and it will open in a new tab.
Conclusion
You’ve now learned the fundamentals of Google Colab. This tutorial covered creating and editing cells, working with data, hardware acceleration, and saving and sharing notebooks via Google Drive and GitHub. Keep exploring Google Colab to uncover more features that can enhance your projects.
- Feel free to post questions or problems related to this tutorial in the comments below. I try to make time to address them on Thursdays and Fridays.
- I’m Christian Mills, a deep learning consultant specializing in computer vision and practical AI implementations.
- I help clients leverage cutting-edge AI technologies to solve real-world problems.
- Learn more about me or reach out via email at [email protected] to discuss your project.