Day 41: 60 days of Data Science and Machine Learning Series
Neural Network with a project..

Welcome back peeps. In this post we are going to understand the basics of Neural Network with Tensorflow with a project.
Some of the other best Series —
100 days : Your Data Science and Machine Learning Degree Series with projects
Complete Data Visualization and Pre-processing Series with projects
Projects Videos —
All the projects, data structures, SQL, algorithms, system design, Data Science and ML , Data Analytics, Data Engineering, , Implemented Data Science and ML projects, Implemented Data Engineering Projects, Implemented Deep Learning Projects, Implemented Machine Learning Ops Projects, Implemented Time Series Analysis and Forecasting Projects, Implemented Applied Machine Learning Projects, Implemented Tensorflow and Keras Projects, Implemented PyTorch Projects, Implemented Scikit Learn Projects, Implemented Big Data Projects, Implemented Cloud Machine Learning Projects, Implemented Neural Networks Projects, Implemented OpenCV Projects,Complete ML Research Papers Summarized, Implemented Data Analytics projects, Implemented Data Visualization Projects, Implemented Data Mining Projects, Implemented Natural Leaning Processing Projects, MLOps and Deep Learning, Applied Machine Learning with Projects Series, PyTorch with Projects Series, Tensorflow and Keras with Projects Series, Scikit Learn Series with Projects, Time Series Analysis and Forecasting with Projects Series, ML System Design Case Studies Series videos will be published on our youtube channel ( just launched).
Subscribe today!
Tech Newsletter —
If you are interested, you can join my newsletter through which I send tech interview tips, techniques, patterns, hacks — Software Development, ML, Data Science, Startups and Technology projects to more than 30K readers. You can subscribe to Tech Brew :
Neural Network in simple terms is an interconnected group of nodes which take input along with information from other nodes, develop output without programmed rules.

A neural network is a type of machine learning algorithm that is inspired by the structure and function of the human brain.
It is a set of algorithms that is designed to recognize patterns and make predictions from data. Neural networks are composed of layers of interconnected nodes, called artificial neurons, which are modeled after the biological neurons in the human brain.
There are different types of neural networks, each with its own architecture and characteristics. Some popular types of neural networks include:
- Feedforward neural networks: Also known as multi-layer perceptrons, these are the simplest type of neural network. They consist of an input layer, one or more hidden layers, and an output layer. Data flows in one direction from the input layer through the hidden layers to the output layer.
- Convolutional neural networks (CNNs): These are neural networks that are designed to process image data. They use convolutional layers, which scan the image with a filter and extract features from it, and pooling layers, which reduce the spatial dimensions of the image to make it more manageable.
- Recurrent neural networks (RNNs): These are neural networks that are designed to process sequential data, such as time series or natural language. They use feedback connections, which allow the network to maintain a state and process data in a step-by-step fashion.
- Autoencoder: Autoencoders are neural networks that are trained to reconstruct their inputs. They consist of an encoder and a decoder. The encoder maps the input to a lower-dimensional representation, and the decoder maps the representation back to the original input. Autoencoders are used for tasks such as dimensionality reduction, anomaly detection and feature learning.
- Generative Adversarial Networks (GANs): GANs are neural networks that are used for generative modeling. They consist of two networks: a generator and a discriminator. The generator produces new samples, while the discriminator tries to distinguish the generated samples from real samples. The two networks are trained together in a game-like manner, where the generator tries to produce samples that can fool the discriminator, while the discriminator tries to correctly identify the generated samples.
Neural networks are trained using a large dataset and the process of adjusting the parameters of the network called backpropagation . The training process adjusts the weights of the network to minimize the difference between the predicted output and the actual output.
How Neural Network works —
The basic building block of a neural network is the artificial neuron, which consists of an input, a set of weights, a bias term, an activation function, and an output.
- The input is the data that the neuron receives from the previous layer.
- The weights are the parameters of the neuron that are used to adjust the strength of the input.
- The bias term is a constant that is added to the weighted input.
- The activation function is a mathematical function that is applied to the weighted input plus the bias term to produce the output of the neuron.
- The output is the value that the neuron generates and passes on to the next layer.
The overall behavior of a neural network is determined by the architecture of the network, which is the number of layers and the number of neurons in each layer, and the values of the weights and biases.
The neural network is trained using a large dataset, where the input is presented to the network, and the output is compared to the desired output. The difference between the predicted output and the actual output is used to adjust the weights and biases of the network so that the network can make better predictions in the future. This process is called backpropagation.
During the training process, the network adjusts the weights and biases so that the difference between the predicted output and the actual output is minimized. Once the training is completed, the network can be used to make predictions on new, unseen data.
In summary, a neural network is a type of machine learning algorithm that is inspired by the structure and function of the human brain. Neural networks consist of layers of interconnected nodes called artificial neurons. The network is trained using a large dataset and the process of adjusting the parameters of the network called backpropagation. The training process adjusts the weights of the network to minimize the difference between the predicted output and the actual output.
A good reference point to understand the vastness of Neural Network —
Tensorflow is an open source platform for machine learning and deep learning developed by Google Brain Team and written in C++, Python, and CUDA created for large numerical computations and deep learning. It ingests the data in the form of tensors which are nothing but multi-dimensional arrays of higher dimensions to handle large amounts of data. It works on the data flow graphs that have nodes and edges and supports both CPUs and GPUs. It works by preprocessing the data, building the model, training and estimating the model.

