building your first deep neural network -introduction to back-propagation (Part 1)

Duy Anh Nguyen
10 min readFeb 24, 2019

The streetlight problem

This toy problem considers how a network learns entire datasets

Consider yourself approaching a street corner in a foreign country. As you approach, you look up and realize that the street light is unfamiliar. How can you know when it’s safe to cross the street?

hãy thử tưởng tượng bạn muốn sang đường ở một quốc gia xa lạ. bạn cảm thấy rằng đèn giao thông không hề quen thuộc với bạn. làm sao bạn biết được cách nào là cách sang đường một cách an toàn ?

street light in a strange country

You can know when it’s safe to cross the street by interpreting the streetlight. But in this case, you don’t know how to interpret it. Which light combinations indicate when it’s time to walk? Which indicate when it’s time to stop? To solve this problem, you might sit at the street corner for a few minutes observing the correlation between each light combination and whether people around you choose to walk or stop. You take a seat and record the following pattern:

bạn có thể biết được cách an toàn để sang đường bằng cách suy luận về tín hiệu đèn giao thông. nhưng trong trường hợp này bạn không hề biết làm như thế nào để suy luận về nó. Tổ hợp đèn nào sẽ cho bạn biết thời gian bạn có thể sang đường đây? và khi nào là thời gian bạn phải dừng lại? để giải quyết vấn đề này có thể bạn sẽ ngồi ở góc đường một vài phút để quan sát sự tương quan giữa mỗi một tổ hợp đèn tín hiệu và liệu trong trường hợp đó những người xung quanh bạn chọn đi hay dừng. bạn ngồi xuống và ghi lại những dữ liệu mẫu sau:

OK, nobody walked at the first light. At this point you’re thinking, “Wow, this pattern could be anything. The left light or the right light could be correlated with stopping, or the central light could be correlated with walking.” There’s no way to know. Let’s take another datapoint:

OK, không ai đi ở tổ hợp đèn này. lúc này bạn đang suy nghĩ “wow, mẫu này có thể là bất cứ điều gì”. Đèn bên trái và đèn bên phải có thể tương ứng với dừng, hoặc đèn ở giữa có thể tương ứng với đi. chưa có gì là chắc chắn. hãy thử lấy một mẫu dữ liệu khác

Now you’re getting somewhere. Only the middle light changed this time, and you got the opposite pattern. The working hypothesis is that the middle light indicates when people feel safe to walk. Over the next few minutes, you record the following six light patterns, noting when people walk or stop. Do you notice a pattern overall?

bây giờ bạn đã có thêm một chút thông tin. chỉ khi đèn ở giữa thay đổi vào lúc này và bạn có một mẫu ngược lại với trước đó. một giả thuyết đó là đèn ở giữa cho biết khi nào mọi người có thể qua đường. một vài phút sau đó, bạn ghi nhận thêm sáu mẫu dự liệu nữa, ghi chú khi đó mọi người qua đường hay dừng lại. bạn có nhận ra được quy luật không?

As hypothesized, there is a perfect correlation between the middle (crisscross) light and whether it’s safe to walk. You learned this pattern by observing all the individual data points and searching for correlation. This is what you’re going to train a neural network to do.

Giả thuyết là có một sự tương quan tuyệt đối giữa đèn ở giữa và khi nào bạn có thể sang đường. Bạn học được quy tắc này bằng cách quan sát tất cả những điểm dữ liệu riêng lẻ và tìm sự tương quan. Đây chính là thứ mà bạn sẽ huấn luyện mạng neuron để làm

Preparing the data because Neural networks don’t read streetlights.

bạn sẽ phải chuẩn bị dữ liệu vì mạng neuron sẽ không đọc đèn tín hiệu đâu

You do indeed have two datasets. On the one hand, you have six streetlight states. On the other hand, you have six observations of whether people walked. These are the two datasets. You can train the neural network to convert from the dataset you know to the dataset that you want to know. In this particular real-world example, you know the state of the streetlight at any given time, and you want to know whether it’s safe to cross the street.

bạn đang có 2 bộ dữ liệu. một bộ dữ liệu về 6 trạng thái đền và một bộ dữ liệu về 6 lần quan sát về việc liệu người đi bộ có qua đường. bạn có thể huấn luyện mạng neuron để chuyển đổi từ bộ dữ liệu bạn biết đến bộ dữ liệu bạn muốn biết. ví như trong ví dụ thực tế này, bạn biết về trạng thái của đèn giao thông tại các thời điểm khác nhau, và bạn muốn biết liệu rằng bạn có thể sang đường được hay không

To prepare this data for the neural network, you need to first split it into these two groups (what you know and what you want to know)

Để chuẩn bị dữ liệu cho mạng neuron, điều đầu tiên bạn cần làm là chia bộ dữ liệu này thành 2 nhóm (nhóm bạn biết và nhóm bạn muốn biết)

Matrices and the matrix relationship Translate the streetlight into math

Math doesn’t understand streetlights.You want to teach a neural network to translate a streetlight pattern into the correct stop/walk pattern. What you really want to do is mimic the pattern of the streetlight in the form of numbers. Let me show you what I mean.

Toán học không hề hiểu đèn giao thông. bạn muốn dạy mạng neuron để chuyển những mẫu đèn hiệu đến mẫu đúng về việc đi hay dừng. Điều thật sự bạn muốn đó là mô phỏng mẫu tín hiệu đèn giao thông ở dạng số. Để tôi cho bạn thấy việc này có ý nghĩa như thế nào

