Lecture 1: Neural network calculation: Two-layer neural network is used to distinguish iris types in tensorflow

  . 1  # _ * _ Coding: _ * _. 8 UTF- 
  2  # using the iris data set, propagation, counter propagation before achieved, visual loss curve 
  . 3  
  . 4  # Import Module desired 
  . 5  Import tensorflow AS TF
   . 6  from sklearn Import Datasets
   . 7  from matplotlib import pyplot aS PLT
   . 8  import numpy aS NP
   . 9  
10  # import data, respectively, and the input feature tag 
. 11 x_data = datasets.load_iris (). data
 12 is y_data = datasets.load_iris (). target
 13 is  
14  # random disrupted Data (because the original data is sequential, the order will not affect the accuracy rate)
15  # seed: The random number seed is an integer. After setting, the random number generated every time is the same. 
16 np.random.seed (1 16) # use the same seed, and ensure that the input feature correspondence label. 
. 17  np.random.shuffle (x_data)
 18 is np.random.seed (1 16 )
 . 19  np.random.shuffle (y_data)
 20 is tf.random.set_seed (1 16 )
 21 is  
22 is  # after the disruption to data set is divided into a training set And the test set, the training set is the first 120 lines, the test set is the last 30 lines 
23 x_train = x_data [:-30 ]
 24 y_train = y_data [:-30 ]
 25 x_test = x_data [-30 :]
 26 y_test = y_data [- 30 :]
 27  
28  
29  #Convert the data type of x, otherwise an error will be reported due to inconsistent data types when the matrix is ​​multiplied later 
30 x_train = tf.cast (x_train, tf.float32)
 31 x_test = tf.cast (x_test, tf.float32)
 32  
33  # from_tensor_slices The input features correspond to the label values ​​one-to-one. (The data set is divided into batches, and each batch of batch data) 
34 train_db = tf.data.Dataset.from_tensor_slices ((x_train, y_train)). Batch (32 )
 35 test_db = tf.data.Dataset.from_tensor_slices (( x_test, android.permission.FACTOR.)) BATCH (32. )
 36  
37 [  # generated neural network parameters, four input feature, so input layer four input nodes; for 3 classification, so the output layer of three neurons 
38  # with tf. Variable () parameter labeled training 
39  # use the same random number seed so that each generation. 
40w1 = tf.Variable (tf.random.truncated_normal ([4, 3], stddev = 0.1, seed = 1 ))
 41 b1 = tf.Variable (tf.random.truncated_normal ([3], stddev = 0.1, seed = . 1 ))
 42 is  
43 is LR = 0.1 # learning rate of 0.1 
44 is train_loss_results = [] # the loss recorded in each round in this list, to provide data for the subsequent loss curve Videos 
45 test_acc = [] # the acc recorded in each round this list, to provide data for subsequent Videos acc curve 
46 is Epoch 500 = # cycles 500 
47 loss_all = 0 # round divided into four step, loss_all generating step recording four and four loss 
48  
49  # training part 
50  for epoch in range (epoch):# Dataset level of cycles, each cycle time epoch data set 
51 is      for step, (x_train, y_train) in the enumerate (train_db): # BATCH level cycles, each cycle a step BATCH 
52 is          with tf.GradientTape () Tape AS : # with a gradient structure of the recording information 
53 is              y = tf.matmul (x_train, W1) + B1 # neural network sum operation 
54 is              y = tf.nn.softmax (y) # output y meet the probability distribution (and only after this operation hot code of the same order, relative loss seeking Save) 
55              Y_ = tf.one_hot (y_train, depth =. 3) # label value into a hot code format to facilitate the calculation and loss Accuracy 
56 is              loss = tf.reduce_mean (TF. square (Y_ - Y)) # using the mean square error = Mean loss function MSE (SUM (Y-OUT) ^ 2) 
57 is             + = loss.numpy loss_all () # each step the calculated cumulative loss, seek to provide data for the subsequent average loss, loss thus calculated more accurately 
58          # calculates the parameters of his loss gradient of 
59          GrADS = tape.gradient (Loss, [w1, B1])
 60  
61 is          # implemented gradient update w1 = w1 - LR * w1_grad, B = B - LR * b_grad 
62 is          w1.assign_sub (LR * GrADS [0]) # parameters w1 self-refresh 
63 is          B1. assign_sub (LR * GrADS [. 1]) # parameter update from b 
64      
65      # of each epoch, the print information loss 
66      Print ( " Epoch: {}, loss: {} " .format (epoch, loss_all /. 4 ))
 67     train_loss_results.append (loss_all /. 4) # The four loss averaging step of recording in this variable 
68      loss_all = 0   # loss_all zero, ready for the next epoch of a record loss 
69  
70      # test section 
71 is      # total_correct of prediction of the number of samples, the total number of samples TOTAL_NUMBER tested, the two variables are initialized to 0 
72      total_correct, TOTAL_NUMBER = 0, 0
 73 is      for x_test, android.permission.FACTOR. in TEST_DB:
 74          # using the updated prediction parameters 
75          y = tf.matmul (x_test, w1) + b1
 76          y = tf.nn.softmax (y)
 77          pred = tf.argmax (y, axis = 1) #Return the index of the maximum value in y, that is, the predicted classification 
78          # Convert pred to the data type of y_test 
79          pred = tf.cast (pred, dtype = y_test.dtype)
 80          # If the classification is correct, correct = 1, otherwise is 0, the result is converted to bool int type value 
81          correct = tf.cast (tf.equal (Pred, android.permission.FACTOR.), DTYPE = tf.int32)
 82          # the number of the batch for each correct add 
83          correct = TF. reduce_sum (correct)
 84          # the number of correct batch together all 
85          total_correct + = int (correct)
 86          # TOTAL_NUM the total number of samples tested, i.e. the number of rows x_test, shape [0] of the variable number of rows returned 
87          total_number + = x_test.shape [0]
 88     # Overall accuracy rate is equal total_correct / TOTAL_NUMBER 
89      ACC = total_correct / TOTAL_NUMBER
 90      test_acc.append (ACC)
 91 is      Print ( " Test_acc: " , ACC)
 92      Print ( " -------------- --------------------------- " )
 93  
94  
95  # draw loss curve 
96 plt.title ( " Loss Function the curve " ) # image title 
97 plt.xlabel ( " Epoch " ) # x-axis variable name 
98plt.ylabel ( " Loss " ) # Y axis variable names 
99 plt.plot (train_loss_results, label = " $ $ Loss " ) # pointwise values and plotted train_loss_results connection, connection icons are Loss 
100 plt.legend () # draw curve icon 
101  plt.show ()
 102  
103  
104  # drawing Accuracy curves 
105 plt.title ( " Acc the curve " ) # image title 
106 plt.xlabel ( " Epoch " ) # X axis variable name 
107 plt.ylabel ( " Acc" ) # Y axis variable name 
108 plt.plot (test_acc, label = " $ $ the Accuracy " ) # point by point and drawn test_acc connection, the connection icon is the Accuracy 
109  plt.legend ()
 110 plt.show ()

 

Guess you like

Origin www.cnblogs.com/wbloger/p/12694149.html