Quick guide to run your Python scripts on Google Colaboratory

Quick guide to run your Python scripts on Google ColaboratoryStart training your neural networks with free GPUs todayEden AuBlockedUnblockFollowFollowingJan 26If you are looking for an interactive way to run your Python script, say you want to start a machine learning project with a couple of friends, look no further — Google Colab is the best solution for you.

You can work online and save your code on your local Google Drive, and it allows you toRun your scripts with free GPUs (and TPUs!)Utilize pre-installed Python libraries and Jupyter Notebook featuresWork anywhere you want, it is on the cloudsShare codes and collaborate with colleaguesIn short,Google Colab = Jupyter Notebook + Free GPUswith arguably cleaner interface than most (if not all) alternatives.

I have come up with some code snippets for you to master Google Colab.

I hope this becomes a go-to article when you need some ready-made codes to solve common problems on Colab.

Originally published on my blog edenau.

github.

io.

BasicsEnabling GPU/TPU AccelerationGo to ‘Runtime’ > ‘Change runtime type’ > ‘Hardware accelerator’, and select ‘GPU’ or ‘TPU’.

You can check if the GPU is enabled byimport tensorflow as tfdevice_name = tf.

test.

gpu_device_name()if device_name != '/device:GPU:0': raise SystemError('GPU device not found')An error will be raised if GPU is not enabled.

Note that you can only run a session continuously for maximum 12 hours, and the environment do not persist across sessions.

Running a CellSHIFT + ENTERExecuting Bash CommandsSimply add an !.before, for example:!ls '/content/gdrive/My Drive/Colab Notebooks/'Let’s check the information of OS, processors and RAM they are using:!cat /proc/version!cat /proc/cpuinfo!cat /proc/meminfoLinux, no surprise.

FilesAccessing Files on Google DriveUse the following script:from google.

colab import drivedrive.

mount('/content/gdrive')You will then be asked to sign in your Google account and copy an authorization code.

Click the link, copy the code, paste the code.

Go to this URL in a browser: https://accounts.

google.

com/signin/oauth/.

Enter your authorization code: ·········· Mounted at /content/gdriveUploading FilesYou can simply upload files manually to your Google Drive, and access them using codes above.

Alternatively, you can use the following code:from google.

colab import filesuploaded = files.

upload()Running Executable FileCopy the executable file to /usr/local/bin, and give yourself permission to execute it.

!cp /content/gdrive/My Drive/Colab Notebooks/<FILE> /usr/local/bin!chmod 755 /usr/local/bin/<FILE>LibrariesInstalling LibrariesUse pip in bash command:!pip install <PACKAGE_NAME>or conda:!wget -c https://repo.

continuum.

io/archive/Anaconda3-5.

1.

0-Linux-x86_64.

sh!chmod +x Anaconda3-5.

1.

0-Linux-x86_64.

sh!bash .

/Anaconda3-5.

1.

0-Linux-x86_64.

sh -b -f -p /usr/local!conda install -q -y –prefix /usr/local -c conda-forge <PACKAGE_NAME>import syssys.

path.

append('/usr/local/lib/python3.

6/site-packages/')Machine LearningTensorboardUse ngrok:# Run Tensorboard in the backgroundLOGDIR = '/tmp/log'get_ipython().

system_raw( 'tensorboard –logdir {} –host 0.

0.

0.

0 –port 6006 &' .

format(LOGDIR))# Use ngrok to tunnel traffic to localhost!.wget https://bin.

equinox.

io/c/4VmDzA7iaHb/ngrok-stable-linux-amd64.

zip!.unzip ngrok-stable-linux-amd64.

zipget_ipython().

system_raw('.

/ngrok http 6006 &')# Retrieve public url!.curl -s http://localhost:4040/api/tunnels | python3 -c "import sys, json; print(json.

load(sys.

stdin)['tunnels'][0]['public_url'])"and you will get a hyperlink like this:https://d5b842b9.

ngrok.

ioYou can access your Tensorboard via the link!RemarksThank you for reading.

I will try to keep this article up-to-date.

Hope this helps!Originally published on my blog edenau.

github.

io.

.

. More details

Leave a Reply