神经网络解决手写数字识别问题
神经网络是深度学习的基础,其强大的拟合和学习能力,让其在图像识别,人工智能方面表现十分出众,这里不介绍神经网络的原理结构,(这一部分在网上非常多),这里给出笔者利用纯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'):
#根据激活函数不同,设置不同的