How Tensorflow works —
- TensorFlow works by defining a computation graph, which is a directed acyclic graph (DAG) of operations, called nodes, that are connected by edges, called tensors. Each node in the graph represents a mathematical operation, such as a matrix multiplication, and each edge represents the flow of data, called tensors, between the nodes. The graph is defined using a high-level programming language, such as Python, and can be executed on a variety of platforms, such as CPUs, GPUs, and TPUs.
- The computation graph is built using the TensorFlow API, which provides a set of functions and classes that can be used to define the operations and tensors in the graph. The graph can be executed using TensorFlow’s session API, which allows developers to feed input data into the graph and retrieve the output from the graph.
- The training process of a neural network model in Tensorflow is done using a technique called back propagation. Backpropagation is an algorithm for supervised learning of artificial neural networks using gradient descent. Given an arbitrary function, the algorithm computes the gradient of the function with respect to each weight by the chain rule, computing the gradient one layer at a time, iterating backward from the last layer to avoid redundant calculations of intermediate terms in the chain rule.
- Once the graph is executed, TensorFlow automatically differentiates the graph to compute gradients, and optimizes the computation graph to make it more efficient. TensorFlow also provides a suite of tools for monitoring and visualizing the performance of the computation graph, such as TensorBoard, which allows developers to inspect the graph, the tensors, and the performance metrics in real-time.
In summary, TensorFlow is an open-source library for machine learning and deep learning that allows developers to create and deploy machine learning models easily. It works by defining a computation graph of operations and tensors that can be executed on a variety of platforms.
The training process of a neural network model in Tensorflow is done using back propagation and Tensorflow automatically differentiates the graph to compute gradients, and optimizes the computation graph to make it more efficient.
A good reference to Tensorflow ( used in this project as well ) —
In this project we are going to learn how to create, train and test a CNN with a project. The data for this project can be found here —
Let’s dive in!
Import necessary libraries
import tensorflow as tf
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.optimizers import RMSprop
%matplotlib inline
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import os
import zipfile
import numpy as np
from keras.preprocessing import image
import random
from tensorflow.keras.preprocessing.image import img_to_array, load_imgImport and Explore the Dataset
train_horse_dir = os.path.join('Path to the data file/horses')train_human_dir = os.path.join('Path to the data file/humans')validation_horse_dir = os.path.join('Path to the data file/horses')validation_human_dir = os.path.join('Path to the data file/humans')train_horse_names = os.listdir(train_horse_dir)
print(train_horse_names[:10])train_human_names = os.listdir(train_human_dir)
print(train_human_names[:10])validation_horse_hames = os.listdir(validation_horse_dir)
print(validation_horse_hames[:10])validation_human_names = os.listdir(validation_human_dir)
print(validation_human_names[:10])print('total training horse images:', len(os.listdir(train_horse_dir)))
print('total training human images:', len(os.listdir(train_human_dir)))
print('total validation horse images:', len(os.listdir(validation_horse_dir)))
print('total validation human images:', len(os.listdir(validation_human_dir)))Output —
total training horse images: 500
total training human images: 527
total validation horse images: 128
total validation human images: 128Explore Images
nrows = 4
ncols = 4
pic_index = 0
fig = plt.gcf()fig.set_size_inches(ncols * 4, nrows * 4)pic_index += 8
next_horse_pix = [os.path.join(train_horse_dir, fname)
for fname in train_horse_names[pic_index-8:pic_index]]
next_human_pix = [os.path.join(train_human_dir, fname)
for fname in train_human_names[pic_index-8:pic_index]]for i, img_path in enumerate(next_horse_pix+next_human_pix):
# Set up subplot; subplot indices start at 1
sp = plt.subplot(nrows, ncols, i + 1)
sp.axis('Off') # Don't show axes (or gridlines)Build a Neural Network Model from Scratch
model = tf.keras.models.Sequential([
# The input shape here is the desired size of the image 300x300 ( 3 bytes color )
# This is the first convolution
tf.keras.layers.Conv2D(16, (3,3), activation='relu', input_shape=(300, 300, 3)),
tf.keras.layers.MaxPooling2D(2, 2),
# The second convolution
tf.keras.layers.Conv2D(32,(3,3),activation = 'relu'),
tf.keras.layers.MaxPooling2D(2,2),
# The third convolution
tf.keras.layers.Conv2D(64,(3,3),activation = 'relu'),
tf.keras.layers.MaxPooling2D(2,2),
# The fourth convolution
tf.keras.layers.Conv2D(64,(3,3),activation = 'relu'),
tf.keras.layers.MaxPooling2D(2,2),
# The fifth convolution
tf.keras.layers.Conv2D(64,(3,3),activation = 'relu'),
tf.keras.layers.MaxPooling2D(2,2),
# The sixth convolution
tf.keras.layers.Conv2D(64,(3,3),activation = 'relu'),
tf.keras.layers.MaxPooling2D(2,2),
# Flatten the results to feed into a DNN
tf.keras.layers.Flatten(),
# 512 neuron hidden layer
tf.keras.layers.Dense(512, activation='relu'),
# We have only 1 output neuron
# and it will contain a value from 0-1 where 0 for 1 class ('horses') and 1 for the other ('humans')
tf.keras.layers.Dense(1, activation='sigmoid')
])
model.summary()
model.compile(loss='binary_crossentropy',
optimizer=RMSprop(lr=0.001),
metrics=['accuracy'])Output —
Model: "sequential"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
conv2d (Conv2D) (None, 298, 298, 16) 448
_________________________________________________________________
max_pooling2d (MaxPooling2D) (None, 149, 149, 16) 0
_________________________________________________________________
conv2d_1 (Conv2D) (None, 147, 147, 32) 4640
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (None, 73, 73, 32) 0
_________________________________________________________________
conv2d_2 (Conv2D) (None, 71, 71, 64) 18496
_________________________________________________________________
max_pooling2d_2 (MaxPooling2 (None, 35, 35, 64) 0
_________________________________________________________________
conv2d_3 (Conv2D) (None, 33, 33, 64) 36928
_________________________________________________________________
max_pooling2d_3 (MaxPooling2 (None, 16, 16, 64) 0
_________________________________________________________________
conv2d_4 (Conv2D) (None, 14, 14, 64) 36928
_________________________________________________________________
max_pooling2d_4 (MaxPooling2 (None, 7, 7, 64) 0
_________________________________________________________________
conv2d_5 (Conv2D) (None, 5, 5, 64) 36928
_________________________________________________________________
max_pooling2d_5 (MaxPooling2 (None, 2, 2, 64) 0
_________________________________________________________________
flatten (Flatten) (None, 256) 0
_________________________________________________________________
dense (Dense) (None, 512) 131584
_________________________________________________________________
dense_1 (Dense) (None, 1) 513
=================================================================
Total params: 266,465
Trainable params: 266,465
Non-trainable params: 0
_________________________________________________________________Data Preprocessing and Train the Model
# All images will be rescaled by 1./255
train_datagen = ImageDataGenerator(rescale=1/255)
validation_datagen = ImageDataGenerator(rescale=1/255)# Flow training images in batches of 128 using train_datagen generator
train_generator = train_datagen.flow_from_directory(
'horse-or-human/',
target_size=(300, 300),
batch_size=128,
class_mode='binary')# Flow training images in batches of 128 using train_datagen generator
validation_generator = validation_datagen.flow_from_directory(
'validation-horse-or-human/',
target_size=(300, 300),
batch_size=32,
class_mode='binary')history = model.fit(
train_generator,
steps_per_epoch= 8,
epochs= 15,
verbose=1,
validation_data = validation_generator,
validation_steps=8)Visualize
acc = history.history['accuracy']
val_acc = history.history['val_accuracy']
loss = history.history['loss']
val_loss = history.history['val_loss']epochs = range(len(acc))plt.plot(epochs, acc, 'bo', label='Training accuracy')
plt.plot(epochs, val_acc, 'b', label='Validation accuracy')
plt.title('Training and validation accuracy')plt.figure()plt.plot(epochs, loss, 'bo', label='Training Loss')
plt.plot(epochs, val_loss, 'b', label='Validation Loss')
plt.title('Training and validation loss')
plt.legend()plt.show()
Learnings —
How to create, train, and test a convolutional neural network with Tensorflow.
Day 42: Coming soon!
Follow and Stay tuned. Keep coding :)
For other projects, tune to —
Build Machine Learning Pipelines( With Code)
Recurrent Neural Network with Keras
Clustering Geolocation Data in Python using DBSCAN and K-Means
Facial Expression Recognition using Keras
Hyperparameter Tuning with Keras Tuner
Custom Layers in Keras
That’s it fellas. Peace out and keep coding :)
Stay Tuned and of-course let me end this post with a quote by Steve Jobs ;)
“You have to be burning with an idea, or a problem, or a wrong that you want to right. If you’re not passionate enough from the start, you’ll never stick it out.”





