Share
Explore

Using Google Collab Notebook to build an ANN (Our Coding tool for our assignment and Project)


Google Colab is a free cloud-based platform that allows users to write and run Python code in a Jupyter notebook environment.
It provides access to powerful computing resources, including GPUs and TPUs, and is a popular choice for building and training artificial neural networks. {Some for free, some you need to pay for.}
LAB LEARNING OUTCOMES:
- How to use Google Colab notebook to write Python code, import libraries, and read/write external files:

Introduction to Google Colab

Google Colab is a cloud-based platform that allows you to write and run Python code in a Jupyter notebook environment.
It provides access to powerful computing resources, including GPUs and TPUs, and is a popular choice for building and training artificial neural networks.

Setting up Google Colab

Open a browser and go to .
Click on "New Notebook" to create a new notebook.
In the first cell, import the necessary libraries:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

info

To create a new notebook in Google Colab, follow these steps:

Open a browser and go to .
Click on "New Notebook" to create a new notebook.
A new notebook will be created with an untitled name. You can rename the notebook by clicking on the "Untitled" text at the top of the notebook and typing in a new name.
You can start writing Python code in the first cell of the notebook.
Alternatively, you can create a new notebook from the File menu:
Open a browser and go to .
Click on "File" in the top left corner of the screen.
Click on "New Notebook" to create a new notebook.
A new notebook will be created with an untitled name. You can rename the notebook by clicking on the "Untitled" text at the top of the notebook and typing in a new name.
You can start writing Python code in the first cell of the notebook.

Google Colab notebooks consist of a list of cells, which can be edited and formatted in various ways. Here are the basic steps to edit and work with cells in the Google Colab notebook:
Types of Cells
Google Colab notebooks have two main types of cells:
Code cells: Allow you to write and run Python code.
Text cells: Let you add formatted text, images, and equations using Markdown.
Editing Text Cells
To edit a text cell in Google Colab:
Double-click on the text cell to enter edit mode.
Edit the Markdown source.
Press Shift+Enter to exit edit mode and render the Markdown.
You can also add new text cells by clicking the "+ Text" button in the toolbar.
Editing Code Cells
To edit a code cell in Google Colab:
Click on the code cell to select it.
Edit the Python code.
Press Shift+Enter to run the code and display the output.
You can also add new code cells by clicking the "+ Code" button in the toolbar.
Working with External Files
Google Colab allows you to read and write external files, such as CSV files, from your Google Drive. To do this: We will be doing this to make our Training Data available to our Model.
Mount your Google Drive:
python
from google.colab import drive
drive.mount('/content/drive')

Navigate to the directory where your file is located:
python
%cd /content/drive/MyDrive/Colab Notebooks/

Read the file using Pandas:
python : Pandas data frame
data = pd.read_csv('filename.csv')

Write the file using Pandas:
python
data.to_csv('filename.csv', index=False)

These are the basic steps to edit and work with cells in the Google Colab notebook. By mastering these steps, students can create and edit Python code, import libraries, and read/write external files in Google Colab.

These are the basic steps to create a new notebook in Google Colab. Once you have created a new notebook, you can start writing Python code, importing libraries, and reading/writing external files.

Importing Libraries

Python has a vast collection of libraries that can be used to perform various tasks.
Here are some of the most commonly used libraries in Google Colab:
NumPy: A library for numerical computing in Python.
Pandas: A library for data manipulation and analysis.
Matplotlib: A library for data visualization.
To import a library in Google Colab, simply use the import statement followed by the name of the library.

Reading and Writing External Files

Google Colab allows you to read and write external files, such as CSV files, from your Google Drive. Here's how to do it:
Mount your Google Drive:
python
from google.colab import drive
drive.mount('/content/drive')

Navigate to the directory where your file is located:
python
%cd /content/drive/MyDrive/Colab Notebooks/

Read the file using Pandas:
python
data = pd.read_csv('filename.csv')

Write the file using Pandas:
python
data.to_csv('filename.csv', index=False)

Building an Artificial Neural Network

Here's an example code to build a simple artificial neural network in Python using the Keras library:

import numpy as np
from keras.models import Sequential
from keras.layers import Dense

# Define the architecture of the neural network
model = Sequential()
model.add(Dense(8, input_dim=4, activation='relu'))
model.add(Dense(1, activation='sigmoid'))

# Compile the model
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])

# Load the dataset
dataset = np.loadtxt("pima-indians-diabetes.csv", delimiter=",")
X = dataset[:,0:8]
Y = dataset[:,8]

# Train the model
model.fit(X, Y, epochs=150, batch_size=10)

# Evaluate the model
scores = model.evaluate(X, Y)
print("%s: %.2f%%" % (model.metrics_names[1], scores[1]*100))

This code defines a neural network with one input layer, one hidden layer with 8 neurons, and one output layer with one neuron.
The activation function used in the hidden layer is ReLU, and the output layer uses the sigmoid activation function.
The model is compiled with the binary cross-entropy loss function, the Adam optimization algorithm, and the accuracy metric.
The dataset used in this example is the Pima Indians Diabetes dataset, which is loaded using the numpy library.
The model is trained for 150 epochs with a batch size of 10.
Finally, the model is evaluated on the same dataset, and the accuracy of the model is printed.

Conclusion

Google Colab is a powerful tool for building and training artificial neural networks.
It provides access to powerful computing resources and a wide range of libraries that can be used to perform various tasks.
By following the steps outlined in this Student Lab Workbook, students can learn how to use Google Colab to write Python code, import libraries, and read/write external files.
Want to print your doc?
This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (
CtrlP
) instead.