28.04.2020 Views

Sách Deep Learning cơ bản

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

6.3 Python code 93

Thì ở bài này quá trình tính đạo hàm ngược lại

Hình 6.3: Quá trình feedforward

Hình 6.4: Quá trình backpropagation

Đấy là vì sao thuật toán được gọi là backpropagation (lan truyền ngược)

6.3 Python code

# Thêm thư viện

import numpy as np

import pandas as pd

# Hàm sigmoid

def sigmoid(x):

return 1/(1+np.exp(-x))

# Đạo hàm hàm sigmoid

def sigmoid_derivative(x):

return x*(1-x)

# Lớp neural network

class NeuralNetwork:

def __init__(self, layers, alpha=0.1):

# Mô hình layer ví dụ [2,2,1]

self.layers = layers

# Hệ số learning rate

self.alpha = alpha

# Tham số W, b

self.W = []

self.b = []

# Khởi tạo các tham số ở mỗi layer

for i in range(0, len(layers)-1):

w_ = np.random.randn(layers[i], layers[i+1])

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!