Lecture 2: Neural Network Optimization--SGD

#Use iris data to realize forward propagation and back propagation and visualize the loss curve

# Import module required 
Import tensorflow TF AS
 from sklearn Import Datasets
 Import matplotlib.pyplot AS PLT
 Import numpy AS NP
 Import Time 


# import data, respectively, and the input feature tag 
x_data = datasets.load_iris (). Data 
y_data = datasets.load_iris ( ) .target 


# random disrupted data (because the original data is sequential order is not disrupted affect accuracy) 
# the sEED: random number seed is an integer, when set, each generated random number are the same 
np. random.seed (1 16) # use the same seed, for example, warranty labels and features correspond 
np.random.shuffle (x_data) 
np.random.seed ( 1 16 ) 
np.random.shuffle (y_data) 
tf.random.set_seed (116 ) 


# after the upset style training data set and test sets, the first 120 lines of the training set, a test set of 30 rows 
x_train x_data = [: - 30 ] 
y_train = y_data [: - 30 ] 
x_test = x_data [-30 :] 
android.permission.FACTOR. = y_data [-30 :] 

# conversion x data types, or because the data is inconsistent matrix being given by multiplying the back 
x_train = tf.cast (x_train, tf.float32) 
x_test = tf.cast ( x_test, tf.float32) 

# from_tensor_slices function makes the input features correspond to the label values ​​one by one. (The data set is divided into batches, and each batch batch of 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 ) 


#Generating a neural network parameters, characteristic number 4 input, input layer four input nodes, classified as 3, so the output neuron becomes 3 
# with tf.Variable () training parameters can be labeled 
# used to make each seed generated The same random number 
w1 = tf.Variable (tf.random.truncated_normal ([4, 3], stddev = 0.1, seed = 1 )) 
b1 = tf.Variable (tf.random.truncated_normal ([3], stddev = 0.1, =. 1 SEED )) 

LR = 0.1 # learning rate 0.1 
train_loss_results = []   # the loss recorded in each round in this list, to provide data for subsequent Videos curve 
test_acc = [] # the round acc recorded in this list to provide data for subsequent Videos acc curve 
Epoch 500 = # cycles 500 
loss_all = 0 # round divided into four step, loss_all recording step 4 produced 4 loss and 


# training portion 

now_time = the time.time ()
for epoch in the Range (epoch): # dataset level of circulation, each epoch cycle time data collection 
  for step, (x_train, y_train) in the enumerate (train_db): # BATCH level cycles, each cycle a step BATCH 
    with TF .GradientTape () Tape AS: # with a gradient structure of the recording information 
      y = tf.matmul (x_train, W1) + B1 # neural network sum operation 
      y = tf.nn.softmax (y)   # output y meet the probability distribution ( after this, the hot code of the same order, relative seeking Save Loss) 
      Y_ = tf.one_hot (y_train, depth =. 3) # label value into hot code format to facilitate the calculation and Accuracy 
      Loss = tf.reduce_mean ( tf.square (Y_ - Y))   # using the mean square error function Mean = MSE (SUM (Y - OUT) ^ 2) 
      loss_all + = Loss #Accumulate the loss calculated in each step to provide data for the subsequent average loss calculation, so that the calculated loss is more accurate 
    # Calculate the gradient of each parameter of the gradient 
    grads = tape.gradient (loss, [w1, b1]) 

    # Implement the gradient update w1 = w1 - LR w1_grad B = B * - * LR b_grad 
    w1.assign_sub (LR * GrADS [0]) # parameters w1 self-refresh 
    b1.assign_sub (LR * GrADS [. 1]) # parameter b1 self-refresh 

  # each epoch, print loss information 
  print ( " Epoch {}, loss: {} " .format (epoch, loss_all / 4 )) 
  train_loss_results.append (loss_all / 4) # average the loss of 4 steps and record in this variable 
  loss_all = 0 # loss_all returns to zero, preparing for recording the loss of the next epoch 

  # Test part 
  #total_correct for predicting the number of samples of, TOTAL_NUMBER the total number of samples tested, the two variables are initialized to 0 
  total_correct, TOTAL_NUMBER = 0, 0
   for x_test, android.permission.FACTOR. in TEST_DB:
     # using the updated prediction parameters 
    y = tf.matmul (x_test, W1) + B1 
    y = tf.nn.softmax (y) 
    pred = tf.argmax (y, = Axis. 1) # returns the index y is the maximum value, i.e. the predicted classification 
    # to convert pred y_test data type 
    Pred = tf.cast (Pred, DTYPE = y_test.dtype)
     # If correctly classified, the correct = 1, otherwise to 0, the result of the conversion type bool to int 
    correct = tf.cast (tf.equal (Pred, android.permission.FACTOR.), DTYPE = tf.int32)
     # the number of the combined batch for each correct 
    correct =tf.reduce_sum (correct)
     # the number of all the combined batch 
    total_correct + = int (correct)
     # Total-Number of total number of samples tested, i.e. the number of rows x_test, shape [0] return variable number of rows 
    total_number + = x_test.shape [0]
   # overall accuracy rate is equal total_correct / TOTAL_NUMBER 
  ACC = total_correct / TOTAL_NUMBER 
  test_acc.append (ACC) 
  Print ( " Test_acc: " , ACC)
   Print ( " ----------- -------------------------- " ) 
total_time = time.time () -now_time
 print ( " total_time " , total_time)


#绘制loss曲线
plt.title("Loss Function Curve")
plt.xlabel("Epoch")
plt.ylabel("Loss")
plt.plot(train_loss_results, "b-.", label="$Loss$")
plt.legend()
plt.show()


#绘制Accuracy曲线
plt.title("Acc Curve")
plt.xlabel("Epoch")
plt.ylabel("Acc")
plt.plot(test_acc, "b-.", label="$Accuracy$")
plt.legend()
plt.show()

 

Guess you like

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