需要联网,以及安装一些基本的库
数据集是iris数据集,前向传播和反向传播用了矩阵形式,感兴趣的可以推导一下公式
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
def sigmoid(inX):
return 1 / (1 + np.exp(-inX))
def createData():
df = pd.read_csv('http://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data',
header=None) # 加载Iris数据集作为DataFrame对象
X = df.iloc[:, [0, 2]].values # 取出2个特征,并把它们用Numpy数组表示
X = X[:] # 复制一个切片取出后面100个用例
y = np.zeros((150, 3))
for i in range(150):
if i < 50:
y[i][0] = 1
elif i < 100:
y[i][1] = 1
else:
y[i][2] = 1
plt.scatter(X[:50, 0], X[:50, 1], color='red', marker='o', label='setosa') # 前50个样本的散点图
plt.scatter(X[50:100, 0], X[50:100, 1], color='blue', marker='x', label='versicolor') # 中间50个样本的散点图
plt.scatter(X[100:, 0], X[100:, 1], color='green', marker='+', label='Virginica') # 后50个样本的散点图
plt.xlabel('petal length')
plt.ylabel('sepal length')
plt.legend(loc=2) # 把说明放在左上角,具体请参考官方文档
return X,y
def gradAscent(dataMat, classLabels, d, q, l):
dataMatrix = np.mat(dataMat).transpose()
n, m = np.shape(dataMatrix)
W = np.ones((q, l))
V = np.ones((d, q))
gamma = np.ones((q, 1))
sita = np.ones((l, 1))
g = np.ones((l, 1))
e = np.ones((q, 1))
learningRate = 0.5
maxCycles = 1000
correctCount = 0
for i in range(maxCycles):
# SGD
if i % 100 == 0:
correctCount = 0
for k in range(m):
# 前向传播
alpha = V.transpose() * dataMatrix[:, k]
b = sigmoid(alpha - gamma)
beta = W.transpose() * b
predict = sigmoid(beta - sita)
if i % 100 == 0:
print("第" + str(i) + "轮:")
print("第" + str(k + 1) + "个用例预测值为:"+ str(np.argmax(predict) + 1))
print("实际值为:" + str(np.argmax(classLabels[k]) + 1))
if(np.argmax(predict) == np.argmax(classLabels[k])):
correctCount += 1
# 反向传播
for j in range(l):
g[j] = (classLabels[k][j] - predict[j]) * predict[j] * (1 - predict[j])
W = W + learningRate * b * g.transpose()
sita = sita - learningRate * g
for h in range(q):
e[h] = b[h] * (1 - b[h]) * W[h] * g
V = V + learningRate * dataMatrix[:, k] * e.transpose()
gamma = gamma - learningRate * e
if i % 100 == 0:
print("第" + str(i) + "轮准确率:" + str(correctCount / 150))
if __name__ == "__main__":
X, y = createData()
# 输出神经元个数l = 3,隐层神经元个数q = 5,输入神经元个数d = 2
l = 3
q = 5
d = 2
gradAscent(X, y, 2, 5, 3)
plt.show()