Solution for XOR Problem With Neural Networks Usin
Solution for XOR Problem With Neural Networks Usin
net/publication/367470780
Solution for XOR Problem with Neural Networks Using Google Colab and
MATLAB / Simulink
CITATIONS READS
0 1,216
4 authors, including:
Viswanatha v. Ramachandra Ac
Research Visvesvaraya Technological University
63 PUBLICATIONS 413 CITATIONS 113 PUBLICATIONS 348 CITATIONS
All content following this page was uploaded by Viswanatha v. on 17 February 2023.
Solution for XOR Problem with Neural Networks Using Google Colab
and MATLAB
1*
/ Simulink
2 3 3
Viswanatha V , Ramachandra A C , Berwyn Suhas , Adithya T
1
Assistant Professor, Department of Electronics and Communication Engineering, Nitte Meenakshi Institute of Technology,
Bangalore, India
2
Professor, Department of Electronics and Communication Engineering, Nitte Meenakshi Institute of Technology, Bangalore, India
3
UG Student, Department of Electronics and Communication Engineering, Nitte Meenakshi Institute of Technology, Bangalore, India
Abstract
To find solution for XOR problem we are considering two widely used software that is being relied on by many software
developers for their work. The first software is Google Colab tool which can be used to implement ANN programs by
coding the neural network using python. This tool is reliable since it supports python language for its implementation.
The other major reason is that we can use GPU and TPU processors for the computation process of the neural network.
The major advantage of this is that for complex Neural Networks instead of using CPU present in the user‘s system we
can use those two processors through online mode without purchasing such processors for our computation. The next
software that can be used for implementing ANN is Matlab Simulink. This software is used for highly calculative and
computational tasks such as Control System, Deep Learning, Machine Learning, Digital Signal Processing and many
more. Matlab is highly efficient and easy to code and use when compared to any other software. This is because Matlab
stores data in the form of matrices and computes them in this fashion. Matlab in collaboration with Simulink can be used
to manually model the Neural Network without the need of any code or knowledge of any coding language. Since
Simulink is integrated with Matlab we can also code the Neural Network in Matlab and obtain its mathematically
equivalent model in Simulink. Also, Matlab has a dedicated tool in its library to implement neural network called NN
tool. Using this tool, we can directly add the data for input, desired output, or target. After inserting the required data, we
can train the network with the given dataset and receive appropriate graph‘s which will be helpful in analyzing the data.
Once the Neural Network is trained, we can test the network through this tool and verify the obtained results.
Keywords: XOR problem, Back propagation, Feature extraction, Deep learning.
Copyright © 2023 The Author(s): This is an open-access article distributed under the terms of the Creative Commons Attribution 4.0 International
License (CC BY-NC 4.0) which permits unrestricted use, distribution, and reproduction in any medium for non-commercial use provided the original
author and source are credited.
Citation: Viswanatha V, Ramachandra A C, Berwyn Suhas, Adithya T (2023). Solution for XOR Problem with Neural 16
Networks Using Google Colab and MATLAB / Simulink. Saudi J Eng Technol, 8(1): 16-28.
Viswanatha V et al; Saudi J Eng Technol, Jan, 2023; 8(1): 16-28
In this sector we are going to discuss about can be separated with a straight line plotted on the
implementing XOR gate shown in fig. 2 in the form of a graph, but that is not the case for XOR Gate. Hence this
Neural Network. Unlike the other basic gates such as gate cannot be realized with a basic neural network
AND, OR and NOT Gates, XOR gate cannot be containing an input and an output layer. Hence to solve
realised as a neural network easily. This is because of this problem we are using two hidden layer which lies
non-linearity condition which is observed in XOR gate between the input and output neuron. And also, we have
when compared to other basic gates [6-8]. All the to use back propagation algorithm to update the weights
outputs such as logic 0‘s and logic 1‘s of other gates and produce the desired output [9-11].
When we consider basic gates such as AND, line as shown in the fig.3 of XOR gate.. To solve this
OR and NOT gates we can realize these gates and issue we use multilayer perception which consists of
implement them into neural network easily using single input layers, hidden layers and output layers. The
layer perception consisting of only input layers and hidden layers are used to provide a non-linear property
output layers as shown in fig.3 of OR gate. But the to the neural network. Hence by using two hidden
same methodology cannot be used to implement an layers we can implement XOR gate in a neural network
XOR gate as the output of the XOR gate is not linear. [12-15].
Hence the outputs cannot be separated using a straight
© 2023 | Published by Scholars Middle East Publishers, Dubai, United Arab Emirates 17
Viswanatha V et al; Saudi J Eng Technol, Jan, 2023; 8(1): 16-28
The architecture shown in fig. 5 displays us the weight is calculated they must be added to their
application of the back propagation algorithm to Feed respective weight at their respective nodes. Once the
Forward XOR Neural Network. Here if the Neural weights are being updated, we are again testing the
network fails to give out the desired output. Hence, we network by forward propagation. If the output isn‘t
implement back propagation algorithm to tune the matching with the desired output still, then we again
weights. The error signal generated is taken as input and find error signal and implement back propagation again.
it is used for computation of change in weight. The This process is continued until the desired output is
change in weight must be calculated for each node being achieved from the neural network [18, 19].
present in the neural network. Once this change of
© 2023 | Published by Scholars Middle East Publishers, Dubai, United Arab Emirates 18
Viswanatha V et al; Saudi J Eng Technol, Jan, 2023; 8(1): 16-28
Fig. 5: Feed forward neural network architecture of non-linear system as XOR gate
3. IMPLEMENTATION AND RESULTS increase the efficiency of the network. Once the training
This section shows two different methods of is completed, we shall calculate the efficiency of the
implementing ANN for XOR gate: network using losses. By plotting losses v/s epochs we
3.1). Implementing ANN using Google Colab Using understand efficiency of the network increasing as the
Python number of epochs is increased. Epochs are usually kept
3.2) Implementing ANN using ANN using Matlab at higher value to give more iteration to improve neural
Simulink network. Final step is to print the actual output.
© 2023 | Published by Scholars Middle East Publishers, Dubai, United Arab Emirates 19
Viswanatha V et al; Saudi J Eng Technol, Jan, 2023; 8(1): 16-28
STEP 1: Import the Required Python Libraries In back propagation definition, first we have to
To create neural network import Numpy and calculate error signal value which is obtained by
name it as np. Also import matplotlib for plotting the subtracting desired output from the actual output. Using
graph for the given data. this error signal, we are updating the weights and bias
inputs using relevant formulas.
STEP 2: Define Activation Function [Sigmoid]
First, we are defining the activation function. STEP 5: Update the Weight And Bias Parameters
We are using sigmoid function which is the most used After back propagation has been implemented,
activation function in neural networks. the gradients obtained from it have to be used to update
FORMULA = 1/(1+exp(u)) the parameters.
Where, u = input data given to the sigmoid function. Wi = Wi – η* ΔWi
Where,
STEP 3: Initialize Neural Network Parameters Wi = weight to be updated
In this step we are initializing the parameters ΔWi = gradient value to be used to update weights
such as weights and bias to the neural network. W1 is η = learning rate
the weight present between input and hidden layer
node. W2 is the weight present between hidden layer Once the weights have been updated, we must
and output node. b1 and b2 are the biasing inputs given return them to the variable used to store which is named
at hidden layer and output layer respectively. as Parameter and implement the same in forward
propagation algorithm.
STEP 4: Define Forward Propagation and Back
Propagation Algorithm STEP 6: Train the Neural Network Model and Test It
After defining the parameters, we initialize Once all parameters and algorithms such as
them while defining forward propagation algorithm. Z1 forward and backward propagation algorithm, it is time
and Z2 holds the input to hidden layer node multiplied to call all these functions under training function to
with their associated weights. train the neural network. Here we are initializing the
FORMULA: inputs, desired output, epochs and learning rate to train
Zi = ∑Wij*Xi + bi the model. Once the model is trained, we must test it by
Where, Z = summation of input multiplied with their providing input via the log tab and verify the output. If
respective weights added with a bias. the output isnot matching with the desired output then
increases number of iterations or epochs to further train
After calculating Z1 and Z2 they are given as the network.
input to sigmoid function and the final output is stored
A1 and A2 respectively. A1 holds the final output value STEP 7: Performance Analysis
at hidden layer and A2 holds the final output at output To accurately measure the performance of the
layer. Cache is used to store the parameter values and neural network we are calculating losses. With respect
cost function is calculated to find error in actual output to epochs, we are plotting the graph of epochs V/s
obtained. losses where losses is set to high at the initial stage
before training the model.
© 2023 | Published by Scholars Middle East Publishers, Dubai, United Arab Emirates 20
Viswanatha V et al; Saudi J Eng Technol, Jan, 2023; 8(1): 16-28
From fig.7, we can observe that initially the summer is given as input to an activation function. The
loss is high, but as the neural network is trained its loss activation function used here is called ReLu function.
value decreases with increase in epochs. This function gives a output logic ‗0‘ if the input is less
than the value ‗0‘, and if the input is more than the
3.2). Implementing ANN Using Matlab Simulink: value ‗0‘ then the output is logic ‗1‘. After this process,
There are various ways to implement ANN in Matlab. the hidden layer output is connected to the output layer
One among them is Simulink where a neural network via a node which is also multiplied by its associated
can be built hands on using inbuilt library functional weights and then allowed to enter the output neuron.
blocks which are already predefined in the library. Also, The same procedure that of the hidden layer is followed
one can code the neural network in Matlab and the same in the output layer and the final output will be in terms
can be converted into Simulink model without creating of either logic‘0‘ or logic ‗1‘. Consider fig. 8, which is
the model manually. Hence this tool is very versatile representing XOR gate implemented in a neural
when compared to other tools regarding neural network. network using Matlab Simulink software. In this block
For our project implementing XOR gate using Simulink diagram there are three layers, they are Input Layers,
has been done manually where we have built an already Hidden Layers and one Output Layer. Scope block is
trained neural network and simulating its functionality predefined in Matlab Simulink which is used for
in Simulink software. In this model we have two input displaying the final output waveform of the XOR gate.
layers acting as input terminals of an XOR gate. Since Since the value of weights between input layer and
XOR gate gives a non-linear output, we are using two hidden layer is 1, we are ignoring them. The weight
hidden layers for compensating this non-linear property. (w3) between hidden layer 1 and output layer is -2, we
At input neuron, we are connecting clock signal to a are using a multiplier block which is predefined in
switch terminal, when the clock pulse is low output of Matlab Simulink to represent the weight (w3). In input
the switch gives an output of logic ‗0‘ and when the layer there are 2 input neurons, hidden layer there is 2
clock signal is high, the switch gives an output of logic neuron namely hidden layer 1 and hidden layer 2 and in
‗1‘. These output signals coming from input layer are output layer there is 1 output neuron. These neurons are
multiplied by weights which are already tuned to a subsystem defined in the Simulink library that has
provide the right functionality for the neural network. their own individual block diagram and its associated
At the hidden layer these input signals are added by a waveforms.
summer along with a bias input and the output of the
3.2.1). INPUT LAYER NEURON 1 design shown in fig.9 for input layer neuron 1, the
One of the distinct characteristics of the input corresponding output waveform of the input layer is
layer is that artificial neurons in the input layer have a obtained and as shown in fig.10. Here total time
different role. Passive neurons do not take the duration of the waveform is 10 units and for every 5
information from the previous layers because they are units of clock pulses applied waveform of input layer
the first layer of the neural network. Based upon the triggers.
© 2023 | Published by Scholars Middle East Publishers, Dubai, United Arab Emirates 21
Viswanatha V et al; Saudi J Eng Technol, Jan, 2023; 8(1): 16-28
3.2.2). INPUT LAYER NEURON 2 respectively. Based upon the design shown in fig.11,
This input neuron 2 is similar to the previous the corresponding output waveform is obtained and is
input neuron except for the time duration of 10 units the shown in fig.12.
output waveform triggers for every 2.5 seconds
© 2023 | Published by Scholars Middle East Publishers, Dubai, United Arab Emirates 22
Viswanatha V et al; Saudi J Eng Technol, Jan, 2023; 8(1): 16-28
3.2.3). HIDDEN NEURON 1 Hidden neural network layers are set up in many ways.
A hidden layer in an artificial neural network In some cases, weighted inputs are randomly assigned.
is a layer in between input layers and output layers, In other cases, they are fine-tuned and calibrated
where artificial neurons take in a set of weighted inputs through a process called back propagation. Based upon
and produce an output through an activation function. It the design of hidden layer of neuron 1 as shown in fig.
is a typical part of nearly any neural network in which 13, the corresponding output waveform of that neuron
we simulate the activities that occur in the human brain. is obtained and as shown in fig. 14.
© 2023 | Published by Scholars Middle East Publishers, Dubai, United Arab Emirates 23
Viswanatha V et al; Saudi J Eng Technol, Jan, 2023; 8(1): 16-28
3.2.4). HIDDEN NEURON 2 output present in the block diagram varies from -0.5 to
The block diagram of hidden layer 2 is similar 1.5. The activation function coverts the negative input
to the first hidden layer except the bias input differs. as logic 0. And the values above zero are set as high or
The bias input in the previous hidden layer is -1.5, but logic 1 by the activation function. Based upon the
here the input bias applied is -0.5. The output waveform design shown in fig.15 the corresponding output is
of the hidden layer 2 is shown below; the summation obtained and is as shown in fig. 16.
© 2023 | Published by Scholars Middle East Publishers, Dubai, United Arab Emirates 24
Viswanatha V et al; Saudi J Eng Technol, Jan, 2023; 8(1): 16-28
© 2023 | Published by Scholars Middle East Publishers, Dubai, United Arab Emirates 25
Viswanatha V et al; Saudi J Eng Technol, Jan, 2023; 8(1): 16-28
The fig.18 represents the output layer which second hidden layer node is having a weight associated
receives input from the two hidden layers. The first as 1.
hidden layer node is having a weight of -2, while the
3.2.6). OUTPUT LAYER represents the summation output, and the black line
The waveform shown in fig.19 is output represents the activation function.
waveform obtained from the output layer. The blue line
© 2023 | Published by Scholars Middle East Publishers, Dubai, United Arab Emirates 26
Viswanatha V et al; Saudi J Eng Technol, Jan, 2023; 8(1): 16-28
4. COMPARATIVE ANALYSIS
© 2023 | Published by Scholars Middle East Publishers, Dubai, United Arab Emirates 27
Viswanatha V et al; Saudi J Eng Technol, Jan, 2023; 8(1): 16-28
17. Ehrentraut, C., Ekholm, M., Tanushi, H., vector machines. Chinese Journal of Aerospace
Tiedemann, J., & Dalianis, H. (2016). Detecting Medicine, 14(3), 277–281.
hospital-acquired infections: a document 19. Viswanatha, V., Ashwini, K., & Pradeep, K.
classification approach using support vector (2021). Internet of things (IoT) based multilevel
machines and gradient tree boosting. Health drunken driving detection and prevention system
Informatics Journal, 24(1), 24–42. using Raspberry Pi 3. International Journal of
18. Zhang, X., Li, Y., & Peng, X. (2016). Brain wave Internet of Things and Web Services, 6.
recognition of word imagination based on support
© 2023 | Published by Scholars Middle East Publishers, Dubai, United Arab Emirates 28