1.代码如下:
# -*- coding: utf-8 -*-
"""
Created on Mon Mar 14 18:58:05 2022
@author: L
"""
import math
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from pandas import DataFrame,Series
#定义函数:
def sigmoid(x):
return 1/(1+math.exp(-x))
#读取数据
data_tr=pd.read_csv('D:\text_class\data_tr.txt')
data_te=pd.read_csv('D:\text_class\data_te.txt')
#设置学习量
yita=0.3
#设置结构
Err = []
Net_in=DataFrame(0.6,index=['input1','input2','theata'],columns=['a'])
Out_in=DataFrame(0,index=['input1','input2','input3','input4','theata'],columns=['a'])
Net_in.iloc[2,0]=-1
Out_in.iloc[4,0]=-1
W_mid=DataFrame(0.5,index=['input1','input2','theata'],columns=['mid1','mid2','mid3','mid4'])
W_out=DataFrame(0.5,index=['input1','input2','input3','input4','theata'],columns=['a'])
W_mid_delta=DataFrame(0,index=['input1','input2','theata'],columns=['mid1','mid2','mid3','mid4'])
W_out_delta=DataFrame(0,index=['input1','input2','input3','input4','theata'],columns=['a'])
#设置训练
n=len(data_tr)
for j in range(1000):
error = []
for i in range(n):
Net_in.iloc[0,0]=data_tr.iloc[i,0]
Net_in.iloc[1,0]=data_tr.iloc[i,1]
real=data_tr.iloc[i,2]
for t in range(0,4):
Out_in.iloc[t,0]=sigmoid(sum(W_mid.iloc[:,t]*Net_in.iloc[:,0]))
res=sigmoid(sum(Out_in.iloc[:,0]*W_out.iloc[:,0]))
error.append(abs(res-real))
print(i,':',res,real)
W_out_delta.iloc[:,0]=yita*res*(1-res)*(real-res)*Out_in.iloc[:,0]
W_out_delta.iloc[4,0]=-(yita*res*(1-res)*(real-res))
W_out=W_out+W_out_delta
for g in range(0,4):
W_mid_delta.iloc[:,g]=yita*Out_in.iloc[g,0]*(1-Out_in.iloc[g,0])*W_out.iloc[g,0]*res*(1-res)*(real-res)*Net_in.iloc[:,0]
W_mid_delta.iloc[2,g]=-(yita*Out_in.iloc[g,0]*(1-Out_in.iloc[g,0])*W_out.iloc[g,0]*res*(1-res)*(real-res))
W_mid=W_mid+W_mid_delta
Err.append(np.mean(error))
print(W_mid,W_out)
plt.plot(Err)#训练集上每一轮的平均误差
plt.show()
plt.close()
#测试部分
error_te=[]
k=len(data_te)
Net_in.iloc[2,0]=-1
Out_in.iloc[4,0]=-1
for i in range(k):
Net_in.iloc[0,0]=data_te.iloc[i,0]
Net_in.iloc[1,0]=data_te.iloc[i,1]
real=data_te.iloc[i,2]
for t in range(0,4):
Out_in.iloc[t,0]=sigmoid(sum(W_mid.iloc[:,t]*Net_in.iloc[:,0]))
res=sigmoid(sum(Out_in.iloc[:,0]*W_out.iloc[:,0]))
error_te.append(abs(res-real))
plt.plot(error_te)#训练集上每一轮的平均误差
plt.show()
plt.close()
该代码使用了DataFrame模块,运行速度会很慢,训练1000次需要30多分钟,可用数组代替,效率会快很多
运行结果如下:
2.求出预测值
题如下:
代码如下:
# -*- coding: utf-8 -*-
"""
Created on Sun Mar 13 20:56:46 2022
@author: L
"""
import math
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from pandas import DataFrame,Series
#定义函数:
def sigmoid(x):
return 1/(1+math.exp(-x))
#读取数据
data_tr=pd.read_csv('D:\text_class\data_tr_else.txt')
data_te=pd.read_csv('D:\text_class\data_te_else.txt')
#设置学习量
yita=0.3
#设置结构
Err = []
Net_in=DataFrame(0.6,index=['input1','input2','theata'],columns=['a'])
Out_in=DataFrame(0,index=['input1','input2','input3','input4','theata'],columns=['a'])
Net_in.iloc[2,0]=-1
Out_in.iloc[4,0]=-1
W_mid=DataFrame(0.5,index=['input1','input2','theata'],columns=['mid1','mid2','mid3','mid4'])
W_out=DataFrame(0.5,index=['input1','input2','input3','input4','theata'],columns=['a'])
W_mid_delta=DataFrame(0,index=['input1','input2','theata'],columns=['mid1','mid2','mid3','mid4'])
W_out_delta=DataFrame(0,index=['input1','input2','input3','input4','theata'],columns=['a'])
#设置训练
n=len(data_tr)
for j in range(1000):
error = []
for i in range(n):
Net_in.iloc[0,0]=data_tr.iloc[i,0]
Net_in.iloc[1,0]=data_tr.iloc[i,1]
real=data_tr.iloc[i,2]
for t in range(0,4):
Out_in.iloc[t,0]=sigmoid(sum(W_mid.iloc[:,t]*Net_in.iloc[:,0]))
res=sigmoid(sum(Out_in.iloc[:,0]*W_out.iloc[:,0]))
error.append(abs(res-real))
print(i,':',res,real)
W_out_delta.iloc[:,0]=yita*res*(1-res)*(real-res)*Out_in.iloc[:,0]
W_out_delta.iloc[4,0]=-(yita*res*(1-res)*(real-res))
W_out=W_out+W_out_delta
for g in range(0,4):
W_mid_delta.iloc[:,g]=yita*Out_in.iloc[g,0]*(1-Out_in.iloc[g,0])*W_out.iloc[g,0]*res*(1-res)*(real-res)*Net_in.iloc[:,0]
W_mid_delta.iloc[2,g]=-(yita*Out_in.iloc[g,0]*(1-Out_in.iloc[g,0])*W_out.iloc[g,0]*res*(1-res)*(real-res))
W_mid=W_mid+W_mid_delta
Err.append(np.mean(error))
print(W_mid,W_out)
plt.plot(Err)#训练集上每一轮的平均误差
plt.show()
plt.close()
#测试部分
k=len(data_te)
Net_in.iloc[2,0]=-1
Out_in.iloc[4,0]=-1
for i in range(k):
Net_in.iloc[0,0]=data_te.iloc[i,0]
Net_in.iloc[1,0]=data_te.iloc[i,1]
for t in range(0,4):
Out_in.iloc[t,0]=sigmoid(sum(W_mid.iloc[:,t]*Net_in.iloc[:,0]))
res=sigmoid(sum(Out_in.iloc[:,0]*W_out.iloc[:,0]))
print('第'+str(i)+'个预测值为: '+str(res))
运行结果如下:
3008