Notice that the pattern of numbers shown here mimics the pattern from the streetlights in the form of 1s and 0s. Each light gets a column (three columns total, because there are three lights). Notice also that there are six rows representing the six different observed streetlights. This structure of 1s and 0s is called a matrix. This relationship between the rows and columns is common in matrices, especially matrices of data (like the streetlights). In data matrices, it’s convention to give each recorded example a single row. It’s also convention to give each thing being recorded a single column. This makes the matrix easy to read. So, a column contains every state in which a thing was recorded. In this case, a column contains every on/off state recorded for a particular light. Each row contains the simultaneous state of every light at a particular moment in time. Again, this is common

lưu ý rằng những mẫu số ở đây cho thấy sự mô phỏng của mẫu đèn tín hiệu dưới dạng những số 0 và 1. mỗi một đèn tương ứng với 1 cột (3 cột tất cả bởi vì có 3 loại đèn). chú ý rằng có 6 cột thể hiện 6 sự quan sát tổ hợp đèn. cấu trúc những số 0 và 1 này gọi là ma trận. Mối liên hệ giữa dòng và cột là rất phổ biến trong ma trận, đặc biệt là ma trận dữ liệu. Trong ma trận dữ liệu chúng ta thường quy ước lấy 1 dòng cho 1 sự quan sát về dữ liệu. và lấy 1 cột để biểu diễn cho một đặc tính của dữ liệu. Điều này giúp chúng ta hiểu về ma trận dễ dàng hơn. Trong trường hợp này, mỗi một cột sẽ ứng với mỗi trạng thái của cho một loại đèn. mỗi một dòng chứ trạng thái tuần tự của mỗi 1 đèn tại 1 thời điểm trong thời gian

Good data matrices perfectly mimic the outside world

The data matrix doesn’t have to be all 1s and 0s. What if the streetlights were on dimmers and turned on and off at varying degrees of intensity? Perhaps the streetlight matrix would look more like this:

Ma trận dữ liệu không nhất thiết phải chứa toàn là 0 và 1. Nếu lỡ như đèn tín hiệu giao thông bị mờ dần và bật hoặc tắt ở nhiều cường độ. Lúc đó ma trân dữ liệu sẽ có dạng như sau:

Matrix A is perfectly valid. It’s mimicking the patterns that exist in the real world (streetlight), so you can ask the computer to interpret them. Would the following matrix still be valid?

Ma trận A hoàn toàn hợp lệ. Nó mô phỏng những mẫu đèn giao thông trong điều kiện thực tế, vì vậy bạn có thể yêu cầu máy tình đọc hiểu chúng. Vậy ma trận sau đây có hợp lệ hay không?

Matrix (B) is valid. It adequately captures the relationships between various training examples (rows) and lights (columns). Note that Matrix A * 10 == Matrix B (A * 10 == B). This means these matrices are scalar multiples of each other.

ma trận B vẫn hợp lệ. Nó biểu hiện mối liên hệ giữa những mẫu dữ liệu huấn luyện và đặc tính tín hiệu đèn. chú ý rằng chúng ta có mối quan hệ sau Matrix A * 10 == Matrix B (A * 10 == B). Matrix A * 10 == Matrix B (A * 10 == B). Điều này chững tỏ chúng là những ma trận có mối quan hệ tuyến tính

Creating a matrix or two in Python

You’ve converted the streetlight pattern into a matrix (one with just 1s and 0s). Now let’s create that matrix (and, more important, its underlying pattern) in Python so the neural network can read it. Python’s NumPy library was built just for handling matrices. Let’s see it in action:

bạn đã chuyển mẫu đèn giao thông sang dạng ma trận (với dạng 1s và 0s). bây giờ chúng ta hãy tạo ra ma trận đó bằng ngôn nhữ Python để mạng neuron có thể đọc nó được. thư viện numpy của python được xây dựng để giải quyết các vấn đề liên quan đến ma trận.

If you’re a regular Python user, something should be striking in this code. A matrix is just a list of lists. It’s an array of arrays. What is NumPy? NumPy is really just a fancy wrapper for an array of arrays that provides special, matrix-oriented functions. Let’s create a NumPy matrix for the output data, too:

nếu bạn là một người thường xuyên dùng python, bạn sẽ được điểm quan trong đoạn code này. ma trận chỉ là list của list. Nó là mảng của mảng. Vậy numpy là gì? numpy chỉ là một wrapper cho mảng và cung cấp những hàm chuyên biệt cho ma trận. chúng ta hãy tạo ma trận của dữ liệu đầu ra:

What do you want the neural network to do? Take the streetlights matrix and learn to transform it into the walk_vs_stop matrix. More important, you want the neural network to take any matrix containing the same underlying pattern as streetlights and transform it into a matrix that contains the underlying pattern of walk_vs_stop. More on that later. Let’s start by trying to transform streetlights into walk_vs_stop using a neural network.

Bạn muốn mạng neuron làm gì? lấy ma trận của đèn giao thông và học cách để biến đổi nó thành ma trận dừng hoặc đi. quan trọng hơn, bạn muốn mạng neuron hoạt động một cách tổng quát trên những dữ liệu khác nhưng có chung dạng đặc thù. Chúng ta sẽ bàn sâu về vấn đề này sau. Hãy bắt đầu bằng việc thử chuyển đổi đèn giao thông thành ma trần dừng đi sử dụng mạng neuron

Building a neural network

The following code explain the basic neural network with the random weight initialization, and using mean square error as a loss function

Đoạn code sau đây thể hiện mạng neuron cơ bản với trọng số ngẫu nhiên và sử dụng mean squre error như là hàm loss

In part 2 we will discuss about full, batch and stochastic gradient descent also overfiting etc ….

The notebook for this section can be found at:

--

--