设计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
[