简单的3层神经网络实现

使用sigmoid作为激活函数的极简3层神经网络(前向)范例

导入numpy库

1
import numpy as np

定义sigmoid函数

1
2
def sigmoid(x):
return 1/(1+np.exp(-x))

初始化神经网络

1
2
3
4
5
6
7
8
9
def init_network():
network={}
network['w1']=np.array([[0.1,0.3,0.5],[0.2,0.4,0.6]])
network['w2']=np.array([[0.1,0.4],[0.2,0.5],[0.3,0.6]])
network['w3']=np.array([[0.1,0.3],[0.2,0.4]])
network['b1']=np.array([0.1,0.2,0.3])
network['b2']=np.array([0.1,0.2])
network['b3']=np.array([0.1,0.2])
return network

定义神经网络

1
2
3
4
5
6
7
8
9
10
11
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

执行

1
2
3
4
network=init_network()
x=np.array([1.0,0.5])
y=forward(network,x)
print(y)import numpy as np

神经网络

文章作者: GeYu
文章链接: https://nuistgy.github.io/2018/08/19/简单的3层神经网络实现/
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 Yu's Blog