专栏名称: 算法与数据结构
算法与数据结构知识、资源分享
目录
相关文章推荐
51好读  ›  专栏  ›  算法与数据结构

神经网络理论基础及Python实现

算法与数据结构  · 公众号  · 算法  · 2017-03-09 10:30

正文

请到「今天看啥」查看全文


tanh ( x )

def logistic ( x ) :

return 1 / ( 1 + np . exp ( - x ))

def logistic_derivative ( x ) :

return logistic ( x ) * ( 1 - logistic ( x ))


设计BP神经网络的形式(几层,每层多少单元个数),用到了面向对象,主要是选择哪种非线性函数,以及初始化权重。layers是一个list,里面包含每一层的单元个数。


class NeuralNetwork :

def __init__ ( self , layers , activation = 'tanh' ) :

"""

:param layers: A list containing the number of units in each layer.

Should be at least two values

:param activation: The activation function to be used. Can be

" logistic " or " tanh "

"""

if activation == 'logistic' :

self . activation = logistic

self . activation_deriv = logistic_derivative

elif activation == 'tanh' :

self . activation = tanh

self . activation_deriv = tanh_deriv

self . weights = []

for i in range ( 1 , len ( layers ) - 1 ) :

self . weights . append (( 2 * np . random . random (( layers [ i - 1 ] + 1 , layers [ i ] + 1 )) - 1 ) * 0.25 )

self . weights . append (( 2 * np . random . random (( layers [







请到「今天看啥」查看全文