基礎(chǔ)介紹
以下為自己動(dòng)手寫的一個(gè)最簡(jiǎn)單最基礎(chǔ)的三層神經(jīng)網(wǎng)絡(luò)模型理疙,采用python語(yǔ)言齿梁,主要使用numpy庫(kù)進(jìn)行數(shù)學(xué)運(yùn)算。
神經(jīng)網(wǎng)絡(luò)類包含一個(gè)輸入層叠纷,一個(gè)隱藏層(最簡(jiǎn)單的矩陣乘子層)刻帚,一個(gè)輸出層。在隱藏層和輸出層采用sigmoid激活函數(shù)涩嚣。具有推導(dǎo)崇众、訓(xùn)練功能,并具有debug內(nèi)部過(guò)程輸出功能航厚。
實(shí)踐環(huán)節(jié)
以下文件包含了神經(jīng)網(wǎng)絡(luò)類
#yyznet.py
import numpy as np
from numpy.core.numeric import full
from scipy.special import expit
class yyznn:
def __init__(self, input, hidden, output, learning_rate, debug=False):
self.inode = input
self.hiddennode = hidden
self.outputnode = output
self.learning_rate = learning_rate
self.initialize()
self.debug = debug
self.activation = expit
def initialize(self):
self.wih = np.zeros((self.hiddennode, self.inode), dtype=np.float)
self.who = np.zeros((self.outputnode, self.hiddennode), dtype=np.float)
def forward(self, input_array):
input_ndarray=np.ravel(np.array(input_array)).T
hidden_in = np.matmul(self.wih, input_ndarray)
hidden_out = self.activation(hidden_in)
output_in = np.matmul(self.who, hidden_out)
output_out = self.activation(output_in)
if self.debug: print("net w:", self.wih, self.who, sep="\n")
return output_out
def fit_once(self, X, y):
X_ndarray = np.ravel(np.array(X)).T
y_ndarray = np.ravel(np.array(y)).T
if self.debug: print("in:", X_ndarray, "out:", y_ndarray, sep='\n')
hidden_in = np.matmul(self.wih, X_ndarray)
hidden_out = self.activation(hidden_in)
output_in = np.matmul(self.who, hidden_out)
output_out = self.activation(output_in)
output_error = y_ndarray - output_out
hidden_error = np.matmul(self.who.T, output_error)
if self.debug: print("error output:", output_error, "errors hidden", hidden_error, sep='\n')
if self.debug: print("old w:", self.wih, self.who, sep='\n')
self.who += self.learning_rate * np.dot(np.expand_dims((output_error * output_out*(1.-output_out)), axis=1),\
np.expand_dims(hidden_out, axis=1).T)
self.wih += self.learning_rate * np.dot(np.expand_dims((hidden_error * hidden_out*(1.-hidden_out)), axis=1),\
np.expand_dims(X, axis=1).T)
if self.debug: print("new w:", self.wih, self.who, sep='\n')
以下文件包含了調(diào)用神經(jīng)網(wǎng)絡(luò)并對(duì)矩陣進(jìn)行擬合的代碼顷歌,代碼對(duì)網(wǎng)絡(luò)進(jìn)行十次訓(xùn)練,輸出每次訓(xùn)練后的擬合結(jié)果幔睬。
#main.py
from operator import truediv
import numpy as np
import yyznet
wly = yyznet.yyznn(4, 10, 2, 1, debug=False)
X = np.array([1, 2, 3, 4])
y = np.array([0.6, 0.8])
print(wly.forward(X))
for i in range(10):
wly.fit_once(X, y)
print(wly.forward(X))
輸出的結(jié)果
[0.5 0.5]
[0.51561992 0.54673815]
[0.5298997 0.58856266]
[0.5440345 0.62834611]
[0.55768086 0.66492412]
[0.56987541 0.69618157]
[0.57982865 0.72111183]
[0.58731892 0.74013608]
[0.59259844 0.75437381]
[0.5961262 0.76499471]
[0.59837175 0.77295694]
可見(jiàn)每次梯度下降的訓(xùn)練都能夠讓擬合結(jié)果更接近[0.6, 0.8]
這個(gè)結(jié)果眯漩,神經(jīng)網(wǎng)絡(luò)有效。
下一步
下一步對(duì)于神經(jīng)網(wǎng)絡(luò)的學(xué)習(xí)中主要在于
- 編程技巧,構(gòu)建能被更加廣泛使用的赦抖、面向?qū)ο蟮膒ython神經(jīng)網(wǎng)絡(luò)
- 學(xué)習(xí)數(shù)學(xué)原理舱卡,依次為根據(jù)調(diào)整神經(jīng)網(wǎng)絡(luò)的參數(shù)