In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces the relevant knowledge of how to use Tensorflow to train BP neural network to achieve iris classification in python, the content is detailed and easy to understand, the operation is simple and fast, and has a certain reference value. I believe you will gain something after reading this python article on how to use Tensorflow to train BP neural network to achieve iris classification. Let's take a look.
Using softwar
Python 3.8,Tensorflow2.0
Problem description
Iris is mainly divided into dog grass Iris (0), variegated Iris (1) and Virginia Iris (2).
It has been found that irises can be classified by calculating calyx length, calyx width, petal length and petal width.
Therefore, as long as enough data of calyx, petals and corresponding species are given, and appropriate neural network training is used, the classification of Iris can be realized.
Build a neural network
The input data is the length of calyx, the width of calyx, the length of petals and the width of petals, which is a matrix of n rows and four columns.
And the output is the probability of each category, which is a matrix of n rows and three columns.
We use BP neural network, X as the input data, Y as the output data, W as the weight, B offset. Yes
Yellowx ∗ Whiteb
Because x is a matrix with n rows and four columns, and y is a matrix with n rows and three columns, w must be a matrix with four rows and three columns, and each neuron corresponds to a b, so b is a matrix with one row and three columns.
The neural network is shown below.
Therefore, as long as we find the right w and b, we can accurately judge the species of iris.
Let's start to train these two parameters.
Training parameters
Loss function
The loss function expresses the difference between the predicted value (y *) and the real value (y). We use the mean square error formula as the loss function.
The smaller the value of the loss function, the closer the predicted value is to the real value, and the more appropriate w and b will be.
If people try it in groups, it certainly won't work. So we use the gradient descent algorithm to find the minimum value of the loss function.
Gradient: a vector that takes a partial derivative of a function. The direction in which the gradient decreases is the direction in which the function decreases.
Where an is the learning rate, that is, the step of gradient descent, if an is too large, it is possible to miss the optimal value, and if an is too small, it will take more steps to find the optimal value. Therefore, it is very important to choose the right learning rate.
Parameter optimization
Parameters are optimized by back propagation.
Back propagation: from back to front, the partial derivative of the loss function to the neuron parameters of each layer is calculated layer by layer, and all parameters are updated iteratively.
such as
You can see that w gradually tends to the minimum value of 0 of loss.
These are all the key points of our training.
Code
Data set
We use the iris data set provided by the sklearn package. A total of 150 sets of data.
Disrupt to ensure the randomness of the data, take the first 120 as the training set and the last 30 as the test set.
# Import data, respectively, input feature and label x_data = datasets.load_iris (). Data # # stores calyx and petal feature data y_data = datasets.load_iris (). Target # stores corresponding categories # randomly disturbs the data (because the original data is sequential, not disrupting the order will affect the accuracy) # seed: random number seed, is an integer, when set The random numbers generated each time are the same (to facilitate teaching, to ensure that the results of each student are consistent) np.random.seed (116C) # uses the same seed to ensure that the input features and tags correspond to np.random.shuffle (x_data) np.random.seed (116) np.random.shuffle (y_data) tf.random.set_seed (116) # the scrambled data set is divided into a training set and a test set, and the training set is the first 120 rows The test set is the last 30 lines x_train = x_data [:-30] y_train = y_data [:-30] x_test = x_data [- 30:] y_test = y_data [- 30:] # convert the data type of x Otherwise, when multiplying matrices, the x_train = tf.cast (x_train, tf.float32) x_test = tf.cast (x_test, tf.float32) # from_tensor_slices function will correspond to the input feature and tag value one by one due to inconsistent data types. (divide the data set into batches, each batch batch group data) train_db = tf.data.Dataset.from_tensor_slices ((x_train, y_train)) .batch (32) test_db = tf.data.Dataset.from_tensor_slices ((x_test, y_test)) .batch (32) Parameter # generates the parameters of the neural network, 4 input features, so the input layer consists of 4 input nodes Because category 3 Therefore, the output layer consists of three neurons # can be trained with tf.Variable () labeling parameter W1 = tf.Variable (tf.random.truncated_normal ([4,3], stddev=0.1)) # four rows and three columns, and the variance is 0.1b1 = tf.Variable (tf.random.truncated_normal ([3], stddev=0.1)) # one row and three columns The variance is 0.1 training a = 0.1 # the learning rate is 0.1epoch = 500 # cycle 500 # training part for epoch in range (epoch): # cycle at dataset level Once per epoch loop dataset for step, (x_train, y_train) in enumerate (train_db): # batch level loop One batch with tf.GradientTape () as tape: # with structure for each step cycle records gradient information y = tf.matmul (x_train, W1) + b1 # neural network multiplication and addition y = tf.nn.softmax (y) # so that the output y conforms to the probability distribution y _ = tf.one_hot (y_train, depth=3) # converts the label value to a single hot code format It is convenient to calculate loss loss = tf.reduce_mean (tf.square (yy)) # using the mean square error loss function mse = mean (sum (ymury*) ^ 2) # to calculate the gradient grads = tape.gradient (loss, [W1) of loss to w, b B1]) # implement gradient update W1 = W1-lr * w1_grad b = b-lr * b_grad w1.assign_sub (a * grads [0]) # Parameter W1 self-update b1.assign_sub (a * grads [1]) # Parameter b self-update test # Test part total_correct, total_number = 0, 0for x_test Y_test in test_db: # forward propagation probability y = tf.matmul (x_test, W1) + b1 y = tf.nn.softmax (y) predict = tf.argmax (y, axis=1) # returns the index of the largest value in y That is, the predicted classification # converts predict to y_test data type predict = tf.cast (predict, dtype=y_test.dtype) # if the classification is correct, then correct=1, otherwise it is 0 Convert the result of Bool type to int type correct = tf.cast (tf.equal (predict, y_test), dtype=tf.int32) # add up the number of correct in each batch correct = tf.reduce_sum (correct) # add up the number of correct in all batch total_correct + = int (correct) # total_number is the total number of samples of the test, that is, the number of rows of x_test Shape [0] returns the number of rows of variables total_number + = x_test.shape [0] # the total accuracy is equal to total_correct/total_numberacc = total_correct/total_ numberprint ("test accuracy =% .2f%"% (acc * 100.0)) my_test = np.array ([[5.9,3.0,5.1) ]) print ("enter 5.9 3.0 5.1 1.8") my_test = tf.convert_to_tensor (my_test) my_test = tf.cast (my_test, tf.float32) y = tf.matmul (my_test, W1) + b1y = tf.nn.softmax (y) species = {0: "Dog Iris", 1: "variegated Iris", 2: "Virginia Iris"} predict = np.array (tf.argmax (y) Axis=1)) [0] # returns the index of the maximum value in y That is, the predicted classification print ("the iris is:" + species.get (predict)) on "how to use Tensorflow to train BP neural network to achieve iris classification in python" is introduced here. Thank you for reading! I believe that everyone has a certain understanding of the knowledge of "how to use Tensorflow to train BP neural network to realize iris classification in python". If you want to learn more knowledge, welcome to follow the industry information channel.
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.