Last Updated:

Python Neural Network | Python AI

Python Neural Network

The neural network is one of the most complex developments in recent times. Some people think that it is difficult to do it, as it is associated with what only professionals work on. However, the good point is that the path to neural network development is open to everyone.

Here it is only necessary to understand what this type of software tool is and how it is possible to create it, as well as how such a creation works. Just about this and will be told to users who want to write something unusual in Python.

How a Neural Network Works

 

What you first need to understand in order to understand what a neural network is, as well as the algorithm for the operation of a neural network. In fact, if you deal with such a moment and what such a development is, you can further think about what you can create it with.

 

A neural network is a special algorithm that performs such a role to simulate the work of the human brain. Simply put, a neural network allows you to do the same thing as artificial intelligence. But this, on the contrary, can make users think that it is very difficult to do this, because the definitions written here sound complicated. However, you can make a small correction - sometimes complex developments can be done without much effort.

Usually, the created neural network (artificial) works together with the teacher - there is a dataset in the kit, which teaches the development of everything required. By the way, the set can include a variety of data: tags, classes, values, and so on. We can say that this becomes the "brain" for the neural network to obtain its "mental ability".

It is possible to apply artificial intelligence in many areas. For example, take the definition of the tone of the text - this requires a special dataset that calculates the emotionality of phrases. Thus, by analyzing all the sentences in the text, the neural network will eventually be able to determine the shade of tonality. At the output, you will get one or another result - for example, the dataset will contain such values as "neutral tone", "positive coloring" or "negative emotions". Depending on the collected dataset, it is possible to get anything.

Previously, users themselves collected signs to determine a particular result. But now people do not need to worry about this – the neural network has automated such a process. And so, you can just enter your data, and then get the result.

Neural network layers

 

Also, what is important when compiling a neural network is its layers. If users are going to fully explore or create their own neural networks. In fact, with the layers, the situation here is very, very simple. It is enough just to highlight 3 stages of information processing. They are also called neural network layers:

  1. The input layer. This includes information for primary processing. That is, users simply enter data here and do nothing else. There are no calculations, no conclusions of the results, and nothing else;
  2. A hidden compute layer. Here, all the calculations required to obtain the result are carried out directly. That is, all the necessary values are intertwined in order to eventually get this or that answer. For example, the definition of an animal by various signs is made in this way. The camera is connected, then all external signs are scanned. This is what happens in the computational layers of a neural network;
  3. The output layer. Here, the user does not need to do anything. You only need to write down or in some other way display the final result obtained by calculations. And therefore, any fact about the analysis of information, which was required by a person, will be confirmed. In short, in the case of the text, the network will give out an emotional coloring.

This is how a neural network works. And no additional steps are required for the user to organize the work of his creation. In words, everything is simple. And in practice, it is a little more difficult, since you will have to write the code itself. But with the desire and desire to create something unusual - this is possible without any serious problems. A person will be able to carry out the work of a primitive neural network.

Creating a Neural Network from Scratch in Python

 

From scratch, to create a neural network is a matter that can be turned without much effort. However, it should be understood that without special knowledge here can not do. If you just think about how to create a neural network without thinking through special algorithms for the greatest efficiency of the code, then there is a high probability of a simple "failure". And therefore, a person must study the structure of creation carefully.

Everything that was described above about the work of the network and layers are mandatory points, as they will be useful in most cases. So, you will have to use the accumulated knowledge for subsequent developments. They will certainly play a role in the process of writing code in Python.

But you also need to understand that without code samples, it will be almost impossible to write your own neural network. After all, then a person simply will not be able to imagine what a special artificial intelligence algorithm should make work. Just for this, it is necessary to refer to any examples provided either in this material or on the network.

What should be in the neural network? The first and most important thing is the layers. One is for entry and exit - there is no longer required. But for calculations, you may need an indefinite amount of what you need to know when developing a neural network. The second is a set of displacements and weights between the layers. And the last thing that is necessary for the operation of hidden layers is the activation function.

