python_deeplearning02_使用python制作神經(jīng)網(wǎng)絡(luò)

20180421 qzd

ch02 - 使用python制作神經(jīng)網(wǎng)絡(luò)


  1. 構(gòu)建框架
  • 初始化函數(shù) -- 設(shè)定輸入層節(jié)點(diǎn)、隱藏層節(jié)點(diǎn)和輸出層節(jié)點(diǎn)的數(shù)量。
  • 訓(xùn)練 -- 學(xué)習(xí)給定訓(xùn)練集樣本后,優(yōu)化權(quán)重(權(quán)重--網(wǎng)絡(luò)的核心)。
  • 查詢(xún) -- 給定輸入,從輸出節(jié)點(diǎn)給出答案。
#neural network class definition
class neuralNetwork:
    
    #initialise the neural network
    def __init__():
        pass
    
    #train the neural network
    def train():
        pass
    
    #query the neural network
    def query():
        pass
  1. 初始化網(wǎng)絡(luò)
 def __init__(self,inputnodes,hiddennodes,outputnodes,learningrate ):
  1. 權(quán)重 -- 網(wǎng)絡(luò)的核心
  • 在輸入層與隱藏層之間的鏈接權(quán)重矩陣W input_hidden,大小為hidden_nodes乘以input_nodes。
  • 在隱藏層和輸出層之間的鏈接權(quán)重矩陣W hidden_output,大小為hidden_nodes乘以output_nodes。
  • 初始化權(quán)重
#self.wih = (np.random.rand(self.hnodes,self.inodes) - 0.5)
#self.who = (np.random.rand(self.onodes,self.hnodes) - 0.5)
self.wih = np.random.normal(0.0,pow(self.hnodes,-0.5),(self.hnodes,self.inodes))
self.who = np.random.normal(0.0,pow(self.onodes,-0.5),(self.onodes,self.hnodes))
  1. 查詢(xún)網(wǎng)絡(luò)
def query(self,inputs_list):
  1. 訓(xùn)練網(wǎng)絡(luò)(權(quán)重部分)
  • 第一部分,針對(duì)給定的訓(xùn)練樣本計(jì)算輸出。這與我們剛剛在query()函數(shù)上所做的沒(méi)什么區(qū)別。
  • 第二部分,將計(jì)算得到的輸出與所需輸出對(duì)比,使用差值來(lái)指導(dǎo)網(wǎng)絡(luò)權(quán)重的更新。
  1. 完整的神經(jīng)網(wǎng)絡(luò)代碼
import numpy as np
#scipy.special for the sigmoid function expit()
import scipy.special

#neural network class definition
class neuralNetwork:
    
    #initialise the neural network
    def __init__(self,inputnodes,hiddennodes,outputnodes,learningrate ):
        #set number of nodes in each input, hidden, output layer
        self.inodes = inputnodes
        self.hnodes = hiddennodes
        self.onodes = outputnodes
        
        #link weight matrices, wih and who
        #weights inside the arrays are w_i_j, where link is from node i to node j in the next layer
        # w11 w21
        # w12 w22 etc
        #self.wih = (np.random.rand(self.hnodes,self.inodes) - 0.5)
        #self.who = (np.random.rand(self.onodes,self.hnodes) - 0.5)
        self.wih = np.random.normal(0.0,pow(self.hnodes,-0.5),(self.hnodes,self.inodes))
        self.who = np.random.normal(0.0,pow(self.onodes,-0.5),(self.onodes,self.hnodes))
        
        #learning rate
        self.lr = learningrate
        
        #activation function is the sigmoid function
        self.activation_function = lambda x:scipy.special.expit(x)
        
        pass
    
    #train the neural network
    def train(self,inputs_list,targets_list):
        #convert inputs list to 2d array
        inputs = np.array(inputs_list,ndmin=2).T
        targets = np.array(targets_list,ndmin=2).T
        
        #calculate signals into hidden layer
        hidden_inputs = np.dot(self.wih,inputs)
        #calculate the signals emerging from hidden layer
        hidden_outputs = self.activation_function(hidden_inputs)
        
        #calculate signals into final output layer
        final_inputs = np.dot(self.who,hidden_outputs)
        #calculate the signals emerging from final output layer
        final_outputs = self.activation_function(final_inputs)
        
        #output layer error is the (target - actual)
        output_errors = targets - final_outputs
        #hidden layer error is the output_errors, split by weights, recombined at hidden nodes
        hidden_errors = np.dot(self.who.T,output_errors)
        
        #update the weights for the links between thehidden and output layers
        self.who += self.lr *np.dot((output_errors * final_outputs*(1.0-final_outputs)), np.transpose(hidden_outputs))
        #update the weights for the links between the input and hidden layers
        self.wih += self.lr *np.dot((hidden_errors * hidden_outputs*(1.0 - hidden_outputs)),np.transpose(inputs))
        
        pass
    
    #query the neural network
    def query(self,inputs_list):
        #convert inputs list to 2d array
        inputs = np.array(inputs_list,ndmin=2).T
        
        #calculate signals into hidden layer
        hidden_inputs = np.dot(self.wih,inputs)
        #calculate the signals emerging from hidden layer
        hidden_outputs = self.activation_function(hidden_inputs)
        
        #calculate signals into final output layer
        final_inputs = np.dot(self.who,hidden_outputs)
        #calculate the signals emerging from final output layer
        final_outputs = self.activation_function(final_inputs)
        
        return final_outputs
#number of input, hidden and output nodes
input_nodes = 3
hidden_nodes = 3
output_nodes = 3

#learning rate is 0.3
learning_rate = 0.3

#create instance of neural network
n = neuralNetwork(input_nodes,hidden_nodes,output_nodes,learning_rate)

n.query([1.0,0.5,-1.5])
  • 輸出結(jié)果

array([[ 0.46356062],
[ 0.58538226],
[ 0.5905356 ]])

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容