Eighteen Python Snippets for Colab Productivity



Original Source Here

Reload any .py file that changed.

%load_ext autoreload
%autoreload

Install most Python packages.

!pip install <package>

Show matplotlib based graph inline of Colab.

import matplotlib
%matplotlib inline
import matplotlib.pyplot as plt

Show the number of installed packages and list all installed packages.

!pip install conda
packages = !conda list
len(packages),packages

=>

(392,
['Package Version ',
'----------------------------- ---------------',
'absl-py 0.10.0 ',
'alabaster 0.7.12 ',
'albumentations 0.1.12 ',
'altair 4.1.0 ',
'argon2-cffi 20.1.0 ',
.
.
.
'wheel 0.36.2 ',
'widgetsnbextension 3.5.1 ',
'wordcloud 1.5.0 ',
'wrapt 1.12.1 ',
'xarray 0.15.1 ',
'xgboost 0.90 ',
'xkit 0.0.0 ',
'xlrd 1.1.0 ',
'xlwt 1.3.0 ',
'yellowbrick 0.9.1 ',
'zict 2.0.0 ',
'zipp 3.4.0 '])

Show base computing image properties.

!cat /proc/cpuinfo

=>

processor	: 0
vendor_id : GenuineIntel
cpu family : 6
model : 79
model name : Intel(R) Xeon(R) CPU @ 2.20GHz
stepping : 0
microcode : 0x1
cpu MHz : 2200.000
cache size : 56320 KB
physical id : 0
siblings : 2
core id : 0
cpu cores : 1
apicid : 0
initial apicid : 0
fpu : yes
fpu_exception : yes
cpuid level : 13
wp : yes
flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ss ht syscall nx pdpe1gb rdtscp lm constant_tsc rep_good nopl xtopology nonstop_tsc cpuid tsc_known_freq pni pclmulqdq ssse3 fma cx16 pcid sse4_1 sse4_2 x2apic movbe popcnt aes xsave avx f16c rdrand hypervisor lahf_lm abm 3dnowprefetch invpcid_single ssbd ibrs ibpb stibp fsgsbase tsc_adjust bmi1 hle avx2 smep bmi2 erms invpcid rtm rdseed adx smap xsaveopt arat md_clear arch_capabilities
bugs : cpu_meltdown spectre_v1 spectre_v2 spec_store_bypass l1tf mds swapgs taa
bogomips : 4400.00
clflush size : 64
cache_alignment : 64
address sizes : 46 bits physical, 48 bits virtual
power management:

Show all computing devices.

from tensorflow.python.client import device_lib
device_lib.list_local_devices()

=>

[name: "/device:CPU:0"
device_type: "CPU"
memory_limit: 268435456
locality {
}
incarnation: 7776197507331903039, name: "/device:XLA_CPU:0"
device_type: "XLA_CPU"
memory_limit: 17179869184
locality {
}
incarnation: 13119766555461003311
physical_device_desc: "device: XLA_CPU device", name: "/device:XLA_GPU:0"
device_type: "XLA_GPU"
memory_limit: 17179869184
locality {
}
incarnation: 12261128317807567470
physical_device_desc: "device: XLA_GPU device", name: "/device:GPU:0"
device_type: "GPU"
memory_limit: 14640891840
locality {
bus_id: 1
links {
}
}
incarnation: 5905533955859115248
physical_device_desc: "device: 0, name: Tesla T4, pci bus id: 0000:00:04.0, compute capability: 7.5"]

Open your Goggle Drive in your Colab notebook instance.

If you don’t have a Google drive on your local computer, then install it. Google Drive is free.

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

=>

Mounted at /gdrive

Create, write to, and read out the file on Google Drive.

with open('/gdrive/My Drive/myfile.txt', 'w') as f:
f.write('Hello Google Drive!')
!cat '/gdrive/My Drive/myfile.txt'

=>

Hello Google Drive!

List top-level files on Google Drive.

!ls /gdrive/'My Drive' -lax'

=>

Colab Notebooks'
.
.
.
text_pre_processing_colab-cpu.ipynb
text_pre_processing_colab_gpu.ipynb
text_pre_processing_colab_p100.ipynb
text_pre_processing_colab-tpu.ipynb
!cp drive/MyDrive/<file>.py
import <file>

or

import sys
sys.path.append('/gdrive/<packageirectory>')
import <package>

Git is installed in Colab. Most Git commands execute in Colab using shell-escape ! . For example:

!git clone https://github.com/<repo>

How to use Tensorboard in Colab.

A Colab notebook shows step by step how to load Kaggle datasets.

Embedding the Julia language in Colab.

Draw graphs with Graphviz.

The documentation for graphiz.

Install graphviz.

!apt-get -y install python-pydot
!apt-get -y install python-pydot-ng
!apt-get -y install graphviz

Create a graph with Graphviz.

from graphviz import Digraph
dot = Digraph(comment='MLOps Flow as a DAG')
print(dot)
dot.node('D', 'Feature Store')
dot.node('L', 'ML Model Lab')
dot.node('S', 'ML Model Stage')
dot.node('P', 'ML Model Production')
dot.node('M', 'ML Model Monitoring')
dot.edges(['DL', 'DS', 'DP',])
dot.edges(['LM', 'SM', 'PM',])
dot.edge('L', 'S', )#constraint='false')
dot.edge('S', 'P', )# constraint='false')
dot.edges(['PD'])
print(dot.source)
dot.render('test-output/round-table.jpg', view=True)
dot

=>

Figure 2. Graph inline Colab instance by graphviz.

Draw cloud vendor architecture Directed Acyclic Graphs with Diagrams.

!pip install diagrams
import diagrams
from diagrams import Diagram , Edge
from diagrams.gcp.ml import AdvancedSolutionsLab
from diagrams.gcp.ml import AIHub
from diagrams.gcp.ml import AIPlatformDataLabelingService
from diagrams.gcp.ml import AIPlatform
from diagrams.gcp.ml import InferenceAPI
from diagrams.gcp.ml import JobsAPI
from diagrams.gcp.ml import AutomlNaturalLanguage
from diagrams.gcp.ml import AutomlTranslation
from diagrams.gcp.ml import SpeechToText
from diagrams.gcp.ml import TextToSpeech
from diagrams.gcp.ml import TranslationAPI
from diagrams.gcp.ml import NaturalLanguageAPI
from diagrams.gcp.ml import DialogFlowEnterpriseEdition
from diagrams.gcp.ml import TPU
from diagrams.gcp.ml import AutomlVideoIntelligence
from diagrams.gcp.ml import VideoIntelligenceAPI
from diagrams.gcp.ml import VisionAPI

=>

Figure 3. Graph inline Colab instance by diagrams.

AI/ML

Trending AI/ML Article Identified & Digested via Granola by Ramsey Elbasheer; a Machine-Driven RSS Bot

%d bloggers like this: