分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow
也欢迎大家转载本篇文章。分享知识,造福人民,实现我们中华民族伟大复兴!
如果以面向对象(OOP)的方式进行BP神经网络系统的设计与实践的话,因为权值的初始化以及类的构造都只进行一次(而且发生在整个流程的开始阶段),所以自然地将权值(全部层layer之间的全部权值)初始化的过程放在类的构函数中,而权值的初始化,一种trivial常用的初始化方法为,对各个权值使用均值为0方差为1的正态分布(也即np.random.randn(shape)
)进行初始化,也即:
class Network(object): # topology:表示神经网络拓扑结构,用list或者tuple来实现, # 比如[784, 30, 10],表示784个神经元的输入层,30个neuron的隐层,以及十个neuron的输出层 def __init__(self, topology): self.topology = topology self.num_layers = len(topology) self.biases = [np.random.randn(y, 1) for y in topology[1:]] self.weights = [np.random.randn(y, x) for x, y in zip(self.weights[:-1], self.weights[1:])]
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
我们不妨以一个简单的具体例子,分析这种初始化方法可能存在的缺陷,如下图所示:
为了简化问题,我们只以最左一层向中间一层的第一个神经元(neuron)进行前向(forward)传递为例进行分析,假设一个输入样本(特征向量, ,其形式如下:
相关代码:
def default_weight_init(self): self.biases = [np.random.randn(y, 1)/y for y in self.topology[1:]] self.weights = [np.random.rand(y, x)/np.sqrt(x) for x, y in zip(self.topology[:-1], self.topology[1:])]def large_weight_init(self): self.biases = [np.random.randn(y, 1) for y in self.topoloy[1:]] self.weights = [np.random.randn(y, x)/np.sqrt(x) for x, y in zip(self.topology[:-1], self.topology[1:])]
- 1
- 2
- 3
- 4
- 5
- 6
- 7
我们看到正是这样一种np.random.randn(y, x)
向np.random.randn(y, x)/np.sqrt(x)
小小的改变,却暗含着丰富的概率论与数理统计的知识,可见无时无刻无处不体现着数学工具的强大。