When the user has familiarized himself with all this, it remains only to start working on the neural network. At the bottom, one of the examples of the simplest development will be shown. There is a minimum of everything necessary for the functionality. However, it should be understood that in the future, with the increase in the number of programming skills in Python, it is possible to create larger projects that can cover more data for the analysis of all different objects of the spheres.

Simple neural network

 

In this case, no additional components are required. To put it more clearly, a person does not need to connect third-party libraries for the functionality of his project, as well as some additional resources. Still, it is the simplest neural network. Here you only need to use the proven Python. In it, just the main action will be performed.

Finally, you can move on to what a person expects in such material. And it will be code in Python. It is presented in only 9 lines, which, directly, is a plus. And so, it will be possible to write it off to your editor without any problems, to make your own neural network:

from numpy import exp, array, random, dot
training_set_inputs = array([[0, 0, 1], [1, 1, 1], [1, 0, 1], [0, 1, 1]])
training_set_outputs = array([[0, 1, 1, 0]]). T
random.seed(1)
synaptic_weights = 2 * random.random((3, 1)) — 1
for iteration in xrange(10000):
output = 1 / (1 + exp(-(dot(training_set_inputs, synaptic_weights))))
synaptic_weights += dot(training_set_inputs. T, (training_set_outputs — output) * output * (1 — output))
print 1 / (1 + exp(-(dot(array([1, 0, 0]), synaptic_weights))))

Such a small code fits on only 9 lines in Python! This means that it is possible to spend only a couple of minutes on it, which, in fact, will bring a lot of benefit. And this means that it is possible to proceed to other stages of developing your own creation without much effort and additional stages of prescribing certain commands.

It also uses a very simple scheme to visualize the operation of the system. That is, there is only one exit, and there are several entrances. Thus, the process will be very easy to process. This procedure will not take much time. So, it will be possible to start calculating certain data without any problems, using only the neural network itself.

As for the additional example, it is much larger, but one more point needs to be clarified here. Although it does not take much effort to write such code - only the resulting creation in Python is available, but additional tools are needed. In this case, mathematical libraries. Here it is necessary to prescribe such means as:

  • exp - exponentialization (this is already clear from the abbreviation in English);
  • array — create a matrix;
  • dot — matrix multiplication;
  • random - the generation of random numbers (to put it simply, "randomization" - the creation of anything and is allowed by the program in this case).

And the code itself, which is recommended to be further studied - here it is:

from numpy import exp, array, random, dot

class NeuralNetwork():
def __init__(self):
# Seed the random number generator, so it generates the same numbers
# every time the program runs.
random.seed(1)

# We model a single neuron, with 3 input connections and 1 output connection.
# We assign random weights to a 3 x 1 matrix, with values in the range -1 to 1
# and mean 0.
self.synaptic_weights = 2 * random.random((3, 1)) — 1

# The Sigmoid function, which describes an S shaped curve.
# We pass the weighted sum of the inputs through this function to
# normalise them between 0 and 1.
def __sigmoid(self, x):
return 1 / (1 + exp(-x))

# The derivative of the Sigmoid function.
# This is the gradient of the Sigmoid curve.
# It indicates how confident we are about the existing weight.
def __sigmoid_derivative(self, x):
return x * (1 — x)

# We train the neural network through a process of trial and error.
# Adjusting the synaptic weights each time.
def train(self, training_set_inputs, training_set_outputs, number_of_training_iterations):
for iteration in xrange(number_of_training_iterations):
# Pass the training set through our neural network (a single neuron).
output = self.think(training_set_inputs)

# Calculate the error (The difference between the desired output
# and the predicted output).
error = training_set_outputs — output

# Multiply the error by the input and again by the gradient of the Sigmoid curve.
# This means less confident weights are adjusted more.
# This means inputs, which are zero, do not cause changes to the weights.
adjustment = dot(training_set_inputs. T, error * self.__sigmoid_derivative(output))

