import math import pandas as pd import numpy as np import matplotlib.pyplot as plt data_tr = pd.read_csv('3.3 data_tr.txt') # 训练集样本 data_te = pd.read_csv('3.3 data_te.txt') # 测试集样本 n = len(data_tr) yita = 0.1 # 学习率 out_in = np.array([0.0, 0, 0, 0, -1]) # 输出层的输入,即隐层的输出 w_mid = np.zeros([3,4]) # 隐层神经元的权值&阈值 w_out = np.zeros([5]) # 输出层神经元的权值阈值 delta_w_out = np.zeros([5]) # 输出层权值、阈值的修正量 delta_w_mid = np.zeros([3,4]) # 中间层权值、阈值的修正量 Err = [] def set_chinese(): # 设置字体 import matplotlib matplotlib.rcParams['font.sans-serif'] = ['SimHei'] matplotlib.rcParams['axes.unicode_minus'] = False def sigmoid(x): # 激活函数 return 1/(1+math.exp(-x)) for j in range(500): error = [] for it in range(n): net_in = np.array([data_tr.iloc[it, 0], data_tr.iloc[it, 1], -1]) # 网络输入 real = data_tr.iloc[it, 2] for i in range(4): out_in[i] = sigmoid(sum(net_in * w_mid[:, i])) # 从输入到隐层的传输过程 res = sigmoid(sum(out_in * w_out)) # 模型预测值 error.append(abs(real-res))#误差 delta_w_out = yita*res*(1-res)*(real-res)*out_in # 输出层权值的修正量 delta_w_out[4] = -yita*res*(1-res)*(real-res) # 输出层阈值的修正量 w_out = w_out + delta_w_out # 更新,加上修正量 for i in range(4): delta_w_mid[:, i] = yita*out_in[i]*(1-out_in[i])*w_out[i]*res*(1-res)*(real-res)*net_in # 中间层神经元的权值修正量 delta_w_mid[2, i] = -yita*out_in[i]*(1-out_in[i])*w_out[i]*res*(1-res)*(real-res) # 中间层神经元的阈值修正量,第2行是阈值 w_mid = w_mid + delta_w_mid # 更新,加上修正量 Err.append(np.mean(error)) set_chinese() # 显示中文 fig = plt.figure() plt.plot(Err)#训练集上每一轮的平均误差 plt.title("每次训练平均误差") plt.xlabel("训练次数") plt.ylabel("误差") plt.savefig("每次训练误差.jpg") plt.show() def ceshi():
net_in = np.array([data_te.iloc[it, 0], data_te.iloc[it, 1], -1]) # 网络输入 for i in range(4): out_in[i] = sigmoid(sum(net_in * w_mid[:, i])) # 从输入到隐层的传输过程 res = sigmoid(sum(out_in * w_out)) # 模型预测值 return res if __name__ == '__main__': for it in range(len(data_te)): print('第', it + 1, '个测试值:', ceshi(),"真实值",data_te.iloc[it,2])