当前位置:网站首页>Classification of iris based on tensorflow neural network
Classification of iris based on tensorflow neural network
2022-07-22 17:08:00 【Zhichao_ ninety-seven】
b Station courses (【 Peking University, 】Tensorflow2.0_ Bili, Bili _bilibili)P8 Code for
# -*- coding: UTF-8 -*-
# Using iris data set , Achieve forward propagation 、 Back propagation , visualization loss curve
# Import the required modules
import tensorflow as tf
from sklearn import datasets
from matplotlib import pyplot as plt
import numpy as np
# Import data , Input features and labels, respectively
x_data = datasets.load_iris().data
y_data = datasets.load_iris().target
# Randomly scramble data ( Because the raw data is sequential , If the order is not disordered, the accuracy will be affected )
# seed: Random number seed , It's an integer , After setting , The random number generated each time is the same ( For the convenience of teaching , To ensure that each student's results are consistent )
np.random.seed(116) # Use the same seed, Ensure that the input features and labels correspond one-to-one
np.random.shuffle(x_data)
np.random.seed(116)
np.random.shuffle(y_data)
tf.random.set_seed(116)
# The disrupted data set is divided into training set and test set , Training set first 120 That's ok , After test set 30 That's ok
x_train = x_data[:-30]
y_train = y_data[:-30]
x_test = x_data[-30:]
y_test = y_data[-30:]
# transformation x Data type of , Otherwise, errors will be reported due to inconsistent data types when multiplying the following matrices
x_train = tf.cast(x_train, tf.float32) # tf.cast Used for tensor data type conversion
x_test = tf.cast(x_test, tf.float32)
# from_tensor_slices Function to make the input feature correspond to the label value one by one .( Batch data sets , 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)
# Generate the parameters of the neural network ,4 There are two input features, so , The input layer is 4 Input nodes ; because 3 classification , Therefore, the output layer is 3 Neurons
# use tf.Variable() Marker parameters can be trained
# Use seed Make the random number generated each time the same ( It's convenient for teaching , Make everyone agree , Don't write... In real use seed)
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, seed=1))
lr = 0.1 # The learning rate is 0.1
train_loss_results = [] # Put each round of loss Record in this list , Draw for the follow-up loss Curves provide data
test_acc = [] # Put each round of acc Record in this list , Draw for the follow-up acc Curves provide data
epoch = 500 # loop 500 round
loss_all = 0 # Minutes per round 4 individual step,loss_all Record four step Generated 4 individual loss And
# Training part
for epoch in range(epoch): # Data set level loops , Every epoch Loop once data set
for step, (x_train, y_train) in enumerate(train_db): #batch Level loop , Every step Circle one batch
with tf.GradientTape() as tape: # with Structure records gradient information
y = tf.matmul(x_train, w1) + b1 # Neural network multiplication and addition operation
y = tf.nn.softmax(y) # Make the output y According to the probability distribution ( After this operation, it is the same order of magnitude as the single heat code , Subtract to find loss)
y_ = tf.one_hot(y_train, depth=3) # Convert the tag value to a unique hot code format , Easy to calculate loss and accuracy
loss = tf.reduce_mean(tf.square(y_ - y)) # The mean square error loss function mse = mean(sum(y-out)^2)
loss_all += loss.numpy() # Each one step Calculated loss Add up , For follow-up loss The average provides data , It's calculated in this way loss More accurate
# Calculation loss The gradient of each parameter
grads = tape.gradient(loss, [w1, b1])
# Achieve gradient update w1 = w1 - lr * w1_grad b = b - lr * b_grad
w1.assign_sub(lr * grads[0]) # Parameters w1 Self updating
b1.assign_sub(lr * grads[1]) # Parameters b Self updating
# Every epoch, Print loss Information
print("Epoch {}, loss: {}".format(epoch, loss_all/4))
train_loss_results.append(loss_all / 4) # take 4 individual step Of loss Average and record in this variable
loss_all = 0 # loss_all Zeroing , To record the next epoch Of loss To prepare for
# Test part
# total_correct The number of samples for the prediction pair , total_number Is the total number of samples tested , Initialize both variables to 0
total_correct, total_number = 0, 0
for x_test, y_test in test_db:
# Use the updated parameters for prediction
y = tf.matmul(x_test, w1) + b1
y = tf.nn.softmax(y)
pred = tf.argmax(y, axis=1) # return y The index of the maximum value in , That is, the classification of prediction
# take pred Convert to y_test Data type of
pred = tf.cast(pred, dtype=y_test.dtype)
# If the classification is correct , be correct=1, Otherwise 0, take bool The result of type is converted to int type
correct = tf.cast(tf.equal(pred, y_test), dtype=tf.int32)
# Each one batch Of correct The numbers add up
correct = tf.reduce_sum(correct)
# Will all batch Medium correct The numbers add up
total_correct += int(correct)
# total_number Is the total number of samples tested , That is to say x_test The number of rows ,shape[0] Returns the number of rows of the variable
total_number += x_test.shape[0]
# The total accuracy is equal to total_correct/total_number
acc = total_correct / total_number
test_acc.append(acc)
print("Test_acc:", acc)
print("--------------------------")
# draw loss curve
plt.title('Loss Function Curve') # Picture title
plt.xlabel('Epoch') # x Axis variable name
plt.ylabel('Loss') # y Axis variable name
plt.plot(train_loss_results, label="$Loss$") # Draw... Point by point trian_loss_results Value and connect , The connection icon is Loss
plt.legend() # Draw a curve icon
plt.show() # Draw an image
# draw Accuracy curve
plt.title('Acc Curve') # Picture title
plt.xlabel('Epoch') # x Axis variable name
plt.ylabel('Acc') # y Axis variable name
plt.plot(test_acc, label="$Accuracy$") # Draw... Point by point test_acc Value and connect , The connection icon is Accuracy
plt.legend()
plt.show()
边栏推荐
- 汉得企业级PaaS平台HZERO即将重磅开源!
- SAP wper (POS interface monitor) idco posting voucher ALV Report
- ig,ax = plt.subplots()
- [redis] redis high availability deployment scheme in distributed scenarios
- Upgrade MySQL 5.6 to 8.0 on Windows
- 汉得x久立特材|携手打造协同办公门户,助力IT内部规范管理
- AT4379 [AGC027E] ABBreviate
- Hande enterprise PAAS platform hzero will soon be heavily open source!
- 【OPEN HAND】汉得企业级PaaS平台HZERO重磅开源!
- 16_ Response status code
猜你喜欢
随机推荐
SAP WPER(POS接口监控器)IDCO过账凭证ALV报表
tensorflow 神经网络实现鸢尾花分类
keepalived
Codeforce d2. RGB substring (hard version) sliding window
Hande apaas low code platform Feida 2.3.0 release was officially released!
ORA-16525 dg broker不可用
Hzero enterprise level digital PAAS platform (II) | enterprise level authority system
Parse numpy.random.get_ State() and numpy.random.set_ state()
tf.placeholder
【MySQL】sql调优实战教学
Data Lake: evolution of data Lake Technology Architecture
Detailed explanation of wechat payment project practice, order creation to payment refund code
16_响应状态码
5.SSH远程服务
接招吧。最强“高并发”系统设计 46 连问,分分钟秒杀一众面试者
[Topic sharing] open source topic of hande enterprise PAAS platform hzero
Make good use of these seven tips in code review, and it is easy to establish your opposition alliance
numpy.ascontiguousarray
TensorFlow 各优化器在鸢尾花分类任务中的应用
Ffmpeg-rk3399 ffplay learning analysis