# Adjust the weights.
self.synaptic_weights += adjustment

# The neural network thinks.
def think(self, inputs):
# Pass inputs through our neural network (our single neuron).
return self.__sigmoid(dot(inputs, self.synaptic_weights))

if __name__ == «__main__»:

#Intialise a single neuron neural network.
neural_network = NeuralNetwork()

print «Random starting synaptic weights: »
print neural_network.synaptic_weights

# The training set. We have 4 examples, each consisting of 3 input values
# and 1 output value.
training_set_inputs = array([[0, 0, 1], [1, 1, 1], [1, 0, 1], [0, 1, 1]])
training_set_outputs = array([[0, 1, 1, 0]]). T

# Train the neural network using a training set.
# Do it 10,000 times and make small adjustments each time.
neural_network.train(training_set_inputs, training_set_outputs, 10000)

print «New synaptic weights after training: »
print neural_network.synaptic_weights

# Test the neural network with a new situation.
print «Considering new situation [1, 0, 0] -> ?: »
print neural_network.think(array([1, 0, 0]))

And this is what the user will get after some time of work. The main thing in this is to understand what is what. Otherwise, there may be difficulties or even worse – errors in the system itself.

Neural Network Training

The process of training a neuron in a specially designed network is a long and time-consuming process. Therefore, you will have to be patient. But with due ingenuity, a person can facilitate his task (but more on this later). In general, users will have to have this algorithm in order to train their small neural network created using the previous paragraph:

  1. Take the input data from the example of the training set, adjust them by weights and transmit them according to a special formula for calculating the output of the neuron.
  2. Calculate the error, which is the difference between the output of the neuron and the desired output in the example training set.
  3. Depending on the direction of the error, adjust the weight slightly.
  4. And now - the hardest and that will take a lot of time. It is necessary to repeat this procedure about 10,000 times.

And all this in order to train your own system for the right answer. The process is very laborious, but if you set up a special automation of such, then without problems it will be possible to train the neuron. And in this case, a person will be able to quickly make the desired project.

Neural Network Training with Keras

For a neural network, it is also worth downloading a special component that can easily carry out correct work with the project. And this software is called Keras. Users can put this into practice in different cases. For example, here's what Keras might be suitable for when working on a neural network:

  • Ability to select the activation function;
  • Select the number of processing layers to determine the efficiency of data calculation and analysis during the direct operation of the neural network. Also, touching the theme of the layers - they can also configure the type, so that detailed adjustment will be possible;
  • Also, it is possible to add a certain number of neurons. Just like that, they need to be trained to achieve the right result.

And for the training itself, you need to enter the following code:

model = hard. Sequential()
model.add(keras.layers.Dense(input_shape=(SIZE_OF_ONE_IMAGE,), units=128, activation='relu'))
model.add(keras.layers.Dense(10, activation='softmax'))

model.summary()

model.compile(optimizer=’sgd’,
loss=’categorical_crossentropy’,
metrics=[‘accuracy’])

After output, you need to use the following code for the second step of the workout:

model.fit(X_train, y_train, epochs=20, batch_size=128)

And the last, final stage is again the use of code, but a different one, in order to verify the accuracy of the training data:

model.evaluate(X_test, y_test)

After this process, the training will be completed.

Running from a Python interpreter

To run from an interpreter, it is recommended that you first make sure that the Python version is higher than 3.6. And then install special modules using pip. This is done in the following way:

!pip install matplotlib
!pip install sklearn
!pipinstalltensorflow

Importing modules and libraries is as follows:

import numpy as np
import matplotlib.pyplot as plt
import gzip
from typing import List
from sklearn.preprocessing import OneHotEncoder
import tensorflow.keras as keras
from sklearn.model_selection import train_test_split
from sklearn.metrics import confusion_matrix
import itertools
%matplotlib inline

After that, you're ready to run all your project's code in Python. However, it is recommended that you do this in a virtual environment. This will be the best way to work with a neural network created by a person in a popular programming language.