接下来我将通过Numpy数组来实现神经网络的前向处理
我们首先需要了解各层间信号传递的实现,以输入层到第一层的第一个神经元的信号传递为例
流程代码如下
#通过Numpy数组实现第一层信号传递
import numpy as np
import matplotlib.pylab as plt
def sigmoid(x):
return 1/(1+np.exp(-x))
X=np.array([1.0,0.5])
W1=np.array([[0.1,0.3,0.5],[0.2,0.4,0.6]])
B1=np.array([0.1,0.2,0.3])
print(W1.shape)
print(X.shape)
print(B1.shape)
'''
输出结果为:
(2, 3)
(2,)
(3,)
'''
A1=np.dot(X,W1)+B1
Z1=sigmoid(A1)
print(A1)
print(Z1)
#接下来是第一层到第二层的信号传递
W2=np.array([[0.1,0.4],[0.2,0.5],[0.3,0.6]])
B2=np.array([0.1,0.2])
A2=np.dot(Z1,W2)+B2
Z2=sigmoid(A2)
#最后是第二层到传输层的信号传递,输出层的实现和之前基本一致,但最后的激活函数和之前的隐藏层有所不同
def identify_function(x):
return x
W3=np.array([[0.1,0.3],[0.2,0.4]])
B3=np.array([0.1,0.2])
A3=np.dot(Z2,W3)+B3
Y=identify_function(A3)#通过恒等函数进行输出
通过对上述代码进行整理,我们按照神经网络的实现惯例来实现三层神经网络
#三层神经网络的实现
import numpy as np
def sigmoid(x):
return 1/(1+np.exp(-x))
def init_network():
network={}
network['W1']=np.array([[0.1,0.3,0.5],[0.2,0.4,0.6]])
network['b1']=np.array([0.1,0.2,0.3])
network['W2']=np.array([[0.1,0.4],[0.2,0.5],[0.3,0.6]])
network['b2']=np.array([0.1,0.2])
network['W3']=np.array([[0.1,0.3],[0.2,0.4]])
network['b3']=np.array([0.1,0.2])
return network
def forward(network,x):
W1,W2,W3=network['W1'],network['W2'],network['W3']
b1,b2,b3=network['b1'],network['b2'],network['b3']
a1=np.dot(x,W1)+b1
Z1=sigmoid(a1)
a2=np.dot(Z1,W2)+b2
Z2=sigmoid(a2)
a3=np.dot(Z2,W3)+b3
y=a3
return y
network=init_network()
x=np.array([1.0,0.5])
y=forward(network,x)
print(y)#[0.31682708 0.69627909]