本文主要介紹了Python利用numpy實(shí)現(xiàn)三層神經(jīng)網(wǎng)絡(luò)的示例代碼,分享給大家,具體如下:
其實(shí)神經(jīng)網(wǎng)絡(luò)很好實(shí)現(xiàn),稍微有點(diǎn)基礎(chǔ)的基本都可以實(shí)現(xiàn)出來.主要都是利用上面這個(gè)公式來做的。
這是神經(jīng)網(wǎng)絡(luò)的整體框架,一共是三層,分為輸入層,隱藏層,輸出層?,F(xiàn)在我們先來講解下從輸出層到到第一個(gè)隱藏層。
使用的編譯器是jupyter notebook
import numpy as np
#定義X,W1,B1
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(X.shape)
print(w1.shape)
print(b1.shape)
def sigmod(x):
return 1/(1 + np.exp(-x))
Z1 = sigmod(A1)
Z1
#定義w2,b2
w2 = np.array([[0.1,0.4],[0.2,0.5],[0.3,0.6]])
b2 = np.array([0.1,0.2])
#查看他們的行狀
print(w2.shape)
print(b2.shape)
A2 = np.dot(Z1,w2) + b2
A2
#定義恒等函數(shù)
def identity_function(x):
return x
#定義w3,b3
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 = identity_function(A3)
Y
將上面的整合一下
#整理
#定義一個(gè)字典,將權(quán)重全部放入字典
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
#定義函數(shù),導(dǎo)入權(quán)重與x,得到Y(jié)
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
A2 = np.dot(A1,w2) + b2
A3 = np.dot(A2,w3) + b3
Y = identity_function(A3)
Y
#調(diào)用函數(shù)
network = init_network()
X = np.array([1.0,0.5])
Y = forward(network,X)
到此這篇關(guān)于Python利用numpy實(shí)現(xiàn)三層神經(jīng)網(wǎng)絡(luò)的示例代碼的文章就介紹到這了,更多相關(guān)numpy三層神經(jīng)網(wǎng)絡(luò)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
您可能感興趣的文章:- Numpy實(shí)現(xiàn)卷積神經(jīng)網(wǎng)絡(luò)(CNN)的示例
- numpy實(shí)現(xiàn)神經(jīng)網(wǎng)絡(luò)反向傳播算法的步驟
- 純用NumPy實(shí)現(xiàn)神經(jīng)網(wǎng)絡(luò)的示例代碼
- Python使用numpy實(shí)現(xiàn)BP神經(jīng)網(wǎng)絡(luò)
- Python基于numpy靈活定義神經(jīng)網(wǎng)絡(luò)結(jié)構(gòu)的方法
- numpy創(chuàng)建神經(jīng)網(wǎng)絡(luò)框架