Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Building a Deep Neural Network in 6 Lines.
Prashant Basnet
Oct 26, 2024
236 views
Power of (Python + Keras) modern deep Learning tools:
This guide walks through the process of building and training an Artificial Neural Network (ANN) using TensorFlow's Keras library. We'll cover everything from initialization to training, with explanations of key concepts along the way.
In just 3 lines of code, we will see how we can create a deep neural network.
Setup and Imports
What is Artificial Neural Network (ANN)?
An ANN is a model inspired by the human brain's structure and function. Just as our brain has approximately 80 billion neurons organized in different layers, an ANN consists of artificial neurons arranged in a sequence of layers.
Brain Analogy:
ANN Structure:
Initializing Artificial neural network as sequence of layers. We will instantiate like
it's a sequential class, which allows exactly to build an ANN, but as a sequence of layers as opposed to computation graph.
here, we are a basic creating our neural network to be a sequential class.
Next, we will look into adding layers such as input, hidden and output layers.
First layer in neural network:
The way to add a fully connected layer into an artificial neural network is to use dense class from tensorflow.Keras. We are going to use this class to create our first layer.
in this dense function we can specify how many hidden neurons we want to have?
Famous question!!
It's a work of an artist. We need to pick 1 number here which wouldn't sound irrelevant or extravagant.
Adding first layer to our neural network, thus creating a shallow neural network .
Second layer (hidden) in neural network:
To transform from shallow network to deep network we simply add one more layer
just by copying the and pasting the same piece of code twice, we have a deep neural network.
What about the output layer?
How many neurons do we need at the output layer?
Depends on what we are doing? if we are predicting something like , a user will leave the app vs not. A user will leave the credit card company vs not. In binary classification case we have output of either 0 or 1. The output layer contains the dimension of the output you want to predict. So we want to pass 1 neurons.
What about the activation function?
we will be using sigmoid activation function instead of relu in the output. Why is that?Because sigmoid function allows to get not only he ultimate predictions but even better, it'll give you the probabilities that the binary outcome is 1 or 0.
So far our architecture looks like:
Thus creating a deep neural network in 3 lines of code:
Our model is not ready to be trained on the data yet. First we need to compile our ANN with:
then we will train our network .
Training Artificial Neural Network:
parameter for the function:
Now to train our ANN, we call our ann object.A function to train machine learning model is normally fit() method.
which almost always takes the same parameter.
when training a artificial neural network, we need to provide two more parameters.
here epochs, is number of times we want to iterate to make our model learn & become more accurate.
This we created our first artificial neural network. This is how we can use our model.
To conclude:
Python and TensorFlow offer an incredible toolkit in the world of deep learning. In just three lines of code, we can create a fully functional, three-layer neural network. With TensorFlow's Keras & Python enables anyone to construct deep learning models that can tackle complex problems, from image classification to language processing, with minimal code.
#python #artificialNeuralNetwork #deeplearning #machineLearning #datascience