123
作者:互联网
1.
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
def sigmoid(x): # 定义网络激活函数
return 1/(1+np.exp(-x))
data_tr = pd.read_csv('C:/Users/CHEN/Documents/WeChat Files/wxid_t1xqjm4fkg2v22/FileStorage/File/2022-03/3.3 data_tr.txt') # 训练集样本
data_te = pd.read_csv('C:/Users/CHEN/Documents/WeChat Files/wxid_t1xqjm4fkg2v22/FileStorage/File/2022-03/3.3 data_te.txt') # 测试集样本
n = len(data_tr)
yita = 0.85 # 自己设置学习速率
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 = []
'''
模型训练
'''
for j in range(1000):
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))#误差
print(it, '个样本的模型输出:', res, 'real:', real)
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))
print(w_mid,w_out)
plt.plot(Err)#训练集上每一轮的平均误差
plt.show()
plt.close()
'''
将测试集样本放入训练好的网络中去
'''
error_te = []
for it in range(len(data_te)):
net_in = np.array([data_te.iloc[it, 0], data_te.iloc[it, 1], -1]) # 网络输入
real = data_te.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_te.append(abs(real-res))
plt.plot(error_te)#测试集上每一轮的误差
plt.show()
np.mean(error_te)
2.
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
def sigmoid(x): # 定义网络激活函数
return 1/(1+np.exp(-x))
data_tr = pd.read_csv('C:/Users/CHEN/Documents/WeChat Files/wxid_t1xqjm4fkg2v22/FileStorage/File/2022-03/3.3 data_tr.txt') # 训练集样本
data_te = pd.read_csv('C:/Users/CHEN/Documents/WeChat Files/wxid_t1xqjm4fkg2v22/FileStorage/File/2022-03/3.3 data_te.txt') # 测试集样本
n = len(data_tr)
yita = 0.85 # 自己设置学习速率
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 = []
'''
模型训练
'''
for j in range(1000):
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))#误差
print(it, '个样本的模型输出:', res, 'real:', real)
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))
print(w_mid,w_out)
plt.plot(Err)#训练集上每一轮的平均误差
plt.show()
plt.close()
'''
将测试集样本放入训练好的网络中去
'''
error_te = []
for it in range(len(data_te)):
net_in = np.array([data_te.iloc[it, 0], data_te.iloc[it, 1], -1]) # 网络输入
real = data_te.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_te.append(abs(real-res))
plt.plot(error_te)#测试集上每一轮的误差
plt.show()
np.mean(error_te)
标签:real,res,mid,123,te,data,out 来源: https://www.cnblogs.com/lzjlzjlzj/p/16036016.html