In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article will explain in detail how to use Python code to build a neural network to master some basic concepts, the content of the article is of high quality, so the editor will share it for you to do a reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.
"using Python" here means that you don't need ready-made neural network libraries such as Keras, Tensorflow, etc., otherwise you don't even need 9 lines.
In this article, I will explain how this neural network is formed, so you can also build your own neural network. An extended version of the source code is also available, but also more beautiful.
But first, what is a neural network? In total, the human brain has more than 100 billion neurons, which are connected to each other through synapses. If a neuron is activated by a strong enough input, it will also activate other neurons, a process called "thinking".
We can create neural networks on computers to model this process, and we don't need to simulate molecular biological complexity, just look at it in general. For the sake of simplicity, we simulate only one neuron with three inputs and one output.
We will train this neuron to solve the following problem. The first four samples are called "training sets". Can you work out the pattern? ? Should it be 0 or 1?
It may have been found that the output is always equal to the input of the first column, so? It's supposed to be 1.
Training process
Although the question is very simple, how to teach neurons to answer this question correctly? We want to give each input a weight, which can be positive or negative. The absolute value of the weight represents the decision power of the input to the output. Before we begin, let's set the weight to a random number, and then start the training process:
The input is read from the sample of the training set, adjusted according to the weight, and then substituted into a special equation to calculate the output of the neuron.
The calculation error is the difference between the actual output of the neuron and the expected output of the training sample.
Fine-tune the weight according to the direction of the error.
Repeat 10000 times.
Finally, the weight of the neuron will reach the optimal value of the training set. If we ask neurons to think about a new situation and follow the same process, we should get a good prediction.
An equation for calculating the output of neurons
You may wonder, what is the person's "special equation" for calculating neuronal output? First, we take the weighted sum of neuron inputs:
Why this equation? First of all, we want the adjustment to be proportional to the error, and then multiply it by the input (0-1). If the input is 0, then the weight will not be adjusted. Finally, multiply the gradient of the Sigmoid curve. For ease of understanding, consider:
We use the Sigmoid curve to calculate the neuron output.
If the absolute value of the output is large, this means that the neuron is certain (there are both positive and negative possibilities).
The gradient of Sigmoid curve is smaller where the absolute value is larger.
If the neuron is convinced that the current weight value is correct, then it does not need much adjustment. Multiplied by the gradient of the Sigmoid curve can be achieved.
In fact, there are other equations that allow neurons to learn faster, mainly taking advantage of their relative simplicity.
Build Python code
Although we do not use the neural network library directly, we still need to import four methods from the Python math library Numpy:
Exp: natural pair constant
Array: creating matrix
Dot: Matrix multiplication
Random: random number
For example, we use the array () method to represent the training set:
The .T function is a matrix transpose. I think now I can take a look at the source code of the beautified version, and finally I will come up with my own ultimate thinking. Comments have been added to the source code to explain line by line. Notice that we have processed the entire training set together for each iteration. The following is a complete example of Python:
(Kaiser: it's about writing the code to the local main.py file and running python main.py in the terminal. Just click run here. )
We used Python to build a simple neural network.
First of all, the neural network gives itself random weights, then trains itself with the training set, and finally thinks about the new situation [100] and predicts 0.99993704, which is very close to the correct answer.
Traditional computer programs cannot be learned, and the greatest charm of neural networks lies in their learning ability, which can adapt to the new situation independently, just like the human mind. Of course, only one neuron can perform a very simple task, but what if we connect millions of such neurons? Is it possible to create something self-conscious one day?
On how to use Python code to build a neural network to master some basic concepts to share here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.