[Deep Learning from scratch] Implement backpropagation processing in neural network by error back propagation method

Introduction

This article is an easy-to-understand output of ** Deep Learning from scratch Chapter 6 Error back propagation method **. I was able to understand it myself in the humanities, so I hope you can read it comfortably. Also, I would be more than happy if you could refer to it when studying this book.

Implemented back propagation processing in neural network

In the previous article, I was able to implement all the parts needed to do the backpropagation method. Therefore, this time, I would like to implement the back propagation process in the neural network by actually using the part and performing the error back propagation method.

** 1, Parameter initialization / Layer creation **

from collections import OrderedDict
class LayerNet:
    def __init__(self, input_size, hiden_size, output_size, weight_init_std = 0.01):
        self.params = {}
        self.params['W1'] = weight_init_std * np.random.randn(input_size, hiden_size)
        self.params['b1'] = np.zeros(hiden_size)
        self.params['W2'] = weight_init_std * np.random.randn(hiden_size, output_size)
        self.params['b2'] = np.zeros(output_size)
    
        #Layer generation
        self.layers = OrderedDict()#Ordered dictionary
        self.layers['Affine1'] = Affine(self.params['W1'],self.params['b1']) #Create all Affine layers on the first layer of the middle layer
        self.layers['Relu1'] = Relu()#Create all Relu layers on the first layer of the middle layer
        self.layers['Affine2'] = Affine(self.params['W2'], self.params['b2']) #Create all Affine layers in the second layer of the middle layer
        self.layers['Relu2'] = Relu() #Create all Relu layers for the second layer of the middle layer
        self.lastLayer = SoftmaxWithLoss() #Layer creation from output layer to loss function
    

Please refer to the parameter initialization as it is the same as for the numerical differentiation neural network.

In the next layer generation part, we first create an ordered dictionary ** OrderDict ** to store the layers and store it in an instance variable.

Next, put the layers in the created OrderDict in order from the left side of the neural network. Layers are managed in order by putting them in OrderDict. By doing so, you can easily perform forward propagation processing by using the for statement to extract layers one by one, and reverse propagation processing by using the for statement in the reverse order.

** 2, implement gradient expression using predict method, loss method, accuracy method, layer **

from collections import OrderedDict
class LayerNet:
    def __init__(self, input_size, hiden_size, output_size, weight_init_std = 0.01):
        self.params = {}
        self.params['W1'] = weight_init_std * np.random.randn(input_size, hiden_size)
        self.params['b1'] = np.zeros(hiden_size)
        self.params['W2'] = weight_init_std * np.random.randn(hiden_size, output_size)
        self.params['b2'] = np.zeros(output_size)

        #Layer generation
        self.layers = OrderedDict()#Ordered dictionary
        self.layers['Affine1'] = Affine(self.params['W1'],self.params['b1']) #Create all Affine layers on the first layer of the middle layer
        self.layers['Relu1'] = Relu()#Create all Relu layers on the first layer of the middle layer
        self.layers['Affine2'] = Affine(self.params['W2'], self.params['b2']) #Create all Affine layers in the second layer of the middle layer
        self.layers['Relu2'] = Relu() #Create all Relu layers for the second layer of the middle layer
        self.lastLayer = SoftmaxWithLoss() #Layer creation from output layer to loss function

    def predict(self, x): #Performs forward propagation processing to the intermediate layer of the neural network
        for layer in self.layers.values():
            x = layer.forward(x) #Process the ordered layers in order with a for statement
        
        return x
    
    def loss(self, x, t): #Performs forward propagation processing from the neural network to the loss function
        y = self.predict(x) #Produce forecast data
        
        return self.lastLayer.forward(y,t) #Performs forward propagation processing from the output layer to the loss function
    
    def accuracy(self,x, t): #Give the correct answer rate
        y = self.predict(x)
        y = np.argmax(y, axis=1) #Label the correct answer forecast from the forecast data
        if t.ndim != 1 : t = np.argmax(t, axis=1) #Correct answer data is one_If it is hot, change it to a one-dimensional array
        
        accuracy = np.sum(y == t) /float(x.shape[0]) #Give the correct answer rate
        
        return accuracy
    
    def gradient(self, x, t): #Gradient equation using layer backpropagation
        #forward
        self.loss(x, t)
        
        #backward
        dout = 1
        dout = self.lastLayer.backward(dout) #Backpropagation of layers from output layer to loss function
        
        layers = list(self.layers.values())
        layers.reverse() #Reverse the order
        
        for layer in layers: #Perform backpropagation processing from the layer to the right of the intermediate layer
            dout = layer.backward(dout)
        
        #Gradient recovery
        grads = {}
        grads['W1'] = self.layers['Affine1'].dW
        grads['b1'] = self.layers['Affine1'].db
        grads['W2'] = self.layers['Affine2'].dW
        grads['b2'] = self.layers['Affine2'].db
        
        return grads
    

