微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用numpy数组存储权重和其他数据时,如何在用户定义的Neuro_net类中实现反向传播的导数?

如何解决使用numpy数组存储权重和其他数据时,如何在用户定义的Neuro_net类中实现反向传播的导数?

这是我编写代码的方式。我知道如何使用链式规则进行反向传播,并且可以对每个层进行数学硬编码,但是我想对其进行泛化,以便可以对任何形状的神经网络进行反向传播,我不知道如何在我推导和访问权重已将我的数据保存在数组中。

from scipy.misc import derivative
import numpy as np
import random
import copy

#Class deFinitions ------------------------------    
    
class Neural_net:
    
    def __init__(self,nr_inp,nr_out,nr_hl,bias = None):
        
        #Bias is optional and depends on how many parameters the constructor receives
        if bias != None:
            
            self.bias = bias
            
        else:
            
            self.bias = np.empty(nr_hl+2,dtype = int)
        
        
        #Declaring neural network shape
        self.nr_inp = nr_inp #number of inputs
        self.nr_out = nr_out #number of outputs
        self.nr_hl = nr_hl #number of hidden layers
        
        #Declaring array shapes
        self.nodes = np.ndarray(shape=(self.nr_hl+2,1)) #how many nodes each hidden layer has
        self.inputs = np.ndarray(shape = (nr_inp,1)) #array with input values
        self.outputs = np.empty(self.nr_hl+1,dtype = object)#array with arrays that hold output values for each layer
        self.weights = np.empty(self.nr_hl+1,dtype=object) #arrays within another array which hold all weight values
        
        
        
        self.ctr = 1 #counter variable
        
        
        
        #Declaring shape of array which holds output values (this shape is kNown before kNowing the number of nodes in hidden layers)
        
        self.outputs[nr_hl] = np.ndarray(shape=(self.nr_out,1))
        
        #Inserting the number of input and output nodes in the "nodes" array
        self.nodes[0] = self.nr_inp 
        self.nodes[nr_hl+1] = self.nr_out
        
        
    def create_hidden_layer(self,nr_nodes): #Create hidden layers
        
        #Inserting number of hidden layer nodes into the nodes array
        self.nodes[self.ctr] = nr_nodes
        
        #Declaring the shape of the array which holdes the outputs of the certain hidden layer
        self.outputs[self.ctr] = np.ndarray(shape=(int(self.nodes[self.ctr]),1))
        
        self.ctr = self.ctr +1
        self.nodes[self.ctr] = self.nr_out
        
        
    def initalize_weights(self): #Initialize weights
        
        
        for x in range (0,self.nr_hl+1):
            
            #Declaring shape of weight array for layer x
            self.weights[x] = np.ndarray(shape=(int(self.nodes[x+1]),int(self.nodes[x])))
            
            #Initializing weight array for layer x
            self.weights[x]= np.random.uniform(0,1,size =(int(self.nodes[x+1]),int(self.nodes[x])))
            
    def activation_function(self,x,b):
        
        return np.tanh(x+b)
            
       
    def forward_pass(self,inputs,target): #Forward pass into the neural network; "target" represents the target values of the outputs
        
        self.inputs = inputs
        self.target = target
        
        for x in range(0,self.nr_hl+1):
        
            
        
            if x == 0:
                
                #Declaring shape of the array which holds outputs of the first hidden layer
                self.outputs[x] = np.ndarray(shape=(int(self.nodes[1]),1))
                
                #Calculating the outputs
                self.outputs[x] = np.dot(self.weights[x],self.inputs)
                
            else:
                
                #Calculating outputs of layer x
                self.outputs[x] = np.dot(self.weights[x],self.outputs[x-1])
               
            
        #copying the output array before it has been put through the activation function    
        self.net_outputs = copy.deepcopy(self.outputs)
        
        for x in range(0,self.nr_hl+1):
            
            for y in range(len(self.outputs[x])):
                
                    #Modifying outputs by using the activation function
                    self.outputs[x][y] = self.activation_function(float(self.outputs[x][y]),self.bias[x])
                    
            
        
    def calculate_error(self):
            
        self.error = np.empty(self.nr_out,dtype = float)
        
        for x in range(self.nr_out):
                
            self.error[x] = float((self.target[x]-self.outputs[self.nr_hl][x])**2)
            
            
    
        
            
            
            
            
            
        


版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。