Article Image

There is one problem with machine learning. We don't all have a good GPU or none at all. As a student it is quite common to just have a laptop which normally doesn't really have a GPU and of course also can't be upgraded besides getting an external GPU...

As mentioned in a previous article Google is here to help us out at least kind of. Google Colab is a service where you can use a Jupyter Notebook on their server including a K80 GPU. This can be connected to your gdrive and then you can start.

This blog is more about the downsides and how to actually work with this service.

The first steps after creating a notebook:

• Activate the GPU:

• Runtime -> Change runtime type -> Select GPU

!apt-get install -y -qq software-properties-common python-software-properties module-init-tools
!apt-get update -qq 2>&1 > /dev/null
auth.authenticate_user()
import getpass

and

    from google.colab import drive
drive.mount('/content/drive')

Both of these blocks will give you an url where you get a token to actually give google colab the access to gdrive. Unfortunately this has to be done every time. The ! in front of some lines/commands indicates that this is a linux and not a python command.

This is useful for some other steps later on. You can check how much disk space you have using

!df -h

Which gives you something like this:

Filesystem      Size  Used Avail Use% Mounted on
overlay         359G  9.8G  331G   3% /
tmpfs           6.4G     0  6.4G   0% /dev
tmpfs           6.4G     0  6.4G   0% /sys/fs/cgroup
tmpfs           6.4G  249M  6.2G   4% /opt/bin
/dev/sda1       365G   12G  354G   4% /etc/hosts
shm              64M     0   64M   0% /dev/shm
tmpfs           6.4G     0  6.4G   0% /sys/firmware
drive           100G   57G   44G  57% /content/drive

Where the last line indicates your gdrive.

Okay this is basically everything to get you started but some more useful information:

• Installing python packages with !pip install ...

Now running your neural network model is straight forward the next thing is to save the model. Here I also sometimes have a problem of saving the trained model to my gdrive using the mounting point but you can use PyDrive:

import os
from oauth2client.client import GoogleCredentials

and then

auth.authenticate_user()
drive = GoogleDrive(gauth)

save the model on the google colab server and use:

upload = drive.CreateFile({'title': 'FILENAME_ON_GDRIVE'})
upload.Upload()

The filename on gdrive somehow needs to be a filename and not a path as it seems that it can only be stored in your root folder. Don't ask me why...

Try it out and post your thoughts!

Have fun!