代码拉取完成,页面将自动刷新
import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl
import tensorflow as tf
import pandas as pd
plt.rcParams['font.sans-serif'] = ['SimHei'] #运行配置参数中的字体(font)为黑体(SimHei)
plt.rcParams['axes.unicode_minus'] = False #运行配置参数总的轴(axes)正常显示正负号(minus)
TRAIN_URL = "http://download.tensorflow.org/data/iris_training.csv"
train_path = tf.keras.utils.get_file(TRAIN_URL.split('/')[-1],TRAIN_URL)
TEST_URL = "http://download.tensorflow.org/data/iris_test.csv"
test_path = tf.keras.utils.get_file(TEST_URL.split('/')[-1],TEST_URL)
#四个属性:花萼长度、宽度,花瓣长度、宽度
# [5. 3. 1.6 0.2 0. ]
#一个标签:0山鸢尾 1变色鸢尾 2维吉尼亚鸢尾
column_names = ['SepalLength','SepalWidth','PetalLength','PetalWidth','Species']
df_iris_train = pd.read_csv(train_path,header=0)
df_iris_test = pd.read_csv(test_path,header=0)
iris_train = np.array(df_iris_train)
iris_test = np.array(df_iris_test)
print(iris_train.shape,iris_test.shape)
#iris_train[:,2:4]取下标值为23的两列
x_train = iris_train[:,0:4]#花瓣长度、宽度
y_train = iris_train[:,4]#标签
x_test = iris_test[:,0:4]
y_test = iris_test[:,4]
num_train = len(x_train)
num_test = len(x_test)
#数据处理中心化
x_train = x_train - np.mean(x_train,axis=0)#求列属性平均值,对源数据进行中心化处理
x_test = x_test - np.mean(x_test,axis=0)
#生成多元模型属性矩阵(合并)
#tf.cast将数据格式转化成dtype数据类型,tf.concat用来拼接张量的函数
X_train = tf.cast(x_train,dtype=tf.float32)
#相当于将多个数值联合放在一起作为多个相同类型的向量,可用于表示各自的概率分布
Y_train = tf.one_hot(tf.constant(y_train,dtype=tf.int32),3)
X_test = tf.cast(x_test,dtype=tf.float32)
Y_test = tf.one_hot(tf.constant(y_test,dtype=tf.int32),3)#将标签值转换为独热编码的形式
# 第三步:设置超参数
learn_rate = 0.5 #学习率
iter = 100 #迭代次数
display_step = 10 #显示间隔
# 第四步:设置模型参数初始值
np.random.seed(612)
#隐含层(单隐含层,16神经元)
w1 = tf.Variable(np.random.randn(4,16),dtype=tf.float32)
b1 = tf.Variable(np.zeros([16]),dtype=tf.float32)
#输出层
w2 = tf.Variable(np.random.randn(16,3),dtype=tf.float32)
b2 = tf.Variable(np.zeros([3]),dtype=tf.float32)
#第五步:训练模型
cce_train = []#记录训练集上的损失
cce_test = []#记录测试集上的损失
acc_train = []#准确率
acc_test = []
for i in range(0,iter+1):
#自动求导
with tf.GradientTape() as tape:#梯度带对象的with语句,实现对w的自动监视
hidden_train = tf.nn.relu(tf.matmul(X_train,w1)+b1)#隐含层激活函数
pred_train = tf.nn.softmax(tf.matmul(hidden_train,w2)+b2)#输出层激活函数
loss_train = tf.reduce_mean(tf.keras.losses.categorical_crossentropy(y_true=Y_train,y_pred=pred_train))
hidden_test = tf.nn.relu(tf.matmul(X_test,w1)+b1)#隐含层激活函数
pred_test = tf.nn.softmax(tf.matmul(hidden_test,w2)+b2)#输出层激活函数
loss_test = tf.reduce_mean(tf.keras.losses.categorical_crossentropy(y_true=Y_test,y_pred=pred_test))
accuracy_train = tf.reduce_mean(tf.cast(tf.equal(tf.argmax(pred_train.numpy(),axis=1),y_train),tf.float32))#用于在给定轴上找到张量中最大值的索引
accuracy_test = tf.reduce_mean(tf.cast(tf.equal(tf.argmax(pred_test.numpy(),axis=1),y_test),tf.float32))
cce_train.append(loss_train)# 把训练集得到的均方误差加入列表 ce_train
cce_test.append(loss_test) # 把训练集得到的均方误差加入列表 ce_test
acc_train.append(accuracy_train)
acc_test.append(accuracy_test)
grads = tape.gradient(loss_train,[w1,b1,w2,b2])#求偏导
# 然后使用迭代公式更新 w
w1.assign_sub(learn_rate*grads[0])
b1.assign_sub(learn_rate*grads[1])
w2.assign_sub(learn_rate*grads[2])
b2.assign_sub(learn_rate*grads[3])
#训练误差和测试误差都是一直单调递减的,在测试集上,损失的下降更快。
if i % display_step == 0:
print("i:",i," ,trainacc:",accuracy_train.numpy()," ,trainloss:",loss_train.numpy(),
" ,testacc:",accuracy_test.numpy(), " ,testloss:",loss_test.numpy())
plt.figure(figsize=(10,4))
plt.subplot(121)
plt.plot(cce_train,color="blue",label="train")
plt.plot(cce_test,color="red",label="test")
plt.xlabel("迭代次数")
plt.ylabel("loss")
plt.legend()
plt.subplot(122)
plt.plot(acc_train,color="blue",label="train")
plt.plot(acc_test,color="red",label="test")
plt.xlabel("迭代次数")
plt.ylabel("accuracy")
plt.legend()
plt.suptitle("训练集和测试集的损失曲线和迭代率曲线",fontsize=14)
plt.show()
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。