神经网络解决手写数字识别问题
神经网络是深度学习的基础,其强大的拟合和学习能力,让其在图像识别,人工智能方面表现十分出众,这里不介绍神经网络的原理结构,(这一部分在网上非常多),这里给出笔者利用纯python写的神经网络代码,实现了对sklearn库中的digits数据集的识别,准确率在93%以上
代码如下,详细见代码注释
import numpy as np
#激活函数tanh
def tanh(x):
return np.tanh(x)
#tanh的导函数,为反向传播做准备
def tanh_deriv(x):
return 1-np.tanh(x)*np.tanh(x)
#激活函数逻辑斯底回归函数
def logistic(x):
return 1/(1+np.exp(-x))
#激活函数logistic导函数
def logistic_deriv(x):
return logistic(x)*(1-logistic(x))
#神经网络类
class NeuralNetwork:
def __init__(self,layers,activation='tanh'):
#根据激活函数不同,设置不同的激活函数和其导函数
if activation == 'logistic':
self.activation = logistic
self.activation_deriv = logistic_deriv
elif activation == 'tanh':
self.activation = tanh
self.activation_deriv = tanh_deriv
#初始化权重向量,从第一层开始初始化前一层和后一层的权重向量
self.weights = []
for i in range(1 , len(layers)-1):
#权重的shape,是当前层和前一层的节点数目加1组成的元组
self.weights.append((2*np.random.random((layers[i-1]+1,layers[i]+1))-1)*0.25)
#权重的shape,是当前层加1和后一层组成的元组
self.weights.append((2*np.random.random((layers[i]+1,layers[i+1]))-1)*0.25)
#fit函数对元素进行训练找出合适的权重,X表示输入向量,y表示样本标签,learning_rate表示学习率
#epochs表示循环训练次数
def fit(self , X , y , learning_rate=0.2 , epochs=10000):
X = np.atleast_2d(X)#保证X是二维矩阵
temp = np.ones([X.shape[0],X.shape[1]+1])
temp[:,0:-1] = X
X = temp #以上三步表示给X多加一列值为1
y = np.array(y)#将y转换成np中array的形式
#进行训练
for k in range(epochs):
i = np.random.randint(X.shape[0])#从0-epochs任意挑选一行
a = [X[i]]#将其转换为list
#前向传播
for l in range(len(self.weights)):
a.append(self.activation(np.dot(a[l],self.weights[l])))
#计算误差
error = y[i] - a[-1]
deltas = [error * self.activation_deriv(a[-1])]
#反向传播,不包括输出层
for l in range(len(a)-2,0,-1):
deltas.append(deltas[-1].dot(self.weights[l].T)*self.activation_deriv(a[l]))
deltas.reverse()
#更新权重
for i in range(len(self.weights)):
layer = np.atleast_2d(a[i])
delta = np.atleast_2d(deltas[i])
self.weights[i] += learning_rate*layer.T.dot(delta)
#进行预测
def predict(self,x):
x = np.array(x)
temp = np.ones(x.shape[0]+1)
temp[0:-1] = x
a = temp
for l in range(0,len(self.weights)):
a = self.activation(np.dot(a,self.weights[l]))
return a
解决异或问题
if __name__ == '__main__':
nn = NeuralNetwork([2,2,1],'tanh')
X = np.array([[0,0],[0,1],[1,0],[1,1]])
y = np.array([0,1,1,0])
nn.fit(X,y)
for i in [[0,0],[0,1],[1,0],[1,1]]:
print i,nn.predict(i)
解决识别手写数字
import numpy as np
from sklearn.datasets import load_digits
from sklearn.metrics import confusion_matrix,classification_report
from sklearn.preprocessing import LabelBinarizer
from sklearn.preprocessing import MinMaxScaler
from sklearn.cross_validation import train_test_split
if __name__ == '__main__':
#加载数字数据集
digits = load_digits()
X = digits.data
y = digits.target
#对X进行最大最小值缩放
X = MinMaxScaler().fit_transform(X)
#生成一个64*100*10的神经网络,激活函数是logistic
nn = NeuralNetwork([64,100,10],'logistic')
X_train,X_test,y_train,y_test = train_test_split(X,y)
#对标签进行标签化
labels_train = LabelBinarizer().fit_transform(y_train)
labels_test = LabelBinarizer().fit_transform(y_test)
print 'start fitting'
nn.fit(X_train,labels_train,epochs=3000)
predictions = []
for i in range(X_test.shape[0]):
o = nn.predict(X_test[i])
predictions.append(np.argmax(o))//选择概率最大的下标作为预测结果
#预测结果
print predictions
#混淆矩阵
print confusion_matrix(y_test,predictions)
#分类报告
print classification_report(y_test,predictions)
网友评论