#库的导入
import numpy as np
import pandas as pd
#输入数据的导入
df = pd.read_csv("train.csv")
df.columns = ["Co", "Cr", "Mg", "Pb", "Ti"]
Co = df["Co"]
Co = np.array(Co)
Cr = df["Cr"]
Cr = np.array(Cr)
Mg=df["Mg"]
Mg=np.array(Mg)
Pb = df["Pb"]
Pb =np.array(Pb)
Ti = df["Ti"]
Ti = np.array(Ti)
samplein = np.mat([Co,Cr,Mg,Pb])
#数据归一化,将输入数据压缩至0到1之间,便于计算,后续通过反归一化恢复原始值
sampleinminmax = np.array([samplein.min(axis=1).T.tolist()[0],samplein.max(axis=1).T.tolist()[0]]).transpose()#对应最大值最小值
#待预测数据为Ti
sampleout = np.mat([Ti])
sampleoutminmax = np.array([sampleout.min(axis=1).T.tolist()[0],sampleout.max(axis=1).T.tolist()[0]]).transpose()
sampleinnorm = ((np.array(samplein.T)-sampleinminmax.transpose()[0])/(sampleinminmax.transpose()[1]-sampleinminmax.transpose()[0])).transpose()
sampleoutnorm = ((np.array(sampleout.T)-sampleoutminmax.transpose()[0])/(sampleoutminmax.transpose()[1]-sampleoutminmax.transpose()[0])).transpose()
sampleinnorm = sampleinnorm.transpose()
sampleoutnorm = sampleoutnorm.transpose()
S = 2 #模糊分级个数
T = 16 #规则生成层、归一化层节点数
iteration = 500 #迭代训练次数
learningrate = 0.01 #学习率
#c1、b1分别为李隶属度层的中心点与宽度向量、w1为输出层的权值,sampleinnorm.shape[1]代表输入数据的4个类别
c1 = np.random.uniform(low=-1, high=1, size=(S, sampleinnorm.shape[1]))
b1 = np.random.uniform(low=-1, high=1, size=(S, sampleinnorm.shape[1]))
w1 = np.random.uniform(low=0, high=1, size=(1, T))
sampleinnorm = np.mat(sampleinnorm)
#开始训练
for l in range(iteration):
print("the iteration is :",l+1)
# 隶属度函数计算层计算输出
Y = sampleoutnorm.copy()
u1 = np.zeros((S, sampleinnorm.shape[1]))
alpha = []
alpha2 = []
a1 = []
for m in range(sampleinnorm.shape[0]):
for i in range(S):
for j in range(sampleinnorm.shape[1]):
u1[i][j] = np.exp((-1) * ((sampleinnorm[m, j] - c1[i][j]) ** 2) / (b1[i][j] ** 2))
# 规则生成层计算输出
alpha1 = np.zeros((T, 1))
for i in range(S):
a = 1
if i == 1:
a = 8
for p in range(S):
b = 1
if p == 1:
b = 4
for q in range(S):
c = 1
if q == 1:
c = 2
for k in range(S):
alpha1[i * a + p * b + q * c + k] = u1[i][0] * u1[p][1] * u1[q][2] * u1[k][3]
alpha.append(alpha1)
# 归一化层计算输出
alphasum = np.sum(alpha1)
alpha2.append(alphasum)
# 输出层计算输出
a2 = np.dot(w1,alpha1)
Y[m] = a2/alphasum
#计算误差
err = sampleoutnorm - Y
loss = np.sum(np.abs(err))
print(" the loss is :",loss)
#反向传播,分别计算参数w1、c1、b1的误差项
deltaw1 = np.zeros((1,T))
deltac1 = np.zeros((S, sampleinnorm.shape[1]))
deltab1 = np.zeros((S, sampleinnorm.shape[1]))
for m in range(sampleinnorm.shape[0]):
changew1 = ((err[m] * alpha[m])/alpha2[m]).transpose()
changec2 = np.zeros((S, sampleinnorm.shape[1]))
changeb2 = np.zeros((S, sampleinnorm.shape[1]))
z=np.zeros((S, sampleinnorm.shape[1]))
v=np.zeros((S, sampleinnorm.shape[1]))
for i in range(S):
for j in range(sampleinnorm.shape[1]):
z[i][j]=2*(sampleinnorm[m, j] - c1[i][j])/(b1[i][j] ** 2)
v[i][j]=2*((sampleinnorm[m, j] - c1[i][j])**2)/(b1[i][j]**3)
h = alpha[m]
for u in range(T):
changec1 = (((w1[:,u]-Y[m])*h[u,:])/alpha2[m])*z
changec2 = changec1+changec2
changeb1 = (((w1[:,u]-Y[m])*h[u,:])/alpha2[m])*v
changeb2 = changeb1+changeb2
deltac1 = deltac1 + changec2
deltab1 = deltab1 + changeb2
deltaw1 = deltaw1 + changew1
#对三个参数进行更新
c1 = c1 + learningrate * deltac1
b1 = b1 + learningrate * deltab1
w1 = w1 + learningrate * deltaw1
print('更新的w1:',w1)
print('更新的b1:',b1)
print('更新的c1:',c1)
#保存训练后的参数
np.save("c1.npy",c1)
np.save("b1.npy",b1)
np.save("w1.npy",w1)
七层楼的疯子
- 粉丝: 1w+
- 资源: 44
最新资源
- 11-【其他】17-新员工入职培训管理流程相关表单(总部级).xlsx
- 11-【其他】02-员工培训出勤统计表.xlsx
- 11-【其他】05-新员工入职培训表.xlsx
- 10-【管理系统】02-员工培训管理系统.xlsx
- 10-【管理系统】03-员工培训管理系统.xlsx
- 10-【管理系统】02-员工培训管理系统 (2).xlsx
- 08-【考核管理】12-员工培训考核统计表.xlsx
- 08-【考核管理】09-新入职员工培训成绩考核统计表.xlsx
- 10-【管理系统】01-人力资源员工培训管理系统.xlsx
- 05-【培训签到】07-新员工培训签到表.xlsx
- 06-【培训手册】01-员工培训手册内容清单.xlsx
- 08-【考核管理】08-企业新员工培训成绩统计表.xlsx
- 05-【培训签到】01-人力资源新员工培训签到表.xlsx
- 03-【培训流程】02-新员工入职培训流程.xlsx
- 01-【培训计划】09-新员工入职培训计划表.xlsx
- 01-【培训计划】11-新员工入职培训计划表.xlsx
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