Sunday, February 21, 2021
Friday, January 22, 2021
Artificial Neural Network (ANN) using Python
Step 1: Importing Libraries For our calculations we will be using numpy
from IPython.core.display import display, HTML
display(HTML("<style>.container { width:103% !important;margin-left: -2%; }</style>"))
import numpy as np
Step 2:Creating a dummy representative input and output Lets create an input array.This will be a simple binary input with all combinations of 0 and 1
input_features=np.array([[0,1],[1,0],[0,0],[1,1]])
input_features
array([[0, 1], [1, 0], [0, 0], [1, 1]])
# It has 4 rows and 2 colums
input_features.shape
(4, 2)
target_output = np.array([[0,1,1,1]]).T
target_output
array([[0], [1], [1], [1]])
Step 3:Assigning random wegiths to the input Since we have two input elements, so there will be two weights involved
weights = np.array([[0.1],[0.2]])
weights
array([[0.1], [0.2]])
Step 4:Adding a Bias value to the input By default, the bias is assigned a value equal to 1.However, the weight assigned to it will be random initially
bias=0.3
Step 5:Defining Learning Rate It is another configurable parameter used in training neural network. It often ranges between 0-1 and is given a small value.It controls how quickly model is adapted to the problem at hand
lr=0.05
Step 6:Applying a Sigmoid Function The problem we are trying to model is a classification problem.Whatever inputs we are going to have, the output will always be restricted between 0 and 1.So sigmoid function fits the bill here.It take an input and generates the output between 0 and 1
def sigmoid(x):
z=1/(1+np.exp(-x))
return(z)
# Testing the function by giving it a value of 2
sigmoid(2)
0.8807970779778823
Step 7:Derivative of the Sigmoid function
def sigmoid_der(x):
z=sigmoid(x)*(1-sigmoid(x))
return(z)
Step 8:Main Logic for ANN Here we will be calculating the following things
- Feedforward Inputs
- Feedforward Output
- Backpropagation(Error Calculation)
- Calculating Derivative
- Multiplying Derivative
- Updating Weights
- Updating Bias Weights
We will run the code for 10,000 times to get the weights and bias right.Each training cycle of the data is known as an epoch
weights = np.array([[0.1],[0.2]])
weights
bias=0.3
lr=0.05
for epochs in range(10000):
input = input_features
# Feedforward inputs
in_o = np.dot(input,weights) + bias
# Feedforward output
out_o = sigmoid(in_o)
# Backpropagation:Calculation of error
error = out_o - target_output # error will be an array of shape (4,1)
x= error.sum()
#print(x)
# Calculating derivative of the error wrt weights
# 1.Derivative of Error wrt transormed output
derror_dout = error
# 2.Derivative of transormed output wrt transformed input
dout_dino = sigmoid_der(out_o)
# 3.input component of the derivative chain
inputs = input_features.T
# Multipying 1 and 2
deriv = derror_dout * dout_dino
# Multiplying deriv with 3 component
deriv_final = np.dot(inputs,deriv)
#print(out_o)
# Updating the weights value
weights -= lr*deriv_final
# Updating the bias value
for i in deriv:
bias -= lr*i
in_o
#sigmoid(in_o)
array([[-3.40015087], [10.74262174], [ 3.46549005], [ 3.87698081]])
# Checking weights and bias value
print(weights)
print(bias)
[[ 7.27733325] [-6.8658431 ]] [3.46559021]
Step 9:Predicting Values Lets look at the first row of inputs and output and predict the output based on the model parameters obtained
input_features[0]
array([0, 1])
expected_output = target_output[0]
expected_output
array([0])
# inputs with weight and bias applied
result1 = np.dot(input_features[0],weights) + bias
result1
array([-3.40025289])
# transfomed input
sigmoid(result1)
array([0.03228756])
# We can see that the output is around 0 which is equal to the expected output
Step 10:Putting it all together
Result1=np.dot(input_features,weights) + bias
Result1
array([[-3.40025289], [10.74292346], [ 3.46559021], [ 3.87708036]])
# transfomed input
sigmoid(Result1)
array([[0.03228756], [0.9999784 ], [0.96969269], [0.97970904]])
So it can be seen that the first element of the output is 0.03 which is close to 0. The rest of the outputs are close to 1. Hence our output is [0,1,1,1].
Through this simple example we saw how we can train an Artificial Neural Network (ANN) and understand how various components are set up and initialised
Web Scraping Tutorial 4- Getting the busy information data from Popular time page from Google
Popular Times Popular Times In this blog we will try to scrape the ...
-
Web Scraping using Rselenium Web Scraping using Rselenium Parag Verma...
-
Complete List of various topics in R Complete List of various topics in R Parag Verma Basics o...
-
Sensors are used in a lot of industrial applications to measure properties of a process. This can be temperature, pressure, humidity, den...