How to use Google Colab

Python Methods and Functions

If you want to create a machine learning model but say you don`t have a computer that can handle the load, Google Colab — this is the platform for you. Even if you have a GPU or a good computer, create a local environment with anaconda, install packages and troubleshoot installation problems — not an easy task. 
Colab Laboratory — it is a free Jupyter laptop environment provided by Google where you can use free GPUs and TPUs that can solve all these problems.

Getting Started

To get started with Colab, you first need log into your google account and then follow this link https://colab.research.google.com .

Opening Jupyter Notebook:
When you open the site, you will see a pop-up window containing the following tabs — 

EXAMPLES: Contain a number of Jupyter notebooks of various examples.
RECENT: Jupyter notebook you have recently worked with.
GOOGLE DRIVE: Jupyter notebook in your google drive.
GITHUB: You can add Jupyter notebook from your GitHub but you first need to connect Colab with GitHub.
UPLOAD: Upload from your local directory.

Otherwise, you can click New Python3 Notebook or New Python2 Notebook in the bottom right corner.

Notebook Description:

When you create a new notebook, it will create a Jupyter notebook with Untitled0.ipynb and save it to its google drive in a folder named Colab Notebooks . Now, since this is essentially a Jupyter notebook, all Jupyter notebook commands will work here. However, you can refer to the details in

Use GPU and TPU:
Click on the "Runtime" dropdown menu. Select Change Runtime Type . Now select everything (GPU, CPU, None) you want from the Hardware Accelerator dropdown menu. 

Check the GPU:

import tensorflow as tf

tf.test.gpu_device_name ()

If a GPU is connected, it will output the following —

 `/ device: GPU: 0` 

Otherwise If the following will be displayed

 `` 

Check TPU:

import os

 

if `COLAB_TPU_ADDR` not in os.environ:

print ( `Not connected to TPU` )

else :

  print ( " Connected to TPU " )

If the GPU connected, it will output the following

 Connected to TPU 

Otherwise, it will output the following

 Not connected to TPU 

Install Python packages —
Use can use pip to install any package. For example:

! pip install pandas

Clone GitHub repo:
Use the git clone command. For example:

! git clone https: / / github.com / souvik3333 / Testing - and - Debugging - Tools

Upload file:

from google.colab import files

uploaded = files.upload ()

Select Choose File and upload the file you want. Please enable third party cookies if disabled.

You can then save it to an info frame.

import io

df2 = pd.read_csv (io.BytesIO (uploaded [ `file_name.csv` ]))

Upload the file by connecting Google Drive:
To mount the drive in the "mntDrive" folder, do the following:

from google.colab import drive

drive.mount ( `/ mntDrive` )

Then you will see the link, click the link, then allow access, copy the pop-up code and paste it into the "Enter authorization code:" field.

Now to see all the details on your google drive, you need to do the following:

! ls "/ mntDrive / My Drive"

File hierarchy:
You can also see the file hierarchy by clicking "& gt;" in the upper left corner under the control buttons (CODE, TEXT, CELL). 

Download files:
Let`s say you want to download" file_name.csv ". You can copy the file to your google drive (in the data folder you need to create a data folder in google drive) by doing the following:

cp file_name.csv "/ mntDrive / My Drive / data / renamed_file_name.csv"

The file will be saved in the data folder with the name renamed_file_name.csv. Now you can download directly from there, or, you can just open the file hierarchy and right-click will give the download option.

Download Jupyter Notebook:
Click the dropdown "File" in the upper left corner. Select "download .ipynb" or "download .py"

Share Jupyter Notebook:
You can share your notebook by adding someone else`s email address or creating a sharing link. 





Get Solution for free from DataCamp guru