Thi's avatar
HomeAboutNotesBlogTopicsToolsReading
About|My sketches |Cooking |Cafe icon Support Thi
💌 [email protected]

Google Colab

Anh-Thi Dinh
API & ServicesGoogleTools
Left aside

URLs

  • Main site.
  • Welcome site.
  • SeedBank by Google: Collection of Interactive Machine Learning Examples.

Colab & Github

👉 Check this section too.

Hotkeys / Shortcuts

Check the command shortcuts in Tools > Keyboard shortcuts (Ctrl + M H), below are the most popular ones (If you use MacOS, replace Ctrl with cmd):
☝
We can use system commands in Colab with !<command>. For example, !git clone ...

Import libraries

Install permanently

Upgrade/Switch TensorFlow versions

Git with Colab

Check out Git
If you don't want to use your username andd password, you can use "Personal access tokens" on Github. Create one here and then use,

Keep Colab awake

F1 then Console and type,

Change to current working directory

By default, the working directory is /content/. One can use below command to change to another place,
From that point, we are working on /content/data-science-learning.

Upload a file to Colab

Each user has a "machine" in /content/.
Create a new cell and paste,
Run 2 times this cell, at the 2nd time, you can choose your file.

Using Google Drive

Run a cell containing following codes,
and then follow the guide on the screen. In order to access to the drive,

Clone a repo from Github

The cloned folder are stored in /content/. If you wanna pull requests, use,

PyTorch with GPU

To enable GPU backend for your notebook, go to Edit → Notebook Settings and set Hardware accelerator to GPU. (ref)

Install 7zip reader, GraphViz, PyDot, cartopy

Save as HTML

Jupyter Notebook has an option to 'Download as' HTML (or other) format. Google Colaboratory does not.
  1. Install the nbconvert package.
  1. Save your Colab notebook.
  1. In your terminal:

Other functions

  • Interrupt a long running python process: Runtime > Interrupt execution (Alt + M + I).
  • Support Jupyter magic commands, check full list here.
◆URLs◆Colab & Github◆Hotkeys / Shortcuts◆Import libraries○Install permanently◆Upgrade/Switch TensorFlow versions◆Git with Colab◆Keep Colab awake◆Change to current working directory◆Upload a file to Colab○Using Google Drive○Clone a repo from Github◆PyTorch with GPU◆Install 7zip reader, GraphViz, PyDot, cartopy◆Save as HTML◆Other functions
About|My sketches |Cooking |Cafe icon Support Thi
💌 [email protected]
1!pip install -q matplotlib-venn
2# or
3!apt-get -qq install -y libfluidsynth1
1# To determine which version you're using:
2!pip show tensorflow
1# For the current version:
2!pip install --upgrade tensorflow
1# For a specific version:
2!pip install tensorflow==1.2
1# For the latest nightly build:
2!pip install tf-nightly
1# Initialize the git repository (optional)
2!git init
1# Set the global username and email
2!git config --global user.email "[email protected]"
3!git config --global user.name "Your Name"
1# Add all the files
2!git add -A
3# or
4!git add .
1# Commit
2!git commit -m "Comment for that commit"
1# Pass your Github credentials
2!git remote rm origin # in the case you meet "fatal: remote origin already exists"
3!git remote add origin <https://<github-username>:<github-password>@github.com/><github-username>/<repository-name>.git
1# Push to origin
2!git push -u origin master
1!git git remote add origin <https://<username>:<access-token>@github.com/><username>/<repo>.git
1function ClickConnect(){
2  console.log("Working");
3  document.querySelector("colab-connect-button").shadowRoot.getElementById("connect").click()
4}
5setInterval(ClickConnect,60000)
1%cd /content/data-science-learning
1from google.colab import files
2
3uploaded = files.upload()
4
5for fn in uploaded.keys():
6  print('User uploaded file "{name}" with length {length} bytes'.format(
7      name=fn, length=len(uploaded[fn])))
1from google.colab import drive
2drive.mount('/content/drive')
1with open('/content/drive/My Drive/foo.txt', 'w') as f:
2  f.write('Hello Google Drive!')
1!git clone <https://github.com/dinhanhthi/data-science-learning.git>
1%cd /content/data-science-learning
2!git pull
1# <https://pypi.python.org/pypi/libarchive>
2!apt-get -qq install -y libarchive-dev && pip install -q -U libarchive
3import libarchive
1# <https://pypi.python.org/pypi/pydot>
2!apt-get -qq install -y graphviz && pip install -q pydot
3import pydot
1!apt-get -qq install python-cartopy python3-cartopy
2import cartopy
1jupyter nbconvert --to <output format> <filename.ipynb>
2# jupyter nbconvert --to html mynotebook.ipynb