Python & Machine Learning Study Memo ④: Machine Learning by Backpropagation


① ② ③ Continued

--Reference materials: Udemy Everyone's AI course Artificial intelligence and machine learning learned from scratch with Python --Issue setting: Given latitude and longitude, create a program to determine whether it belongs to Tokyo or Kanagawa. --Judgment method: Judgment is made by a neural network with 2 input layers, 2 intermediate layers, and 1 output layer.

Machine learning with backpropagation

The weight of neurons in the output layer is adjusted based on the error between the output and the correct answer. Moreover, the weight of the neuron in the intermediate layer is adjusted based on the weight of the neuron in the output layer.

Intermediate layer-Adjusting the weight of the output layer

Intermediate layer-The weight adjustment of the output layer can be obtained by the following formula.

\delta_{mo} = (Output value-Correct answer value) \derivatives of the times output\\
Correction amount= \delta_{mo} \times middle layer value\times learning coefficient

Differentiation of sigmoid function

Regarding the "derivative of output" in the above equation, the derivative of the sigmoid function $ f (x) = \ frac {1} {1 + e ^ x} $ used in the activation function of the neural network this time is as follows. can get.

f(x)' = f(x)\cdot(1-f(x))


Source code

class NeuralNetwork:
    #Input weight
    w_im = [[1.0, 1.0], [1.0, 1.0], [1.0, 1.0]]  # [[i1-m1, i1-m2], [i2-m1, i2-m2], [bias1-m1, bias1-m2]]
    w_mo = [1.0, 1.0, 1.0]  # [m1-o, m2-o, bias2-0]
    #Declaration of each layer
    input_layer = [0.0, 0.0, 1.0] # i1, i2, bias1
    middle_layer = [Neuron(), Neuron(), 1.0] # m1, m2, bias2
    ouput_layer = Neuron() # o

    def learn(self, input_data):

        #Output value
        output_data = self.commit([input_data[0], input_data[1]])
        #Correct answer value
        correct_value = input_data[2]
        #Learning coefficient
        k = 0.3

        #Output layer → Intermediate layer
        delta_w_mo = (correct_value - output_data) * output_data * (1.0 - output_data)
        old_w_mo = list(self.w_mo)
        self.w_mo[0] += self.middle_layer[0].output * delta_w_mo * k
        self.w_mo[1] += self.middle_layer[1].output * delta_w_mo * k
        self.w_mo[2] += self.middle_layer[2] * delta_w_mo * k

Input layer-Adjusting intermediate layer weights

The weight adjustment of the input layer-intermediate layer can be obtained by the following equation. By adjusting the weight of the front neuron based on the adjustment result of the rear neuron, it is possible to adjust any number of layers.

\delta_{im} = \delta_{mo} \times Intermediate output weight\times Differentiation of intermediate layer output\\
Correction amount= \delta_{im} \times Input layer value\times learning coefficient

Source code

class NeuralNetwork:
        #Middle layer → Input layer
        delta_w_im = [
            delta_w_mo * old_w_mo[0] * self.middle_layer[0].output * (1.0 - self.middle_layer[0].output),
            delta_w_mo * old_w_mo[1] * self.middle_layer[1].output * (1.0 - self.middle_layer[1].output)
        self.w_im[0][0] += self.input_layer[0] * delta_w_im[0] * k
        self.w_im[0][1] += self.input_layer[0] * delta_w_im[1] * k
        self.w_im[1][0] += self.input_layer[1] * delta_w_im[0] * k
        self.w_im[1][1] += self.input_layer[1] * delta_w_im[1] * k
        self.w_im[2][0] += self.input_layer[2] * delta_w_im[0] * k
        self.w_im[2][1] += self.input_layer[2] * delta_w_im[1] * k

Learning results

Prepare the following as training data learning.png

If you throw the following data into the neural network trained above ...

data_to_commit = [[34.6, 138.0], [34.6, 138.18], [35.4, 138.0], [34.98, 138.1], [35.0, 138.25], [35.4, 137.6], [34.98, 137.52], [34.5, 138.5], [35.4, 138.1]]

You can see that it is possible to determine whether it is classified as Tokyo or Kanagawa.


Recommended Posts

Python & Machine Learning Study Memo ④: Machine Learning by Backpropagation
Python & Machine Learning Study Memo: Environment Preparation
Python & Machine Learning Study Memo ③: Neural Network
Python & Machine Learning Study Memo ⑥: Number Recognition
Python & Machine Learning Study Memo ⑤: Classification of irises
Python & Machine Learning Study Memo ②: Introduction of Library
Python & Machine Learning Study Memo ⑦: Stock Price Forecast
Python learning memo for machine learning by Chainer Chapters 1 and 2
Machine learning summary by Python beginners
Interval scheduling learning memo ~ by python ~
"Scraping & machine learning with Python" Learning memo
[Memo] Machine learning
[Learning memo] Basics of class by python
Python learning memo for machine learning by Chainer Chapter 8 Introduction to Numpy
Python learning memo for machine learning by Chainer Chapter 10 Introduction to Cupy
Python learning memo for machine learning by Chainer Chapter 9 Introduction to scikit-learn
Python class (Python learning memo ⑦)
Python module (Python learning memo ④)
Machine learning course memo
Python learning memo for machine learning by Chainer Chapter 13 Neural network training ~ Chainer completed
Python learning memo for machine learning by Chainer Chapter 13 Basics of neural networks
Python learning memo for machine learning by Chainer until the end of Chapter 2
Python exception handling (Python learning memo ⑥)
Machine learning with Python! Preparation
Machine Learning Study Resource Notepad
Practical machine learning system memo
Beginning with Python machine learning
4 [/] Four Arithmetic by Machine Learning
Memo for building a machine learning environment using Python
Build a python machine learning study environment on macOS sierra
Python control syntax, functions (Python learning memo ②)
Machine learning with python (1) Overall classification
Input / output with Python (Python learning memo ⑤)
<For beginners> python library <For machine learning>
Python: Preprocessing in Machine Learning: Overview
Python memo
python memo
Python memo
python memo
python learning
Python memo
A memorandum of scraping & machine learning [development technique] by Python (Chapter 5)
Python memo
Python memo
Notes on PyQ machine learning python grammar
Python numbers, strings, list types (Python learning memo ①)
Making Sandwichman's Tale by Machine Learning ver4
Amplify images for machine learning with python
Machine learning with python (2) Simple regression analysis
[python] Frequently used techniques in machine learning
Why Python is chosen for machine learning
"Python Machine Learning Programming" Summary Note (Jupyter)
Four arithmetic operations by machine learning 6 [Commercial]
Python: Preprocessing in machine learning: Data acquisition
Python data structure and operation (Python learning memo ③)
[Python] When an amateur starts machine learning
[Python] Web application design for machine learning
[Online] Everyone's Python study session # 55 Rough memo
Python and machine learning environment construction (macOS)
Python standard library: second half (Python learning memo ⑨)
An introduction to Python for machine learning