This article aims to implement a deep neural network from scratch. We will implement a deep neural network containing a hidden layer with four units and one output layer. The implementation will go from very scratch and the following steps will be implemented.
1. Visualizing the input data 2. Deciding the shapes of Weight and bias matrix 3. Initializing matrix, function to be used 4. Implementing the forward propagation method 5. Implementing the cost calculation 6. Backpropagation and optimizing 7. prediction and visualizing the output
Architecture of the model:
The architecture of the model has been defined by the following figure where the hidden layer uses the Hyperbolic Tangent as the activation function while the output layer, being the classification problem uses the sigmoid function.
Weights and bias:
The weights and the bias that is going to be used for both the layers have to be declared initially and also among them the weights will be declared randomly in order to avoid the same output of all units, while the bias will be initialized to zero. The calculation will be done from the scratch itself and according to the rules given below where W1, W2 and b1, b2 are the weights and bias of first and second layer respectively. Here A stands for the activation of a particular layer.
The cost function of the above model will pertain to the cost function used with logistic regression. Hence, in this tutorial we will be using the cost function:
Code: Visualizing the data
Code: Initializing the Weight and bias matrix
Here is the number of hidden units is four, so, the W1 weight matrix will be of shape (4, number of features) and bias matrix will be of shape (4, 1) which after broadcasting will add up to the weight matrix according to the above formula. Same can be applied to the W2.
Code: Forward Propagation :
Now we will perform the forward propagation using the W1, W2 and the bias b1, b2. In this step the corresponding outputs are calculated in the function defined as forward_prop.
Code: Defining the cost function :
Code: Finally back-propagating function:
This is a very crucial step as it involves a lot of linear algebra for implementation of backpropagation of the deep neural nets. The Formulas for finding the derivatives can be derived with some mathematical concept of linear algebra, which we are not going to derive here. Just keep in mind that dZ, dW, db are the derivatives of the Cost function w.r.t Weighted sum, Weights, Bias of the layers.
Code: Training the custom model Now we will train the model using the functions defined above, the epochs can be put as per the convenience and power of the processing unit.
Output with learnt params
After training the model, take the weights and predict the outcomes using the forward_propagate function above then use the values to plot the figure of output. You will have similar output.
Deep Learning is a world in which the thrones are captured by the ones who get to the basics, so, try to develop the basics so strong that afterwards, you may be the developer of a new architecture of models which may revolutionalize the community.
- How to move back and forward in History using Selenium Python ?
- ML - Neural Network Implementation in C++ From Scratch
- Back Propagation through time - RNN
- Implementation of neural network from scratch using NumPy
- ANN - Implementation of Self Organizing Neural Network (SONN) from Scratch
- LSTM - Derivation of Back propagation through time
- Deep Neural Network With L - Layers
- DeepPose: Human Pose Estimation via Deep Neural Networks
- ML | Naive Bayes Scratch Implementation using Python
- Python Django | Google authentication and Fetching mails from scratch
- forward driver method - Selenium Python
- A single neuron neural network in Python
- back driver method - Selenium Python
- Implementing Artificial Neural Network training process in Python
- turtle.forward() method in Python-Turtle
- Bidirectional Associative Memory (BAM) Implementation from Scratch
- Affinity Propagation in ML | To find the number of clusters
- Gauss's Forward Interpolation
- Difference between Backward and Forward chaining
- copy in Python (Deep Copy and Shallow Copy)
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.