28.04.2020 Views

Sách Deep Learning cơ bản

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

94 Chương 6. Backpropagation

b_ = np.zeros((layers[i+1], 1))

self.W.append(w_/layers[i])

self.b.append(b_)

# Tóm tắt mô hình neural network

def __repr__(self):

return "Neural network [{}]".format("-".join(str(l) for l in self.layers))

# Train mô hình với dữ liệu

def fit_partial(self, x, y):

A = [x]

# quá trình feedforward

out = A[-1]

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

out = sigmoid(np.dot(out, self.W[i]) + (self.b[i].T))

A.append(out)

# quá trình backpropagation

y = y.reshape(-1, 1)

dA = [-(y/A[-1] - (1-y)/(1-A[-1]))]

dW = []

db = []

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

dw_ = np.dot((A[i]).T, dA[-1] * sigmoid_derivative(A[i+1]))

db_ = (np.sum(dA[-1] * sigmoid_derivative(A[i+1]), 0)).reshape(-1,1)

dA_ = np.dot(dA[-1] * sigmoid_derivative(A[i+1]), self.W[i].T)

dW.append(dw_)

db.append(db_)

dA.append(dA_)

# Đảo ngược dW, db

dW = dW[::-1]

db = db[::-1]

# Gradient descent

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

self.W[i] = self.W[i] - self.alpha * dW[i]

self.b[i] = self.b[i] - self.alpha * db[i]

def fit(self, X, y, epochs=20, verbose=10):

for epoch in range(0, epochs):

self.fit_partial(X, y)

if epoch % verbose == 0:

loss = self.calculate_loss(X, y)

print("Epoch {}, loss {}".format(epoch, loss))

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

Saved successfully!

Ooh no, something went wrong!