Python neural network learning-neural network knowledge guide (1)-what is a neural network?

foreword

After being silent for so long, I originally said to release a Go language framework-free website building tutorial, but I found out that since I haven’t seen the Go language much this year, the engineering model of Go seems to have new changes. In addition, I contacted the tutor, and the tutor asked me to learn The knowledge of neural network, so simply postpone the schedule of building a website in go language, and learn neural network first.

As always, my goal is still to make it so that you can tell it to others after watching it.

Today, let's take a look at the basic concepts of neural networks and have a preliminary understanding of it. (The first few articles may have more theoretical knowledge, and then practice later)

What is a neural network?

This concept will not be explained here, let us understand this mysterious thing step by step.

Neurons? function?

neurons in living things

We have all learned it in high school (since I read this article, I assume that you are an engineering student or a science student, so you should have studied biology in high school), there are neurons in the biological brain, the sketch is as follows (a single nerve cell ):

The left side is to receive signals from other neurons. After processing, if a response is required, neurotransmitters will be released at the end to transmit the signal to the next nerve cell (although the above explanation may not be accurate, but it is probably understandable to the role of this nerve cell).

In simple terms, a neuron has three basic functions:'

1. to receive a stimulus or signal

2. Processing of this information

3. Respond accordingly to the signal

functions in programming languages

In programs, we often use functions (functions) or methods (methods) or others, which are collectively referred to as functions here. For example, a very simple maximum value function:

# 返回num1和num2的最大值
def Max(num1, num2):
    if num1 > num2:
        num2 = num1
    return num2

can be seen

1. This function receives two parameters: num1 and num2

2. Compare the size of num1 and num2

3. The maximum value is returned

Neurons! function!

Now, it is easy to understand, in fact, a neuron can be roughly regarded as a simple function, this neuron (function) receives a signal (parameter), processes the information, and reacts (return value) ! How simple, in the future, when we program, don't talk about writing a functional function, just say, come on, write a neuron that compares the maximum and minimum values ​​hahahahahaha.

Neuron + Neuron = Neural Network

High school biology textbooks say that many neurons form a neural network. The number of neurons in the human body is about tens of billions. Even a simple ant has hundreds of thousands of neurons, and such a large number of neurons are connected one by one. Working together, they have developed a surprising ability: intelligence. And this network is also called a neural network.

So, neuron( function ) + neuron( function ) = neural network ( artificial neural network ).

Although this sentence is not strict and precise, it is very helpful to understand how to use a programming language to write a neural network, that is, we need to use a function set of functions. The output of this function is used as the input of the next or even subsequent functions. The input of a function comes from The output of one or even multiple functions, such as the schematic diagram:

Understand the above four pictures, believe me, you have stepped into the door of neural network programming. congratulations!

In programming, the classification of neural networks

According to my current knowledge, neural networks are roughly divided into two categories: predictors and classifiers.

But in fact, there should be no big difference between these two types of neural networks. For example, in automatic driving, for a photo of a certain road condition, we can say: predict the next move, turn left or right or other, or say: Classify this scene, turn left, turn right or something else, so there is no need to worry too much about this classification.

But there seems to be a difference in some cases, such as inputting Fahrenheit and giving Celsius. Without knowing the clear conversion formula, the neural network here should only be called a predictor, because the input is Fahrenheit. After processing, it gives Possible Celsius results.

If it is called a classifier, then the problem is too big. For example, there are infinite temperature values ​​between 73 degrees Fahrenheit and 74 degrees Fahrenheit, and it is impossible to "classify" them all.

Function: Celsius to Fahrenheit function

# 将摄氏度转为华氏度
def CToF(C):
    return 1.8 * C + 32

The function of the above function is very simple, it is to convert Celsius to Fahrenheit, no matter what value we input in Celsius, we can get the exact value of Fahrenheit.

But is this what we want? no! What we need are neural networks!

Neuron: Celsius -> Fahrenheit

When we see an operation, such as 41*41, we can see that the result is greater than 1600 at a glance, but it is difficult to see the result at a glance, but the computer can directly calculate that the result is 1681.

Similarly, we know that the body temperature is about 36.5 degrees Celsius or 97.7 degrees Celsius, but we cannot accurately know what the current body temperature is, but neurons can " infer " what our current body temperature should be through their own "experience " .

So, here is a very interesting thing: the neural network does not need to be calculated accurately, but approximate calculation is required . When programming neural networks, you don't need to be precise, and in fact, you can't be precise. In real life, no matter what aspect, you can always find some special examples, some people jokingly call it the BUG of Earth OL.

Neural Networks: Temperature Conversion

Suppose we are a bit fuzzy about the formula F = 1.8 * C + 32 now, we only know that we need to add 32, and forget how much we need to multiply in front of C, fortunately, there is an idle neuron in our brain that can be used to process This conversion also has a Fahrenheit thermometer and a Celsius thermometer in the hand.

set initial conditions

