Back-propagation Algorithm

CrossKnight
3 min readAug 26, 2020

--

Backpropagation

หรือในชื่อ backward propagation of errors คืออัลกอริทึมที่ใช้ในการคำนวณหา Error ของ Output ที่ได้จาก Neural Network และเพื่อให้ง่ายต่อการเข้าใจ จึงหยิบ Perceptron (Neural Network อย่างง่าย) เป็นตัวอย่างการประกอบการอธิบาย

Perceptron Neural Network

Forward Propagation

Input Layer

จากภาพด้านบน จะพบว่ามี Node สีฟ้าอยู่ 2 Node นั้นคือ Input Data(Xi)

Xi, i ∈ 1, 2

Output Layer

Node สีส้ม และ Node สีน้ำตาล โดย Node สีส้ม จะมีการนำ Weight ที่เกิดจากการสุ่มในช่วงเริ่มต้นของการ Train คูณกับ Input Data แล้วนำผลลัพธ์จากการคูณมาบวกกับ Bias(B) ซึ่งเกิดจากการสุ่มในช่วงเริ่มต้นเช่นกัน

Z = W · X + B

ซึ่งในการ Implement จะแทน Weight(W) ด้วย Matrix ขนาด mxn โดยที่ m คือจำนวน Output Node (ในที่นี้มี 1 Node) และ n คือ จำนวน Input Node (ในที่นี้มี 2 Node) จึงสามารถคำนวณ z ได้ดังสมการนี้

ดังนั้นหากปรับ Error At w2 = (-0.222)(0.242)(0.1) = -0.0053724

Learning Rate เท่ากับ 0.5 จะปรับค่า w2 ได้ดังนี้

Update w2 = w2 - Learning_Rate*Error_at_w1
= 0.5 -(0.5)(-0.0053724)
= 0.5026862

Implement with NumPy

Architecture of a 2-layer Neural Network

นิยาม Neural Network Model ที่ไม่ใส่ Bias

class NeuralNetwork:
def __init__(self, x, y):
self.input = x
self.weights1 = np.random.rand(self.input.shape[1],4)
self.weights2 = np.random.rand(4,1)
self.y = y
self.output = np.zeros(y.shape)

กำหนดค่า X และ y

import numpy as np

X = np.array([[0,0,1],
[0,1,1],
[1,0,1],
[1,1,1]])

y = np.array([[0],[1],[1],[0]])
X.shape, y.shape

สร้างตัวแปรที่จะทำ Neural Network

nn1 = NeuralNetwork(X,y)

เริ่มทำการ Train Neural Network Class

import numpy as np

def sigmoid(x):
return 1.0/(1+ np.exp(-x))

def sigmoid_derivative(x):
return x * (1.0 - x)

class NeuralNetwork:
def __init__(self, x, y, l):
self.input = x
self.weights1 = np.random.rand(self.input.shape[1],4)
self.weights2 = np.random.rand(4,1)
self.y = y
self.output = np.zeros(self.y.shape)
self.learning_rate = l

def loss(self):
return sum((self.y - self.output)**2)

def feedforward(self):
self.layer1 = sigmoid(np.dot(self.input, self.weights1))
self.output = sigmoid(np.dot(self.layer1, self.weights2))

def backprop(self):
d_weights2 = np.dot(self.layer1.T, (2*(self.y - self.output) * sigmoid_derivative(self.output)))
d_weights1 = np.dot(self.input.T, (np.dot(2*(self.y - self.output) * sigmoid_derivative(self.output), self.weights2.T) * sigmoid_derivative(self.layer1)))

self.weights1 += self.learning_rate*d_weights1
self.weights2 += self.learning_rate*d_weights2

Train Model

จะทำการ Train Model ด้วย learning_rate ที่มีค่าตั้งแต่ 0.1 ถึง 1.0

learning_rate = 0.1
nn = [None]*10for i in range(10) :
nn[i]= NeuralNetwork(X,y,learning_rate)
learning_rate = learning_rate+0.1loss=[]for i in range(10) :
dum = []
for j in range(1000):
nn[i].feedforward()
nn[i].backprop()
dum.append(nn[i].loss())
loss.append(dum)

Plot Loss

import pandas as pd
from matplotlib import pyplot as plt
%matplotlib inline
df = []
for i in range(10):
df.append(pd.DataFrame(loss[i], columns=['loss']))
df[1].head()

และ Plot Graph เพื่อแสดงผล

from random import randint
data = []
for i in range(10):
data.append(go.Scatter(y=df[i]['loss'],
mode="lines", line=dict(
width=2,
color='#%06X' % randint(0, 0xFFFFFF)),
name="loss " + str(i))
)
layout1 = go.Layout(title='Loss',
xaxis=dict(title='epochs'),
yaxis=dict(title=''))
fig1 = go.Figure(data, layout=layout1)
fig1.show()

Reference

https://blog.pjjop.org/basic-machine-learning

บทความนี้เป็นส่วนหนึ่งของรายวิชา AI จัดทำขึ้นเพื่อทดลองทำ Lab ซึ่งหากมีข้อผิดพลาดใดๆ ขออภัยไว้ ณ ที่นี้

--

--

No responses yet