Neural networks in Python: how everything works

Pinterest LinkedIn Tumblr
  1. How machine learning works
  2. Neural network as a sub-sector of machine learning
  3. Reasons for choosing Python for programming neural networks 

A neural network in Python is written for a number of good reasons: the brevity of the programming language, a low entry threshold (you don’t need to study the nuances of the code for years), a huge number of plug-in libraries, and much more.

Nobody forbids writing in other languages, but it will be longer, more difficult, much more knowledge will be required, which, at times, is inappropriate. And yet, even using Python, you need to have at least a basic understanding of how neural networks work. From our material, you will learn how it works, why Python and where you can learn this language.

How machine learning works

Machine learning is a class of artificial intelligence methods, the purpose of which is not to solve a problem directly, but to teach systems to independently solve these problems. Methods of statistics are used to construct them.

Performance in a given task is not improved by programming, but by data. For example, how successfully the mail service is able to weed out spam or how speech recognition systems have advanced with the advent of voice assistants Alice, Siri, Alexa and others.

Machine learning solves the following tasks:

  • Fraud Detection – Tracking abnormal patterns in bank card or account transactions.
  • Forecasting – Predicting future quotes for stocks, currencies, cryptocurrencies, and other securities.
  • Image recognition – identifying objects and faces (if necessary, identifying them) in pictures and videos.

One type of machine learning is supervised learning. Its essence lies in the fact that the system is “trained”. In the first step, she is presented with many examples of a specific problem and the desired conclusion, thus she is taught to understand past data. In the second stage, when the system is trained, it is presented with new input data in order to predict the conclusions on its own.

Let’s consider an example of creating a spam detector. You can create it based on intuition and manually choosing the rules for its work. Such a system can function. However, it is very difficult to find the right templates and create a successful detector based only on intuition.

This machine learning method is used to predict the output. 

Regression problem – Continuous output must be predicted. For example, based on the size of a residential building, predict its value.

Classification problem – a discrete number of quality labels should be predicted. For example, based on the number of words in an email, predict whether it is spam or not.

Neural network as a sub-sector of machine learning

Today the IT industry is developing at an unprecedented pace, along with the growing popularity of machine learning. It has a huge impact on many processes in the world, so more and more people are interested in its directions. One of them is neural networks. They are built on the principle of the organization and functioning of our brain.

The human brain is made up of over one hundred billion neuron cells. The place of contact of two neurons is the synapse. It serves to transmit nerve impulses from one cell to another. When a sufficient number of impulses come to the neuron through it, then the neuron will transmit them further. This is how our thinking is built.

“The model of this phenomenon is a neural network created using a computer. In this case, there is no need to build a molecular level and reflect biological processes. The model recreates work flowing at a higher level.”

This is possible thanks to the use of mathematical matrices. They are tables consisting of rows and columns, at the intersection of which are numbers – matrix elements. For simplicity, let’s create a model of one neuron with three sources of information and one output. The goal is to train the neural network to solve the problem presented in the image below. The training set will be examples one through four.

Example 10 0 10
Example 21 1 1one
Example 30 1 1one
Example 40 1 10
New situationone hundred?

Did you manage to see the pattern and understand which number is hidden under the question mark – 1 and 0?

If you were careful, you will notice that the output in all examples matches the value of the left column. So the answer is 1.

Each neural network created with a computer has several layers. Their number and the number of neurons in them can be different. The first layer is always input, the last one is output, all intermediate layers are hidden layers.

Neural network as a sub-sector of machine learning

The first layer contains neurons, whose task is to receive signals. However, they do not process them.

The arrows that transmit signals are synapses. Their function is to multiply the input signal xi by the synaptic weight wi. The sum of the values ​​of the incoming signals in each of the neurons is determined by the formula:

d = w0 + w1 x1 +… + wn xn

where w0 is the displacement parameter.

Using the activation function y = S (d) (non-linear sigmoidal function), the result d is reduced to the range [0 … 1]

Neural network as a sub-industry of machine learning [/ caption]

alpha is the slope parameter of the function S (d). The larger the value of this parameter, the larger the angle of the tangent at the inflection point of the function. Accordingly, the function will be cooler.

In order to train a neural network, we must tell it what we want from it. For example, with an input signal of 0 or 1, we expect an output equal to 1 from the network.

Reasons for choosing Python for programming neural networks

The most common programming language for artificial neural networks is Python. Why is it exactly Python neural networks that have gained such popularity, despite the presence of many other programming languages? IT professionals often agree on this question and point to several reasons.

Reasons for choosing Python for programming neural networks

Research projects. A large number of ready-made examples of neural networks in Python are research projects. At the same time, the scientists working on them are most often not programmers. This language has a low entry threshold: no one writes neural networks in Python from scratch, as it takes a long time. There are libraries for Python neural networks that have already been written by experts. So a whole community of neural networks has developed around Python. If you are engaged in research activities, then follow the example of others in this matter.

You can give a similar example with another programming language. Fortran is one of the first high-level languages ​​in which many programs and subroutine libraries were written. But today it is not used by programmers, but it is widespread among physicists.

Conciseness and interoperability . The language allows complex algorithms to be developed in a short time. It is distinguished by simplicity, brevity and expressiveness. In addition, it has a powerful interoperability mechanism with C \ C ++, which allows for fast computations. It is possible to create simple and complex neural networks in Python.

Flexibility. Neural networks are mostly small programs, but there is a need to frequently change them, choosing the best architecture, data preprocessing, and other parameters. Therefore, there are practically no difficulties with legacy code, but there is a need for rapid development. Creating and building neural networks in Python is an option that meets these requirements better than using C ++ or Java. Machine learning frameworks are a Python or Torch frontend and a C ++ backend, most often.