We know that the conversion relationship between Celsius and Fahrenheit is: F = constant * C + 32, we need to estimate the size of this constant, so now let this constant be 1, that is, the initial conversion relationship is: F = 1 * C + 32.

We measured the following set of data using a thermometer:

Fahrenheit Celsius
97.7 36.5
77 25
212 100

After setting, our idle neural network is set as shown in the following figure:

try to calculate

Now that everything is ready, we feed the measured 36.5 into the network, and after the network calculates, it outputs 68.5.

Then calculate the error: 97.7 - 68.5 = 29.2. (Remember, the error can be positive or negative and can be 0, but error = real value - calculated value , of course you can also calculate value - real value, as long as you know it yourself, and don't reverse it when calculating.)

29.2 degrees Fahrenheit, this error is still quite large, next, we need to increase the coefficient of C to 1.5 (the coefficient is multiplied by 1.5, I admit that this change is too large, but this is for the sake of understanding, in fact, the change should be smaller ). The neuron becomes the following:

Enter the data of the second degree Celsius (25), calculate again, and the network output is 69.5.

Now the error is: 77 - 69.5 = 7.5. The error becomes smaller instantly, but the error still exists, so we need to continue to adjust, 1.5*1.5 = 2.25, so the neuron becomes as follows:

Now calculate the data for the third degree Celsius (100) and get 257.

Now the error is: 212 - 257 = -45 (Whether you use the real value-calculated value or the calculated value-real value , you must ensure consistency.). The error becomes larger again, and it is now negative, which proves that we have adjusted too violently, and the measurement data has disappeared here, so we have to choose a coefficient of 1.5 with a relatively small error as the final training result of the neuron.

Final Results

In the end we got a conversion formula from Celsius to Fahrenheit: F = 1.5 * C + 32. Although it is quite different from the real formula (F = 1.8 * C + 32), this idea is the "core" of the neural network One of the ideas: Random initial data (for example, F = 1 * C + 32 in this example), use the data to calculate the error (using the three sets of measured data), adjust the data to recalculate (calculate the error, recalculate), again and again Adjust the data and finally get a better result (F = 1.5 * C + 32) as the final network.

The above is my summary, which may be different from the official statement, but as long as everyone understands the general idea, I believe it will be very fast to learn.

Improvements to the neurons above

At the beginning, when our parameter was 1, we got a large error. After adjusting the parameters, the calculation error decreased sharply. However, our parameter adjustment strategy did not change. It was still multiplied by 1.5, which caused the parameter to change too drastically, which caused the error. upheaval.

In the above calculation, the first error is 29.2, and the second error is 7.5. What if we only change a part of the error each time? Like, 1 percent of error? Go ahead and try it out. Still from the beginning!

first neuron

try to train

Input the temperature 36.5 of the first set of data into the network, and the output is: 68.5.

Calculation error: 97.7 - 68.5 = 29.2. At this time, the adjustment parameter 1 is: 1 + 29.2 / 100 = 1.292. Now the new neuron looks like this:

Input the Celsius degree 25 of the second set of data into the network, and the output is: 64.3.

Calculation error: 77 - 64.3 = 12.7. It can be seen that although the error has not changed too drastically, at least the error is slowly decreasing, and it is on the right path.

At this time, the adjustment parameter is: 1.292 + 12.7 / 100 = 1.419. Now the new neuron looks like this:

At this time, input the last set of data 100 degrees Celsius into the network, and the output is 173.9.

Calculation error: 212 - 173.9 = 38.1. The adjustment parameter is: 1.419 + 38.1 / 100 = 1.8. ( Sorry, I calculated an accurate 1.8 by mistake. This is an accidental situation. Not all neural networks can be calculated so accurately, but I hope everyone can understand the idea here. )

At this time, the new neuron is as follows:

At this point, the training data is over.

select final result

Generally speaking, I think that any one of the last two times can be selected as the final result. The exact value of 1.8 was obtained purely by chance this time. In actual application, it should be closer to 1.8 than 1.419, or higher than 1.8, which is reflected on the number axis. The last calculated parameter should be in the yellow area:

Therefore, under unknown circumstances, the results of these two times may be better. For example, the improved version is directly multiplied by 1.5, and the penultimate 1.5 is better. In the improved version, the last time is obviously better ( Relatively speaking).

write to the end

Although the content looks extremely simple today, this is one of the core knowledge of neural networks. After understanding these, it is not difficult to learn neural networks well, but there are several helpful suggestions to remember:

1. Appropriately change the parameters, large error large adjustment, small error small adjustment.

2. The parameters of neurons do not need to be precise, they only need to have an approximate value, even if it seems a bit absurd, it is also a value that can be used.

3. Each training should be based on the adjusted basis of the previous training.

If you are interested, you can try to change the adjustment rate in the improved version to 2% to calculate the final result, give it a try haha

Recently, when school started, I found a part-time job at home, and went to work during the day, so I could only study and record when I took time off. I will update it from time to time, and try to update it every week or two. I hope everyone will understand! Thank you! See you next time!

Guess you like

Origin blog.csdn.net/qq_38431572/article/details/116570390