Welcome To Colab - Colab
Welcome To Colab - Colab
Play with Gemini multimodal outputs, mixing text and images in an iterative way.
Discover the multimodal Live API (demo here).
Learn how to analyse images and detect items in your pictures using Gemini (bonus,
there's a 3D version as well!).
Unlock the power of the Gemini thinking model, capable of solving complex tasks with its
inner thoughts.
Use Gemini grounding capabilities to create a report on a company based on what the
model can find on the Internet.
Extract invoices and form data from PDFs in a structured way.
Create illustrations based on a whole book using Gemini large context window and
Imagen.
To learn more, take a look at the Gemini cookbook or visit the Gemini API documentation.
Colab now has AI features powered by Gemini. The video below provides information on how to
use these features, whether you're new to Python or a seasoned veteran.
https://colab.research.google.com/drive/1abM2zBdIJSKILJpF8DOYAOfkbV2Y-OiB#scrollTo=q5HLXu2LCFkq&printMode=true 1/36
21/07/2025, 00:24 Copy of Welcome to Colab - Colab
What is Colab?
Colab, or ‘Colaboratory’, allows you to write and execute Python in your browser, with
Whether you're a student, a data scientist or an AI researcher, Colab can make your work easier.
Watch Introduction to Colab or Colab features you may have missed to learn more or just get
started below!
For example, here is a code cell with a short Python script that computes a value, stores it in a
variable and prints the result:
seconds_in_a_day = 24 * 60 * 60
seconds_in_a_day
86400
To execute the code in the above cell, select it with a click and then either press the play button
to the left of the code, or use the keyboard shortcut 'Command/Ctrl+Enter'. To edit the code, just
click the cell and start editing.
Variables that you define in one cell can later be used in other cells:
seconds_in_a_week = 7 * seconds_in_a_day
seconds_in_a_week
604800
Colab notebooks allow you to combine executable code and rich text in a single document,
along with images, HTML, LaTeX and more. When you create your own Colab notebooks, they
are stored in your Google Drive account. You can easily share your Colab notebooks with co-
workers or friends, allowing them to comment on your notebooks or even edit them. To find out
more, see Overview of Colab. To create a new Colab notebook you can use the File menu above,
or use the following link: Create a new Colab notebook.
Colab notebooks are Jupyter notebooks that are hosted by Colab. To find out more about the
Jupyter project, see jupyter.org.
https://colab.research.google.com/drive/1abM2zBdIJSKILJpF8DOYAOfkbV2Y-OiB#scrollTo=q5HLXu2LCFkq&printMode=true 2/36
21/07/2025, 00:24 Copy of Welcome to Colab - Colab
You can import your own data into Colab notebooks from your Google Drive account, including
from spreadsheets, as well as from GitHub and many other sources. To find out more about
importing data, and how Colab can be used for data science, see the links below under Working
with data.
import numpy as np
import IPython.display as display
from matplotlib import pyplot as plt
import io
import base64
ys = 200 + np.random.randn(100)
x = [x for x in range(len(ys))]
data = io.BytesIO()
plt.savefig(data)
image = F"data:image/png;base64,{base64.b64encode(data.getvalue()).decode()}"
alt = "Sample Visualization"
display.display(display.Markdown(F""""""))
plt.close(fig)
https://colab.research.google.com/drive/1abM2zBdIJSKILJpF8DOYAOfkbV2Y-OiB#scrollTo=q5HLXu2LCFkq&printMode=true 3/36
21/07/2025, 00:24 Copy of Welcome to Colab - Colab
Colab notebooks execute code on Google's cloud servers, meaning that you can leverage the
power of Google hardware, including GPUs and TPUs, regardless of the power of your machine.
All you need is a browser.
For example, if you find yourself waiting for pandas code to finish running and want to go faster,
you can switch to a GPU runtime and use libraries like RAPIDS cuDF that provide zero-code-
change acceleration.
To learn more about accelerating pandas on Colab, see the 10-minute guide or US stock market
data analysis demo.
Colab is used extensively in the machine learning community with applications including:
To see sample Colab notebooks that demonstrate machine learning applications, see the
machine learning examples below.
https://colab.research.google.com/drive/1abM2zBdIJSKILJpF8DOYAOfkbV2Y-OiB#scrollTo=q5HLXu2LCFkq&printMode=true 4/36
21/07/2025, 00:24 Copy of Welcome to Colab - Colab
Machine learning
These are a few of the notebooks related to machine learning, including Google's online
machine learning course. See the full course website for more.
Intro to Pandas DataFrame
Intro to RAPIDS cuDF to accelerate pandas
Getting started with cuML's accelerator mode
Linear regression with tf.keras using synthetic data
import tensorflow as tf
from tensorflow.keras import layers, models, optimizers
from sklearn.metrics import confusion_matrix, accuracy_score, precision_score, recall_sco
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
import itertools
import gc
# Build model
model = models.Sequential([
layers.Input(shape=(28*28,)),
layers.Dense(128, activation='relu'),
layers.Dropout(0.2),
layers.Dense(64, activation='relu'),
layers.Dense(10, activation='softmax')
])
https://colab.research.google.com/drive/1abM2zBdIJSKILJpF8DOYAOfkbV2Y-OiB#scrollTo=q5HLXu2LCFkq&printMode=true 6/36
21/07/2025, 00:24 Copy of Welcome to Colab - Colab
# Compile model
model.compile(
optimizer=optimizer,
loss='sparse_categorical_crossentropy',
metrics=['accuracy']
)
try:
# Train model
history = model.fit(
x_train, y_train,
epochs=epochs,
batch_size=batch_size,
validation_split=0.1,
verbose=0
)
# Compute metrics
cm = confusion_matrix(y_test, y_pred)
acc = accuracy_score(y_test, y_pred)
prec_micro = precision_score(y_test, y_pred, average='micro', zero_divisi
prec_macro = precision_score(y_test, y_pred, average='macro', zero_divisi
rec_micro = recall_score(y_test, y_pred, average='micro', zero_division=0
rec_macro = recall_score(y_test, y_pred, average='macro', zero_division=0
f1_micro = f1_score(y_test, y_pred, average='micro', zero_division=0)
f1_macro = f1_score(y_test, y_pred, average='macro', zero_division=0)
# Store results
result_dict = {
'epochs': epochs,
'optimizer': opt_name,
'batch_size': batch_size,
'confusion_matrix': cm,
'accuracy': acc,
'precision_micro': prec_micro,
'precision_macro': prec_macro,
'recall_micro': rec_micro,
'recall_macro': rec_macro,
'f1_micro': f1_micro,
'f1_macro': f1_macro,
'final_train_acc': history.history['accuracy'][-1],
'final_val_acc': history.history['val_accuracy'][-1]
}
results.append(result_dict)
except Exception as e:
print(f"Error in experiment: {str(e)}")
continue
# Clean up memory
del model, optimizer
gc.collect()
https://colab.research.google.com/drive/1abM2zBdIJSKILJpF8DOYAOfkbV2Y-OiB#scrollTo=q5HLXu2LCFkq&printMode=true 8/36
21/07/2025, 00:24 Copy of Welcome to Colab - Colab
print("\n" + "-"*60)
print("F1-MACRO COMPARISON BY OPTIMIZER AND BATCH SIZE")
print("-"*60)
f1_pivot = df_results.pivot_table(
index=['Optimizer', 'Batch_Size'],
columns='Epochs',
values='F1_Macro',
aggfunc='mean'
).round(4)
print(f1_pivot)
# Summary statistics
print("\n" + "-"*60)
print("SUMMARY STATISTICS")
print("-"*60)
print("Mean Accuracy:", df_results['Accuracy'].mean().round(4))
print("Std Accuracy:", df_results['Accuracy'].std().round(4))
print("Best Accuracy:", df_results['Accuracy'].max().round(4))
print("Worst Accuracy:", df_results['Accuracy'].min().round(4))
plt.subplot(2, 3, 5)
df_results.boxplot(column='F1_Macro', by='Batch_Size', ax=plt.gca())
plt.title('F1-Macro by Batch Size')
plt.suptitle('')
plt.tight_layout()
plt.show()
https://colab.research.google.com/drive/1abM2zBdIJSKILJpF8DOYAOfkbV2Y-OiB#scrollTo=q5HLXu2LCFkq&printMode=true 10/36
21/07/2025, 00:24 Copy of Welcome to Colab - Colab
https://colab.research.google.com/drive/1abM2zBdIJSKILJpF8DOYAOfkbV2Y-OiB#scrollTo=q5HLXu2LCFkq&printMode=true 12/36
21/07/2025, 00:24 Copy of Welcome to Colab - Colab
https://colab.research.google.com/drive/1abM2zBdIJSKILJpF8DOYAOfkbV2Y-OiB#scrollTo=q5HLXu2LCFkq&printMode=true 13/36
21/07/2025, 00:24 Copy of Welcome to Colab - Colab
https://colab.research.google.com/drive/1abM2zBdIJSKILJpF8DOYAOfkbV2Y-OiB#scrollTo=q5HLXu2LCFkq&printMode=true 14/36
21/07/2025, 00:24 Copy of Welcome to Colab - Colab
https://colab.research.google.com/drive/1abM2zBdIJSKILJpF8DOYAOfkbV2Y-OiB#scrollTo=q5HLXu2LCFkq&printMode=true 15/36
21/07/2025, 00:24 Copy of Welcome to Colab - Colab
https://colab.research.google.com/drive/1abM2zBdIJSKILJpF8DOYAOfkbV2Y-OiB#scrollTo=q5HLXu2LCFkq&printMode=true 16/36
21/07/2025, 00:24 Copy of Welcome to Colab - Colab
https://colab.research.google.com/drive/1abM2zBdIJSKILJpF8DOYAOfkbV2Y-OiB#scrollTo=q5HLXu2LCFkq&printMode=true 17/36
21/07/2025, 00:24 Copy of Welcome to Colab - Colab
https://colab.research.google.com/drive/1abM2zBdIJSKILJpF8DOYAOfkbV2Y-OiB#scrollTo=q5HLXu2LCFkq&printMode=true 18/36
21/07/2025, 00:24 Copy of Welcome to Colab - Colab
Experiment 14/36: Epochs=15, Optimizer=SGD, Batch Size=1000
Accuracy: 0.8796, F1-macro: 0.8773
https://colab.research.google.com/drive/1abM2zBdIJSKILJpF8DOYAOfkbV2Y-OiB#scrollTo=q5HLXu2LCFkq&printMode=true 19/36
21/07/2025, 00:24 Copy of Welcome to Colab - Colab
https://colab.research.google.com/drive/1abM2zBdIJSKILJpF8DOYAOfkbV2Y-OiB#scrollTo=q5HLXu2LCFkq&printMode=true 20/36
21/07/2025, 00:24 Copy of Welcome to Colab - Colab
https://colab.research.google.com/drive/1abM2zBdIJSKILJpF8DOYAOfkbV2Y-OiB#scrollTo=q5HLXu2LCFkq&printMode=true 21/36
21/07/2025, 00:24 Copy of Welcome to Colab - Colab
https://colab.research.google.com/drive/1abM2zBdIJSKILJpF8DOYAOfkbV2Y-OiB#scrollTo=q5HLXu2LCFkq&printMode=true 22/36
21/07/2025, 00:24 Copy of Welcome to Colab - Colab
https://colab.research.google.com/drive/1abM2zBdIJSKILJpF8DOYAOfkbV2Y-OiB#scrollTo=q5HLXu2LCFkq&printMode=true 23/36
21/07/2025, 00:24 Copy of Welcome to Colab - Colab
https://colab.research.google.com/drive/1abM2zBdIJSKILJpF8DOYAOfkbV2Y-OiB#scrollTo=q5HLXu2LCFkq&printMode=true 24/36
21/07/2025, 00:24 Copy of Welcome to Colab - Colab
https://colab.research.google.com/drive/1abM2zBdIJSKILJpF8DOYAOfkbV2Y-OiB#scrollTo=q5HLXu2LCFkq&printMode=true 25/36
21/07/2025, 00:24 Copy of Welcome to Colab - Colab
https://colab.research.google.com/drive/1abM2zBdIJSKILJpF8DOYAOfkbV2Y-OiB#scrollTo=q5HLXu2LCFkq&printMode=true 26/36
21/07/2025, 00:24 Copy of Welcome to Colab - Colab
https://colab.research.google.com/drive/1abM2zBdIJSKILJpF8DOYAOfkbV2Y-OiB#scrollTo=q5HLXu2LCFkq&printMode=true 27/36
21/07/2025, 00:24 Copy of Welcome to Colab - Colab
https://colab.research.google.com/drive/1abM2zBdIJSKILJpF8DOYAOfkbV2Y-OiB#scrollTo=q5HLXu2LCFkq&printMode=true 28/36
21/07/2025, 00:24 Copy of Welcome to Colab - Colab
https://colab.research.google.com/drive/1abM2zBdIJSKILJpF8DOYAOfkbV2Y-OiB#scrollTo=q5HLXu2LCFkq&printMode=true 29/36
21/07/2025, 00:24 Copy of Welcome to Colab - Colab
https://colab.research.google.com/drive/1abM2zBdIJSKILJpF8DOYAOfkbV2Y-OiB#scrollTo=q5HLXu2LCFkq&printMode=true 30/36
21/07/2025, 00:24 Copy of Welcome to Colab - Colab
================================================================================
RESULTS SUMMARY
================================================================================
------------------------------------------------------------
ACCURACY COMPARISON BY OPTIMIZER AND BATCH SIZE
------------------------------------------------------------
Epochs 10 15 20
Optimizer Batch_Size
Adagrad 100 0.9585 0.9651 0.9701
1000 0.9069 0.9186 0.9246
5000 0.8255 0.8597 0.8771
Adam 100 0.9798 0.9788 0.9791
1000 0.9707 0.9735 0.9789
5000 0.9425 0.9515 0.9612
RMSprop 100 0.9811 0.9791 0.9792
1000 0.9718 0.9762 0.9784
5000 0.9326 0.9518 0.9547
SGD 100 0 9383 0 9492 0 9543
https://colab.research.google.com/drive/1abM2zBdIJSKILJpF8DOYAOfkbV2Y-OiB#scrollTo=q5HLXu2LCFkq&printMode=true 32/36
21/07/2025, 00:24 Copy of Welcome to Colab - Colab
SGD 100 0.9383 0.9492 0.9543
1000 0.8451 0.8796 0.8960
5000 0.5078 0.6714 0.7641
------------------------------------------------------------
F1-MACRO COMPARISON BY OPTIMIZER AND BATCH SIZE
------------------------------------------------------------
Epochs 10 15 20
Optimizer Batch_Size
Adagrad 100 0.9581 0.9649 0.9698
1000 0.9052 0.9174 0.9234
5000 0.8212 0.8564 0.8743
Adam 100 0.9797 0.9787 0.9789
1000 0.9705 0.9733 0.9787
5000 0.9418 0.9510 0.9609
RMSprop 100 0.9811 0.9790 0.9790
1000 0.9716 0.9760 0.9783
5000 0.9316 0.9513 0.9541
SGD 100 0.9375 0.9487 0.9537
1000 0.8409 0.8773 0.8942
5000 0.4319 0.6367 0.7479
------------------------------------------------------------
TOP 5 BEST PERFORMING COMBINATIONS (by Accuracy)
------------------------------------------------------------
Epochs Optimizer Batch_Size Accuracy F1_Macro
6 10 RMSprop 100 0.9811 0.9811
3 10 Adam 100 0.9798 0.9797
30 20 RMSprop 100 0.9792 0.9790
18 15 RMSprop 100 0.9791 0.9790
27 20 Adam 100 0.9791 0.9789
------------------------------------------------------------
SUMMARY STATISTICS
------------------------------------------------------------
Mean Accuracy: 0.9176
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
/tmp/ipython-input-39-2888371127.py in <cell line: 0>()
206 print("-"*60)
207 print("Mean Accuracy:", df_results['Accuracy'].mean().round(4))
--> 208 print("Std Accuracy:", df_results['Accuracy'].std().round(4))
209 print("Best Accuracy:", df_results['Accuracy'].max().round(4))
210 print("Worst Accuracy:", df_results['Accuracy'].min().round(4))
https://colab.research.google.com/drive/1abM2zBdIJSKILJpF8DOYAOfkbV2Y-OiB#scrollTo=q5HLXu2LCFkq&printMode=true 33/36
21/07/2025, 00:24 Copy of Welcome to Colab - Colab
https://colab.research.google.com/drive/1abM2zBdIJSKILJpF8DOYAOfkbV2Y-OiB#scrollTo=q5HLXu2LCFkq&printMode=true 34/36
21/07/2025, 00:24 Copy of Welcome to Colab - Colab
https://colab.research.google.com/drive/1abM2zBdIJSKILJpF8DOYAOfkbV2Y-OiB#scrollTo=q5HLXu2LCFkq&printMode=true 35/36
21/07/2025, 00:24 Copy of Welcome to Colab - Colab
https://colab.research.google.com/drive/1abM2zBdIJSKILJpF8DOYAOfkbV2Y-OiB#scrollTo=q5HLXu2LCFkq&printMode=true 36/36