用C語言從頭開始實現(xiàn)一個神經(jīng)網(wǎng)絡(luò)
本文轉(zhuǎn)自雷鋒網(wǎng),如需轉(zhuǎn)載請至雷鋒網(wǎng)官網(wǎng)申請授權(quán)。
在本文中,我們將用C語言從頭開始實現(xiàn)一個基本的神經(jīng)網(wǎng)絡(luò)框架。之所以在C語言中這樣做,是因為大多數(shù)庫和其他高級語言(如Python)都抽象出了實現(xiàn)細(xì)節(jié)。在C語言中實現(xiàn)反向傳播實際上會讓我們更詳細(xì)地了解改變權(quán)重和偏差是如何改變網(wǎng)絡(luò)的整體行為的。
注意:本文假設(shè)您了解反向傳播算法背后的數(shù)學(xué)原理。
我們的目標(biāo)是建立一個通用的框架,其中的層數(shù)和神經(jīng)元將由用戶根據(jù)他的要求指定。因此,我們將從用戶獲得以下輸入來定義我們的神經(jīng)網(wǎng)絡(luò)框架:
1. 層數(shù)
2.每層神經(jīng)元數(shù)目
3.學(xué)習(xí)速率
4.訓(xùn)練例子
5.輸出標(biāo)簽
定義層和神經(jīng)元結(jié)構(gòu):
一旦我們有了層的數(shù)量和每層神經(jīng)元的數(shù)量,我們就可以創(chuàng)建我們的神經(jīng)網(wǎng)絡(luò)的架構(gòu)。但首先我們必須定義神經(jīng)元和層的結(jié)構(gòu)。
神經(jīng)元結(jié)構(gòu)將包含以下參數(shù):
層結(jié)構(gòu)將有許多神經(jīng)元在該層和一個指針的neuron_t結(jié)構(gòu)。
創(chuàng)建架構(gòu):
現(xiàn)在,讓我們使用create_architecture()函數(shù)創(chuàng)建我們的神經(jīng)網(wǎng)絡(luò)的體系結(jié)構(gòu)。
在下面的代碼片段中,外部For循環(huán)創(chuàng)建層,內(nèi)部For循環(huán)將指定數(shù)量的神經(jīng)元添加到該層。我們也隨機(jī)初始化神經(jīng)元的權(quán)值在0到1之間。
訓(xùn)練的例子:
我們將使用get_input()函數(shù)存儲訓(xùn)練示例:
得到輸出標(biāo)簽:
我們將使用get_desired_exports()函數(shù)存儲輸出標(biāo)簽
前向傳遞:
第i層第j個神經(jīng)元的激活與(i−1)第(i−1)層神經(jīng)元的激活關(guān)系為:
注意:σ是激活函數(shù)。這里輸出層使用sigmoid激活函數(shù),隱藏層使用Relu激活函數(shù)。
sigmoid函數(shù):
Relu函數(shù):
讓我們實現(xiàn)forward_prop()函數(shù)
反向傳遞:
反向傳播的目標(biāo)是反向傳播錯誤并更新權(quán)值以最小化錯誤。這里,我們將使用均方誤差函數(shù)來計算誤差。
權(quán)重(dw)和偏差(dbias)的變化是使用成本函數(shù)C對網(wǎng)絡(luò)中的權(quán)重和偏差的偏導(dǎo)數(shù)(∂C/ ∂weights和∂C/∂ bias)來計算的。
sigmoid函數(shù)的導(dǎo)數(shù):
relu函數(shù)的導(dǎo)數(shù):
反向傳播背后的四個基本方程:
讓我們在back_prop()函數(shù)中實現(xiàn)這些公式:
更新權(quán)重:
在每個epoch中,我們將使用update_weights()函數(shù)更新網(wǎng)絡(luò)權(quán)值和偏差
測試框架:
現(xiàn)在我們已經(jīng)準(zhǔn)備好了所有的部分,我們將驗證框架的工作情況。因此,讓我們創(chuàng)建一個4層的神經(jīng)網(wǎng)絡(luò),輸入層有2個神經(jīng)元,第一隱含層有4個神經(jīng)元,第二隱含層有4個神經(jīng)元,輸出層有1個神經(jīng)元。另外,隱藏的和輸出的神經(jīng)元會有偏差。
現(xiàn)在,我們可以針對不同的邏輯門訓(xùn)練這個神經(jīng)網(wǎng)絡(luò),比如XOR, OR等等。在下面的示例中,我們將實現(xiàn)XOR gate。
首先,提供所需的層數(shù)和每層神經(jīng)元的數(shù)目:
Enter the number of Layers in Neural Network: 4 Enter number of neurons in layer[1]: 2 Enter number of neurons in layer[2]: 4 Enter number of neurons in layer[3]: 4 Enter number of neurons in layer[4]: 1 |
神經(jīng)網(wǎng)絡(luò)體系結(jié)構(gòu)將根據(jù)給定的規(guī)范創(chuàng)建:
Created Layer: 1 Number of Neurons in Layer 1: 2 Neuron 1 in Layer 1 created Neuron 2 in Layer 1 created Created Layer: 2 Number of Neurons in Layer 2: 4 Neuron 1 in Layer 2 created Neuron 2 in Layer 2 created Neuron 3 in Layer 2 created Neuron 4 in Layer 2 created Created Layer: 3 Number of Neurons in Layer 3: 4 Neuron 1 in Layer 3 created Neuron 2 in Layer 3 created Neuron 3 in Layer 3 created Neuron 4 in Layer 3 created Created Layer: 4 Number of Neurons in Layer 4: 1 Neuron 1 in Layer 4 created |
所有的權(quán)值將在0和1之間隨機(jī)初始化。
接下來,提供學(xué)習(xí)率和輸入訓(xùn)練示例。下面是XOR邏輯門的真值表。
我們將以上4個輸入作為神經(jīng)網(wǎng)絡(luò)的訓(xùn)練實例。
Enter the learning rate (Usually 0.15): 0.15 Enter the number of training examples: 4 Enter the Inputs for training example[0]: 0 0 Enter the Inputs for training example[1]: 0 1 Enter the Inputs for training example[2]: 1 0 Enter the Inputs for training example[3]: 1 1 |
輸出標(biāo)簽:
Enter the Desired Outputs (Labels) for training example[0]: 0 |
Enter the Desired Outputs (Labels) for training example[1]: 1 Enter the Desired Outputs (Labels) for training example[2]: 1 Enter the Desired Outputs (Labels) for training example[3]: 0 |
我們的神經(jīng)網(wǎng)絡(luò)將在這4個訓(xùn)練實例上訓(xùn)練20000個epoch。現(xiàn)在,測試訓(xùn)練好的神經(jīng)網(wǎng)絡(luò):
Enter input to test: 0 0 Output: 0 Enter input to test: 0 1 Output: 1 Enter input to test: 1 0 Output: 1 Enter input to test: 1 1 Output: 0 |
總結(jié):
這是一個神經(jīng)網(wǎng)絡(luò)框架的基本實現(xiàn),目的是了解神經(jīng)網(wǎng)絡(luò)的基本原理和反向傳播算法。可以通過實現(xiàn)各種損失函數(shù)和提供保存/裝載重量來增強(qiáng)代碼。