Python Neural Network
Python Neural Network
Python Neural Network
Abstract—In this paper a basic introduction to neural Neural networks are defined as networks consisting of
networks is made. An emphasis is given on a two layer perceptron many interconnected neurons. A neuron (or nerve cell) is a
used extensively for function approximation. The special biological cell that processes information. Neurons
backpropagation learning rule is than briefly introduced. A short
are connected at joints called synapses. A typical neuron
introduction into Python programming language is made and a
program for the perceptron design is written and discussed in together with a synaptic joint is shown in Fig. 1.
some detail. The “neurolab” library is used for this purpose.
cell body
Index Terms—Neural networks, Perceptron, Python. axon
axon
synaptic terminal
synaptic vesicles
I. INTRODUCTION nucleus
synaptic gap
neurotransmiters
n a
n a 1R
x
Published By:
Blue Eyes Intelligence Engineering
Retrieval Number: F10750486S419/19©BEIESP 373 & Sciences Publication
DOI: 10.35940/ijitee.F1075.0486S419
Neural Network Programming In Python
(3) +S1
1x
f +S 1 2x
f + S3 1
x f
1 2 3
1 b 1 b 1 b
2x
The function f can be linear or nonlinear function of n. It S1 1
x
S 1 S3 1
x
R
is usually called the transfer function. Complicated neural
networks are of course composed of many neurons. A basic Fig. 6: Three-layer feedforward neural network
unit of complicated neural networks is a layer, which is
made of one or more parallel neurons. Neurons within the The output of such a three-layer feedforward neural
same layer have usually the same transfer function. The network is determined by the following equation:
output(s) of a neural network can in such a case be
determined by the following equation 𝒂 = 𝒇3 (𝑾𝟑 ∙ 𝒇2 𝑾𝟐 ∙ 𝒇1 𝑾𝟏 ∙ 𝒑 + 𝒃𝟏 + 𝒃𝟐 + 𝒃𝟑 ) (5)
p2 f SR
x
+
n
f a
most commonly used one) is the two-layer perceptron
b2 S1
x
S1 x
1 1 b S shown in Fig. 7.
S1
x
R
n1 a1 input logsig layer linear layer
pR w
S,R f
b1 1
p 1 a 2 a
2
Fig. 3: Schematic representation of a single layer R1 W 1x W 2x
x
1 S 1 S 1
1x
S R n 2x
SS
1
n2
+S1 1x +S 1 2x
Neural networks can in general be composed of more than
1 2
one layer. Based on the connections between layers neural 1 b 1 b
networks can be divided into intralayer, interlayer or S11x 2x
S 1
R
recurrent, as shown in Fig. 4.
Fig. 7: A two-layer perceptron used for function
approximation
bad
approximation
good
approximation
feedforward feedback
Fig. 5: Feedforward and feedback neural networks Fig. 8: Good and poor function approximation
Published By:
Blue Eyes Intelligence Engineering
Retrieval Number: F10750486S419/19©BEIESP 374 & Sciences Publication
DOI: 10.35940/ijitee.F1075.0486S419
International Journal of Innovative Technology and Exploring Engineering (IJITEE)
ISSN: 2278-3075, Volume-8, Issue-6S4, April 2019
Its main application is function approximation. A very The parameters mean that the network expects inputs in
important question however still has to be answered. So far, the [-10, 10] range. The hidden layer has 15 neurons and the
nothing was said about the process of determining the output layer 1 neuron. It should be emphasized that the
correct values of the parameters w and b in order that the weights are randomly initialized.
neural network functions properly. In order to do this a After that we need to create samples for neural network
training process must be conducted. Although there is a training. Let’s say that we want the neural network to
myriad of training approaches, most of them fall in the approximate function 0.75 sin(s) in the range [-10, 10]. So,
following two categories [7]: input vector x can be created by the following command:
• Supervised learning x = np.linspace(-10, 10, 100)
In this case a network is trained by a sequence of pairs of It consists of 100 equally spaced values between -10 and
vectors. The first one is the input vector and the second one 10. The corresponding output vector y can be easily
the target vector. The weights can be modified at each step obtained.
(after each pair) or a matrix of all the vectors can be formed
and then used for training. The training processes are y = 0.75*np.cos(x)
therefore called incremental or batch learning [8]. In the next step we must reshape both x and y into column
• Unsupervised learning form. The len() function returns the number of items in an
object.
In this case there are no target vectors. The weights are
modified based only on the input vectors. size = len(x)
The algorithm used to train the two layer perceptron is the
Then we can modify x and y into new vectors inp (input)
so called backpropagation algorithm (also known as Delta
and trg (target) by the following commands:
rule) [9]. It is composed of both forward and backward
stage. It is described in some detail in [10]. The main goal of inp = x.reshape(size,1)
the algorithm is to make the difference between the actual trg = y.reshape(size,1)
and the target outputs as small as possible.
The reshape() function’s arguments are the numbers of
rows and columns in the new vector. Now, after the creation
IV. PYTHON PROGRAM& RESULTS
of the input and target vectors, we can train the neural
In order to use Python, we must of course install it. On network using them. We will use the following command:
Windows platform it is very popular to install it together
with Anaconda and then make the program in Jupyter error = net.train(inp, trg, epochs=300, show=100,
Notebook [11]. Among many other possibilities a PyCharm goal=0.01)
software package canbe installed to use Python [12]. The train() function we have used is the main part of the
As already noted in the Introduction, Python is a high- presented program. In order to get its detailed description, it
level general-purpose programming language. It is is best to check the documentation [18]. It has several
especially popular among scientific community due to a parameters. The first two are the input and the target vector.
wide set of freely available libraries, in particular scientific Then the number of epochs needs to be stated (the default
ones (linear algebra, visualization tools, plotting, image value is 500). The show parameters determines the steps at
analysis, differential equations solving, symbolic which the error is printed out (the default value is 100). The
computations, statistics etc.). Probably the three most goal parameter determines at which value of the error the
important ones are Numpy, SciPy and Matplotlib [13]. training will stop (the default value is 0.01). It is also
NumPy is a library which adds a support for creation of important to at least know which training algorithm is being
large, multi-dimensional arrays and matrices, together with a used. The default is the “Gradient descent with momentum
large set of operations working on them [14].The SciPy backpropagation and adaptive learning rate” algorithm (in
software library implements a set of functions for processing Neurolab library it is known as neurolab.train.train_gdx().
scientific data, such as statistics, signal processing, image Actually, all the default values can be obtained by the
processing, and function optimization [15]. Matplotlib is, as net.trainf.defaults command. The typical output of the
the name of course suggests, a library used for plotting data program at this stage is given below:
[16]. Beside these common libraries, a Neurolab library,
created specifically for neural networks implementation Epoch: 100; Error: 0.02729823200135833;
[17], will be used. The code starts with the import of the Epoch: 200; Error: 0.018311521643474635;
needed libraries: Epoch: 300; Error: 0.01722912104269513;
The maximum number of train epochs is reached
importnumpy as np As we stated the goal to be 0.01, the learning process
importneurolab as nl stopped at 300 iterations without reaching the target error of
importpylab as pl 0.01. This is however by no means the only possibility. As
the weights of the neural network are randomly initialized
Then a two layer neural network is created by the
following command:
Published By:
Blue Eyes Intelligence Engineering
Retrieval Number: F10750486S419/19©BEIESP 375 & Sciences Publication
DOI: 10.35940/ijitee.F1075.0486S419
Neural Network Programming In Python
Fig 11: The result when only 15 values are used for
training
Published By:
Blue Eyes Intelligence Engineering
Retrieval Number: F10750486S419/19©BEIESP 376 & Sciences Publication
DOI: 10.35940/ijitee.F1075.0486S419
International Journal of Innovative Technology and Exploring Engineering (IJITEE)
ISSN: 2278-3075, Volume-8, Issue-6S4, April 2019
V. CONCLUSION
Neural networks are still a hot topic within artificial
intelligence field. In this paper a simple two layer
perceptron used for function approximation is made in
Python using Neurolab library. All the steps in the program
are thoroughly explained. The performance of the network is
also analyzed and the dependence of the network
performance on the number of training samples and the
number of neurons in the hidden layer are demonstrated.
The whole procedure is valuable for anyone starting the
study of neural networks and wanting them to be
implemented in Python. The further step into more complex
neural networks is facilitated in this way.
REFERENCES
1. L. V. Fausett, “Fundamentals of neural networks:
architectures, algorithms, and applications,” Prentice-Hall,
1994.
2. T. Lindblad, J. M. Kinser, and J. G. Taylor, J. G. “Image
processing using pulse-coupled neural networks,” Springer,
2005.
Fig 12: The result with only 3 neurons in hidden layer 3. J. Howse, “OpenCV computer vision with Python,” Packt
Publishing, 2013.
4. J. L. McClelland, D. E. Rumelhart, and PDP Research Group,
Contrary to intuition, there is also a problem with neural “Parallel distributed processing,” Explorations in the
network having too many neurons. If we discard longer Microstructure of Cognition, 2, 216-271, 1986
training times needed when network has more neurons in 5. https://www.tiobe.com/tiobe-index/
hidden layer, a problem of overfitting might appear as well. 6. R. Van Hattem, “Mastering Python,” Packt Publishing, 2016.
If we use the network with 200 neurons in the hidden layer 7. A. Zilouchian , and M. Jamshidi, “Intelligent control systems
and then analyze with 500 values in the [-10,10] interval, we using soft computing methodologies,” CRC press, 2001.
8. M. T. Hagan, H. B. Demuth, M. H. Beale, and O. De Jesus,
get the result shown in Fig. 13. “Neural network design, 2nd Ed.,” Hagan and Demuth, 2013.
9. I. N. Da Silva, D. H. Spatti, D. H., R. A. Flauzino, R. A., L.
H. BartocciLiboni, and S. F. dos Reis Alves, “Artificial neural
networks: A practical course,” Springer, 2017.
10. A. F. Gad, “Practical Computer Vision Applications Using
Deep Learning with CNNs,” Apress, 2018.
11. J. P. Mueller, “Beginning programming with Python for
dummies,” John Wiley & Sons, 2018.
12. Q. N. Islam, “Mastering PyCharm,” Packt Publishing, 2015.
13. R. Johansson, “Numerical Python: Scientific Computing and
Data Science Applications with Numpy, SciPy and
Matplotlib, 2nd Ed.,” Apress, 2019.
14. I. Idris, “NumPy: Beginner's Guide, 3rd Ed.,” Packt
Publishing Ltd, 2015.
15. J. Nunez-Iglesias, S. van der Walt, and H. Dashnow, “Elegant
SciPy: The Art of Scientific Python," O'Reilly, 2017.
16. D. M. McGreggor, “Mastering matplotlib: A practical guide
that takes you beyond the basics of matplotlib and gives
solutions to plot complex data,” Packt Publishing, 2015
17. https://pythonhosted.org/neurolab/
18. https://pythonhosted.org/neurolab/lib.html#neurolab.train.trai
n_gdx
Fig. 13: The result with only 200 neurons in hidden layer
We can clearly see that many points are far from the
target values, despite the goal of the training being reached.
Published By:
Blue Eyes Intelligence Engineering
Retrieval Number: F10750486S419/19©BEIESP 377 & Sciences Publication
DOI: 10.35940/ijitee.F1075.0486S419