Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to create a network model using python

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

Today, I will talk to you about how to use python to create a network model. Many people may not know much about it. In order to make you understand better, the editor has summarized the following for you. I hope you can get something from this article.

Neural network (NN), also known as artificial neural network (ANN), is a subset of learning algorithms in the field of machine learning, which generally draws lessons from the concept of biological neural network. At present, neural network is widely used in computer vision, natural language processing and other fields. Andrey Bulezyuk, a senior machine learning expert in Germany, said, "Neural networks are revolutionizing machine learning because they can effectively simulate the complex abstractions of various disciplines and industries without much human involvement."

In general, the artificial neural network consists of the following components:

The input layer (input layer) that receives and transmits data

Hidden layer (hidden layer)

Output layer (output layer)

Weight between layers (weight)

Activation function (activation function) used by each hidden layer

In this tutorial, we use a simple Sigmoid activation function, but note that in the deep neural network model, the sigmoid activation function is generally not the first choice because it is prone to gradient dispersion.

In addition, there are several different types of artificial neural networks, such as feedforward neural networks, convolution neural networks and recurrent neural networks. In this paper, we will take a simple feedforward or perceptual neural network as an example, this type of artificial neural network is to transfer data directly from front to back, referred to as the forward propagation process.

Back propagation algorithm is usually needed to train feedforward neurons, which needs to provide corresponding input and output sets for the network. When the input data is transmitted to the neuron, it is processed accordingly and the resulting output is transmitted to the next layer.

The following figure briefly shows a neural network structure:

In addition, the best way to understand how a neural network works is to learn how to build one from scratch without using any toolkit. In this article, we will demonstrate how to create a simple neural network using Python. problem

The following table shows the problems we will solve:

We will train the neural network to predict the correct output value when providing a new set of data.

As you can see from the table, the output value is always equal to the first value in the input section. Therefore, we can expect the output of the new situation (? ) the value is 1.

Let's see if we can use some Python code to get the same result. Create a neural network class | NeuralNetwork Class

We will create a NeuralNetwork class in Python to train neurons to provide accurate predictions, and this class contains other helper functions. We will not use the neural network library to create this simple neural network example, but we will import the basic Numpy library to assist in the calculation.

The Numpy library is a basic library for processing data, and it has the following four important calculation methods:

EXP-- is used to generate the natural index

Array-- is used to generate matrix

Dot-- is used for matrix multiplication

Random-- is used to generate random numbers

Apply Sigmoid function

We will use the Sigmoid function, which draws an "S" curve as the activation function of the neural network created in this article.

This function maps any value to between 0 and 1 and helps us to normalize the weighted sum of inputs.

After that, we will create a derivative of the Sigmoid function to help calculate the basic adjustment of the weight.

You can use the output of the Sigmoid function to generate its derivative. For example, if the output variable is "x", then its derivative will be x * (1mae x). Training model

The training model means that we will teach the neural network the stage of accurate prediction. Each input has a weights-positive or negative, which means that inputs with large positive or negative weights will have a greater impact on the resulting output.

Note that at the beginning of the model training, the initialization of each weight is a random number.

The following is the training process in the example questions of the neural network constructed in this paper:

1. Get the input from the training data set, make some adjustments according to their weight, and transmit layer by layer by calculating the output of the neural network.

two。 Calculate the error rate of back propagation. In this case, it is the error between the output predicted by the neuron and the expected output of the training data set.

3. According to the error range, some small weight adjustments are made by using the error weighted derivative formula.

4. Repeat this process 15000 times, processing the entire training set at the same time during each iteration

Here, we use the ".T" function to offset the matrix. Therefore, numbers will be stored in this way:

Finally, the weight of neurons will be optimized according to the training data provided. Therefore, if the output of the neural network is consistent with the expected output, it means that the training is completed and accurate prediction can be carried out, which is the way of back propagation. Encapsulation

Finally, after initializing the NeuralNetwork class and running the entire program, here is the complete code for how to create a neural network in the Python project:

Import numpy as npclass NeuralNetwork (): def _ init__ (self): # set the random number seed np.random.seed (1) # convert the weight into a matrix of 3x1, whose value distribution is-1 to 1 And the mean is 0 self.synaptic_weights = 2 * np.random.random ((3,1))-1 def sigmoid (self, x): # apply the sigmoid activation function return 1 / (1 + np.exp (- x)) def sigmoid_derivative (self, x): # to calculate the partial derivative of Sigmoid function return x * (1-x) def train (self, training_inputs, training_outputs) Training_iterations): # training model for iteration in range (training_iterations): # get the output output = self.think (training_inputs) # calculation error error = training_outputs-output # fine tuning weight adjustments = np.dot (training_inputs.T Error * self.sigmoid_derivative (output) self.synaptic_weights + = adjustments def think (self, inputs): # input gets output through the network # converted to floating point data type inputs = inputs.astype (float) output = self.sigmoid (np.dot (inputs) Self.synaptic_weights)) return outputif _ _ name__ = "_ _ main__": # initialize neurological neural_network = NeuralNetwork () print ("Beginning Randomly Generated Weights:") print (neural_network.synaptic_weights) # training data training_inputs = np.array ([[0meme 0rem 1], [1meme 1jue 1] Training_outputs = np.array. T # starts training neural_network.train (training_inputs, training_outputs). 15000) print ("Ending Weights After Training:") print (neural_network.synaptic_weights) user_input_one = str (input ("User Input One:")) user_input_two = str (input ("User Input Two:") user_input_three = str (input ("User Input Three:") print ("Considering New Situation:", user_input_one, user_input_two) User_input_three) print ("New Output data:") print (neural_network.think (np.array ([user_input_one, user_input_two, user_input_three])) print ("Wow, we did it!")

The following is the output from running the code:

The above is a simple neural network that we managed to create. First, the neural network begins to assign some random weights to itself, and then it uses training samples to train itself.

Therefore, if there is a new sample input [1 minute 0], the output value is 0.9999584. The expected correct answer is 1, it can be said that the two are very close, considering that the Sigmoid function is a nonlinear function, this error is acceptable.

After reading the above, do you have any further understanding of how to use python to create a network model? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

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.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report