The predict method performs forward propagation processing of the neural network. As I wrote a little earlier, forward propagation processing can be performed by using the for statement in OrderDict, performing the forward method in order, and returning the output value with return.

Since the loss method performs the predict method plus the activation function and loss function of the output layer, it can be implemented by using the result for the forward of the lastLayer after the predict method.

The accuracy is the same as for numerical differentiation, so please refer to that.

Gradient formulas using layers can be implemented by reverse propagation processing of the neural network, first reverse the order of OrderDict with the reverse method, then perform backward with the for statement in order, and return the result with return. can.

Recommended Posts

[Deep Learning from scratch] Implement backpropagation processing in neural network by error back propagation method
[Deep Learning from scratch] About the layers required to implement backpropagation processing in a neural network
[Deep Learning from scratch] Speeding up neural networks I explained back propagation processing
Lua version Deep Learning from scratch Part 6 [Neural network inference processing]
Deep learning from scratch (forward propagation edition)
"Deep Learning from scratch" in Haskell (unfinished)
Deep learning / error back propagation of sigmoid function
[Python] [Natural language processing] I tried Deep Learning ❷ made from scratch in Japanese ①
[Deep Learning from scratch] Initial value of neural network weight using sigmoid function
Error back propagation method (back propagation)
Deep Learning from scratch
Implement Neural Network from 1
[Deep Learning] Execute SONY neural network console from CUI
[Deep Learning from scratch] Initial value of neural network weight when using Relu function
[Deep Learning from scratch] Implementation of Momentum method and AdaGrad method
Deep Learning from scratch 1-3 chapters
Try to build a deep learning / neural network with scratch
An amateur stumbled in Deep Learning from scratch Note: Chapter 1
Chapter 3 Neural Network Cut out only the good points of deep learning made from scratch
Non-information graduate student studied machine learning from scratch # 2: Neural network
An amateur stumbled in Deep Learning from scratch Note: Chapter 3
An amateur stumbled in Deep Learning from scratch Note: Chapter 7
An amateur stumbled in Deep Learning from scratch Note: Chapter 5
An amateur stumbled in Deep Learning from scratch Note: Chapter 4
An amateur stumbled in Deep Learning from scratch Note: Chapter 2
I tried to implement Perceptron Part 1 [Deep Learning from scratch]
Chapter 7 [Error back propagation method] P275 ~ (Middle) [Learn by moving with Python! New machine learning textbook]
[Deep Learning from scratch] Main parameter update methods for neural networks
Why ModuleNotFoundError: No module named'dataset.mnist' appears in "Deep Learning from scratch".
Deep learning from scratch (cost calculation)
Deep Learning memos made from scratch
Deep learning / matrix product error backpropagation
[Deep Learning from scratch] I tried to implement sigmoid layer and Relu layer.
Python vs Ruby "Deep Learning from scratch" Chapter 2 Logic circuit by Perceptron
[Learning memo] Deep Learning made from scratch [Chapter 7]
Deep learning / Deep learning from scratch 2-Try moving GRU
Deep learning / Deep learning made from scratch Chapter 6 Memo
[Learning memo] Deep Learning made from scratch [Chapter 5]
[Learning memo] Deep Learning made from scratch [Chapter 6]
Deep learning / Deep learning made from scratch Chapter 7 Memo
[Windows 10] "Deep Learning from scratch" environment construction
[Deep Learning from scratch] About hyperparameter optimization
"Deep Learning from scratch" Self-study memo (Part 12) Deep learning
[Learning memo] Deep Learning made from scratch [~ Chapter 4]
Lua version Deep Learning from scratch Part 5.5 [Making pkl files available in Lua Torch]
[For beginners] After all, what is written in Deep Learning made from scratch?
"Deep Learning from scratch" self-study memo (unreadable glossary)
[Python / Machine Learning] Why Deep Learning # 1 Perceptron Neural Network
"Deep Learning from scratch" Self-study memo (9) MultiLayerNet class
Deep Learning from scratch ① Chapter 6 "Techniques related to learning"
Good book "Deep Learning from scratch" on GitHub
Deep Learning from scratch Chapter 2 Perceptron (reading memo)
[Learning memo] Deep Learning from scratch ~ Implementation of Dropout ~
Python: Deep Learning in Natural Language Processing: Basics
Python vs Ruby "Deep Learning from scratch" Summary
"Deep Learning from scratch" Self-study memo (10) MultiLayerNet class
"Deep Learning from scratch" Self-study memo (No. 11) CNN