当前位置:文档之家› 零基础入门深度学习(3) - 神经网络和反向传播算法

零基础入门深度学习(3) - 神经网络和反向传播算法

计算一个神经元的输出的方法和计算一个感知器的输出是一样的。

假设神经元的输入是向量sigmoid函数,则其输出:sigmoid函数的导数是:神经网络其实就是按照一定规则连接起来的多个神经元。

上图展示了一个我们可以发现它的规则包括:神经元按照层来布局。

最左边的层叫做输入层,负责接收输入数据;最右边的层叫输入层和输出层之间的层叫做隐藏层,因为它们对于外部来说是不可见的。

同一层的神经元之间没有连接。

如上图,输入层有三个节点,我们将其依次编号为1、2、3;隐藏层的4个节点,编号依次为4、5、6、7;最后输出层的两个节点编号为8、9。

因为我们这个神经网络是全连接网络,所以可以看到每个节点都和和输入层的三个节点1、2、3之间都有连接,其连接上的权重分别为则每一层的输出向量的计算可以表示为:首先,我们根据上一节介绍的算法,用样本的特征,计算出神经网络中每个隐藏层节点的输出然后,我们按照下面的方法计算出每个节点的误差项:观察上图,我们发现权重仅能通过影响节点的输入值影响网络的其它部分,设如上图,可以分解出5个领域对象来实现神经网络:Network 神经网络对象,提供API接口。

它由若干层对象组成以及连接对象组成。

Layer 层对象,由多个节点组成。

Node 节点对象计算和记录节点自身的信息(比如输出值7.'''8.s e l f.u p s t r e a m_n o d e=u p s t r e a m_n o d e9.s e l f.d o w n s t r e a m_n o d e=d o w n s t r e a m_n o d e10.s e l f.w e i g h t=r a n d o m.u n i f o r m(-0.1,0.1)11.s e l f.g r a d i e n t=0.012.13.d e f c a l c_g r a d i e n t(s e l f):14.'''15.计算梯度16.'''17.s e l f.g r a d i e n t=s e l f.d o w n s t r e a m_n o d e.d e l t a*s e l f.u p s t r e a m_n o d e.o u t p u t18.19.d e f g e t_g r a d i e n t(s e l f):20.'''21.获取当前的梯度22.'''23.r e t u r n s e l f.g r a d i e n t24.25.d e f u p d a t e_w e i g h t(s e l f,r a t e):26.'''27.根据梯度下降算法更新权重28.'''29.s e l f.c a l c_g r a d i e n t()30.s e l f.w e i g h t+=r a t e*s e l f.g r a d i e n t31.32.d e f__s t r__(s e l f):33.'''34.打印连接信息35.'''36.r e t u r n'(%u-%u)->(%u-%u)=%f'%(37.s e l f.u p s t r e a m_n o d e.l a y e r_i n d e x,38.s e l f.u p s t r e a m_n o d e.n o d e_i n d e x,39.s e l f.d o w n s t r e a m_n o d e.l a y e r_i n d e x,40.s e l f.d o w n s t r e a m_n o d e.n o d e_i n d e x,41.s e l f.w e i g h t)Connections对象,提供Connection集合操作。

1.c l a s s C o n n e c t i o n s(o b j e c t):2.d e f__i n i t__(s e l f):3.s e l f.c o n n e c t i o n s=[]4.5.d e f a d d_c o n n e c t i o n(s e l f,c o n n e c t i o n):6.s e l f.c o n n e c t i o n s.a p p e n d(c o n n e c t i o n)7.8.d e f d u m p(s e l f):9.f o r c o n n i n s e l f.c o n n e c t i o n s:10.p r i n t c o n nNetwork对象,提供API。

1.c l a s s N e t w o r k(o b j e c t):2.d e f__i n i t__(s e l f,l a y e r s):3.'''4.初始化一个全连接神经网络5.l a y e r s:二维数组,描述神经网络每层节点数6.'''7.s e l f.c o n n e c t i o n s=C o n n e c t i o n s()8.s e l f.l a y e r s=[]9.l a y e r_c o u n t=l e n(l a y e r s)10.n o d e_c o u n t=0;11.f o r i i n r a n g e(l a y e r_c o u n t):12.s e l f.l a y e r s.a p p e n d(L a y e r(i,l a y e r s[i]))13.f o r l a y e r i n r a n g e(l a y e r_c o u n t-1):14.c o n n e c t i o n s=[C o n n e c t i o n(u p s t r e a m_n o d e,d o w n s t r e a m_n o d e)15.f o r u p s t r e a m_n o d e i n s e l f.l a y e r s[l a y e r].n o d e s16.f o r d o w n s t r e a m_n o d e i n s e l f.l a y e r s[l a y e r+1].n o d e s[:-1]]17.f o r c o n n i n c o n n e c t i o n s:18.s e l f.c o n n e c t i o n s.a d d_c o n n e c t i o n(c o n n)19.c o n n.d o w n s t r e a m_n o d e.a p p e n d_u p s t r e a m_c o n n e c t i o n(c o n n)20.c o n n.u p s t r e a m_n o d e.a p p e n d_d o w n s t r e a m_c o n n e c t i o n(c o n n)21.22.23.d e f t r a i n(s e l f,l a b e l s,d a t a_s e t,r a t e,i t e r a t i o n):24.'''25.训练神经网络26.l a b e l s:数组,训练样本标签。

每个元素是一个样本的标签。

27.d a t a_s e t:二维数组,训练样本特征。

每个元素是一个样本的特征。

28.'''29.f o r i i n r a n g e(i t e r a t i o n):30.f o r d i n r a n g e(l e n(d a t a_s e t)):31.s e l f.t r a i n_o n e_s a m p l e(l a b e l s[d],d a t a_s e t[d],r a t e)32.33.d e f t r a i n_o n e_s a m p l e(s e l f,l a b e l,s a m p l e,r a t e):34.'''35.内部函数,用一个样本训练网络36.'''37.s e l f.p r e d i c t(s a m p l e)38.s e l f.c a l c_d e l t a(l a b e l)39.s e l f.u p d a t e_w e i g h t(r a t e)40.41.d e f c a l c_d e l t a(s e l f,l a b e l):42.'''43.内部函数,计算每个节点的d e l t a44.'''45.o u t p u t_n o d e s=s e l f.l a y e r s[-1].n o d e s46.f o r i i n r a n g e(l e n(l a b e l)):47.o u t p u t_n o d e s[i].c a l c_o u t p u t_l a y e r_d e l t a(l a b e l[i])48.f o r l a y e r i n s e l f.l a y e r s[-2::-1]:49.f o r n o d e i n l a y e r.n o d e s:50.n o d e.c a l c_h i d d e n_l a y e r_d e l t a()。

相关主题