致敬环节:https://blog.csdn.net/Snoopy_Yuan/article/details/70846554
因为太难了,我选择直接抄,基本无改动。。。
但有点意思的是,自适应学习率的最后,计算出的错误率是0.013,和固定学习速率一样,猜测源代码大神正好碰上了抖动,而我没碰上。
不过从理解上,学习速率优化的目的是提升学习效率,画图能看到错误率减小的速率明显提升,但大量训练之后,应该和学习率关联不明显了。
主程序adaptive_learningrate_BPnetwork.py
import pandas as pd
import matplotlib.pyplot as plt
#在线加载UCI数据集
from urllib.request import urlopen
url = "http://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data"
raw_data = urlopen(url) # download the file
attr = ['sepal_length','sepal_width','petal_length','petal_width','species']
dataset = pd.read_csv(raw_data, delimiter=",", header = None, names = attr) #names为列名列表
#读取四个输入变量的值,iloc是根据列索引读取
X = dataset.iloc[:,:4].get_values()
# label (generation after transform output to categorical variables)
dataset.iloc[:,-1] = dataset.iloc[:,-1].astype('category')
label = dataset.iloc[:,4].values.categories #类别名称
# output 1 (generation after string categorical variables to numerical values)
dataset.iloc[:,4].cat.categories = [0,1,2]
y = dataset.iloc[:,4].get_values() #分类这列的数值,赋值替换成0 1 2
# output 2 (generation after one hot encoding)
Y = pd.get_dummies(dataset.iloc[:,4]).get_values() #独热编码
#划分测试集和验证集,样本占比0.5,随机数种子是用某种规则生成随机数
from sklearn.cross_validation import train_test_split
train_X, test_X, train_y, test_y, train_Y, test_Y = train_test_split(X,y,Y,test_size = 0.5, random_state = 42)
#构造BP网络
from BP_network import *
bpn1 = BP_network() # 初始化BP网络
bpn1.CreateNN(4, 5, 3, actfun = 'Sigmoid', learningrate = 0.05) # build the network
'''
#固定学习率测试
e = []
for i in range(1000):
err, err_k = bpn1.TrainStandard(train_X, train_Y)
e.append(err)
# draw the convergence curve of output error by each step of iteration
import matplotlib.pyplot as plt
f1 = plt.figure(1)
plt.xlabel("epochs")
plt.ylabel("error")
plt.ylim(0, 1)
plt.title("training error convergence curve with fixed learning rate")
# plt.title("training error convergence curve\n learning rate = 0.05")
plt.plot(e)
plt.show()
# get the test error in test set
pred = bpn1.PredLabel(test_X);
count = 0
for i in range(len(test_y)):
if pred[i] == test_y[i]: count += 1
test_err = 1 - count / len(test_y)
print("test error rate: %.3f" % test_err)
'''
#自适应学习率测试
bpn2 = BP_network() # initial a BP network class
bpn2.CreateNN(4, 5, 3, actfun='Sigmoid', learningrate=0.05) # build the network
e = []
for i in range(1000):
err, err_k = bpn2.TrainStandard_Dynamic_Lr(train_X, train_Y)
e.append(err)
# draw the