building your first deep neural network — Practical Tricks(Part 2)

Duy Anh Nguyen
11 min readFeb 27, 2019

Full, batch, and stochastic gradient descent

As it turns out, this idea of learning one example at a time is a variant on gradient descent called stochastic gradient descent, and it’s one of the handful of methods that can be used to learn an entire dataset. How does stochastic gradient descent work? As you saw in the previous example, it performs a prediction and weight update for each training example separately. In other words, it takes the first streetlight, tries to predict it, calculates the weight_delta, and updates the weights. Then it moves on to the second streetlight, and so on. It iterates through the entire dataset many times until it can find a weight configuration that works well for all the training examples.

việc học theo một mẫu dữ liệu tại một thời điểm được gọi là “stochastic gradient descent” và nó là một cách hữu ích cho việc học trên toàn bộ dữ liệu. vậy “stochastic gradient descent” hoạt động nhue thế nào? như bạn đã thấy ở bài trước nó thể hiện sự phỏng đoán và cập nhật trọng số cho mỗi riêng một mẫu dữ liệu. Nói theo cách khác, nó lấy mẫu đèn giao thông đầu tiên, thử phỏng đoán việc đi hay dừng, tính toán độ lệch về trọng số, và cập nhật trọng số. Sau đó nó chuyển sang mẫu thứ hai và cứ tiếp tục như vậy. Tiến trình này lặp đi lặp lại trên toàn bộ dữ liệu nhiều lần cho đến khi nó tìm được trọng số hoạt động cho tất cả các mẫu dữ liệu huấn luyện

(Full) gradient descent updates weights one dataset at a time

Another method for learning an entire dataset is gradient descent (or average/full gradient descent). Instead of updating the weights once for each training example, the network calculates the average weight_delta over the entire dataset, changing the weights only each time it computes a full average

Một phương pháp để học hoàn toàn bộ dữ liệu đó là gradient descent. Thay vì cập nhật trọng số 1 lần cho mỗi 1 mẫu dữ liệu huấn luyện, mạng neuron sẽ tính delta trung bình của trọng số trên toàn bộ tập dữ liệu, và chỉ khi nào tính được giá trị trung bình này, trọng số mới được cập nhật

Batch gradient descent updates weights after n examples

This will be covered in more detail later, but there’s also a third configuration that sort of splits the difference between stochastic gradient descent and full gradient descent. Instead of updating the weights after just one example or after the entire dataset of examples, you choose a batch size (typically between 8 and 256) of examples, after which the weights are updated.

điều này sẽ được nói chi tiết hơn ở những phần sau, nhưng có một cách thứ 3 ngoài “stochastic” và “full” gradient descent. Thay vì cập nhật trọng số sau mỗi một điểm dữ liệu hoặc sau toàn bộ dữ liệu, bạn chọn kích thước nhóm (thông thường giữa 8 và 256) của dữ liệu mà sau đó trọng số được cập nhật

Neural networks learn correlation

You just got done training a single-layer neural network to take a streetlight pattern and identify whether it was safe to cross the street. Let’s take on the neural network’s perspective for a moment. The neural network doesn’t know that it was processing streetlight data. All it was trying to do was identify which input (of the three possible) correlated with the output. It correctly identified the middle light by analyzing the final weight positions of the network. Notice that the middle weight is very near 1, whereas the far-left and far-right weights are very near 0. At a high level, all the iterative, complex processes for learning accomplished something rather simple: the network identified correlation between the middle input and output. The correlation is located wherever the weights were set to high numbers. Inversely, randomness with respect to the output was found at the far-left and far-right weights (where the weight values are very near 0). How did the network identify correlation? Well, in the process of gradient descent, each training example asserts either up pressure or down pressure on the weights. On average, there was more up pressure for the middle weight and more down pressure for the other weights. Where does the pressure come from? Why is it different for different weights?

bạn vừa mới hoàn thành việc huấn luyện mạng neuron một lớp (part 1) để chuyển đổi tín hiệu đèn giao thông sang việc liệu rằng có an toàn để sang đường hay không. chúng ta hãy nhìn nhận về mạng neuron một lúc. Mạng neuron không hề biết nó đang xử lý tín hiệu về đèn giao thông. Tất cả những gì nó làm là xác định được sự tương quan giữa dữ liệu đầu vào và dữ liệu đầu ra. Mạng neuron xác định chính xác được đèn ở giữa bằng cách phân tích vị trí trọng số cuối cùng. Chú ý rằng trọng số ở giữa rất gần 1. Trong khi trọng số ở bên trái và bên phải rất gần 0. Nhìn nhận một cách tổng quát, quá trình học của mạng neuron chỉ đơn giản là xác định tương quan của trọng số ở giữa và tín hiệu đầu ra. Ngược lại, sự ngẫu nhiên của đầu ra phụ thuộc ở trọng số bên trái và bên phải. Làm thế nào mà mạng neuron xác định được sự tương quan? trong quá trình “gradient descent” mỗi một điểm dữ liệu huấn luyện xác định được “áp lực” theo hướng xuống hoặc lên trên những trọng số. Tổng trung bình lại, có nhiều áp lực hướng lên ở trọng số nằm giữa và nhiều áp lực hướng xuống cho những trọng số khác. Các áp lực này bắt nguồn từ đâu? và vì sao các trọng số khác nhau lại có những áp lực khác nhau?

Up and down pressure

It comes from the data. Each node is individually trying to correctly predict the output given the input. For the most part, each node ignores all the other nodes when attempting to do so. The only cross communication occurs in that all three weights must share the same error measure. The weight update is nothing more than taking this shared error measure and multiplying it by each respective input. Why do you do this? A key part of why neural networks learn is error attribution, which means given a shared error, the network needs to figure out which weights contributed (so they can be adjusted) and which weights did not contribute (so they can be left alone).

Áp lực đến từ dữ liệu. Mỗi một nút đang cố gắng đoán kết quả đầu ra dựa theo đầu vào. Cho hầu hết các trường hợp, mỗi một nút bỏ thường bỏ qua các nút khác khi làm điều này. Điều duy nhát mà các nút này liên hệ chéo với nhâu đó là 3 trọng số phải có 1 lượng lỗi như nhau. Việc cập nhật trọng số chỉ là lấy sai số chung và nhân với đầu vào tương ứng. Vì sao bạn làm điều này? điều quan trọng nhất để mạng neuron học đó là sự phân chia lỗi, có nghĩa là cho 1 lỗi chung, mạng neuron cần chỉ ra được trọng số nào góp phần và trọng số nào không góp phần

Consider the first training example. Because the middle input is 0, the middle weight is completely irrelevant for this prediction. No matter what the weight is, it’s going to be multiplied by 0 (the input). Thus, any error at that training example (regardless of whether it’s too high or too low), can be attributed to only the far-left and right weights. Consider the pressure of this first training example. If the network should predict 0, and two inputs are 1s, then this will cause error, which drives the weight values toward 0. The Weight Pressure table helps describe the effect of each training example on each respective weight. + indicates that it has pressure toward 1, and — indicates that it has pressure toward 0. Zeros (0) indicate that there is no pressure because the input datapoint is 0, so that weight won’t be changed. Notice that the far-left weight has two negatives and one positive, so on average the weight will move toward 0. The middle weight has three positives, so on average the weight will move toward 1.

Chúng ta hãy xem thử mẫu dữ liệu huấn luyện đầu tiên. bởi vì dữ liệu đầu vào ở giữa là 0, nó hoàn toàn không liên quan đến việc suy đoán của mô hình mạng neuron. Bất kể trọng số là gì nó cũng sẽ bị nhân với 0 (dữ liệu đầu vào). Do đó bất kể sai số nào trong dữ liệu huấn luyện chỉ có thể đóng góp vào trọng số bên trái hoặc bên phải. Hãy xem xét áp lực ở dữ liệu huấn luyện đầu tiên. nếu mạng neuron đoán là 0, và hai dữ liệu đầu vào là 1s, sẽ có lỗi xảy ra, khiến cho giá trị trọng số tiến về 0.Bảng áp lực trọng số giúp thể hiện những tác động của mỗi một mẫu dữ liệu huấn luyện tương ứng: + cho thấy áp lực tiến về 1, và — cho thấy áp lực tiến về 0. 0 cho thấy không có áp lực bởi vì dữ liệu đầu vào là 0 vì vậy trọng số sẽ không thay đổi. Chú ý rằng trọng số phía bên trái có 2 giá trị âm và 1 giá trị dương, nên trọng số trung bình sẽ di chuyển về 0. Trọng số ở giữa có 3 giá trị dương nên trọng số trung bình sẽ di chuyển về 1

Each individual weight is attempting to compensate for error. In the first training example, there’s dis-correlation between the far-right and far-left inputs and the desired output. This causes those weights to experience down pressure. This same phenomenon occurs throughout all six training examples, rewarding correlation with pressure toward 1 and penalizing de-correlation with pressure toward 0. On average, this causes the network to find the correlation present between the middle weight and the output to be the dominant predictive force (heaviest weight in the weighted average of the input), making the network quite accurate.

mỗi một trọng số riêng lẻ sẽ cố gắng “bù” vào lỗi. Ở dữ liệu training đầu tiên, có sự không tương quan giữa dữ liệu đầu vào bên trái và bên phải với kết quả mong muốn. Điều này khiến cho trọng số chịu áp lực đi xuống. Hiện tượng này xảy ra cho cả 6 mẫu dữ liệu huấn luyện, tăng độ tương quan với áp lực tiến về 1 và giảm sự tương quan với áp lực về 0. Về trung bình, điều này khiến cho mạng neuron tìm thấy sự tương quan giữa trọng số ở giữa và dữ liệu đầu ra là sự tương quan lớn

Edge case: Overfitting

Sometimes correlation happens accidentally. Consider again the first example in the training data. What if the far-left weight was 0.5 and the far-right weight was –0.5? Their prediction would equal 0. The network would predict perfectly. But it hasn’t remotely learned how to safely predict streetlights (those weights would fail in the real world). This phenomenon is known as overfitting.

Đôi khi sự tương quan xảy ra một cách bất ngờ. Hãy xem xét lại ví dụ đầu tiên ở dữ liệu huấn luyện. Điều gì sẽ xảy ra nếu trọng số ở bên trái là 0.5 và trọng số bên phải là -0.5. phỏng đoán của model sẽ là 0. Mạng neuron sẽ đoán một cách chính xác. Nhưng nó sẽ không học được làm như thế nào để có thể đoán được khi nào bạn có thể sang đường. Hiện tượng này gọi là “overfitting”

Stacking neural networks

When you look at the following architecture, the prediction occurs exactly as you might expect when I say, “Stack neural networks.” The output of the first lower network (layer_0 to layer_1) is the input to the second upper neural network (layer_1 to layer_2). The prediction for each of these networks is identical to what you saw before.

khi bạn nhìn vào kiến trúc dưới đây, bạn sẽ nhận ra đó là “mạng neuron xếp lớp”. Đầu ra của mạng cấp thấp (ayer_0 đến layer_1) là tín hiệu đầu vào cho mạng cấp cao trên nó (layer_1 to layer_2). Sự Tiên đoán cho mỗi một mạng tương tự như những gì bạn đã biết trước đó

Backpropagation: Long-distance error attribution

The weighted average error. What’s the prediction from layer_1 to layer_2? It’s a weighted average of the values at layer_1. If layer_2 is too high by x amount, how do you know which values at layer_1 contributed to the error? The ones with higher weights (weights_1_2) contributed more. The ones with lower weights from layer_1 to layer_2 contributed less. Consider the extreme. Let’s say the far-left weight from layer_1 to layer_2 was zero. How much did that node at layer_1 cause the network’s error? Zero. It’s so simple it’s almost hilarious. The weights from layer_1 to layer_2 exactly describe how much each layer_1 node contributes to the layer_2 prediction. This means those weights also exactly describe how much each layer_1 node contributes to the layer_2 error. How do you use the delta at layer_2 to figure out the delta at layer_1? You multiply it by each of the respective weights for layer_1. It’s like the prediction logic in reverse. This process of moving delta signal around is called back-propagation.

Trọng số sẽ trung bình độ sai lêch. Điều phỏng đoán gì xảy ra ở layer_1 đến layer_2? đó là trung bình trọng số của giá trị tại layer_1. Nếu layer_2 là quá cao (khoảng một lượng x), làm sao bạn biết được gía trị nào ở layer_1 đóng góp vào lỗi? Phần với trọng số lớn hơn (weights_1_2) đóng góp nhiều hơn. phần với trọng số thấp hơn từ layer_1 đến layer_2 đóng góp ít hơn. Nếu xét về khía cạnh trọng số phía bên trái từ layer_1 đến layer_2 là 0. Thì nút tại layer_1 tạo lỗi cho mạng neuron là bao nhiêu? 0 ! Đơn giản. Trọng số từ layer_1 đến layer_2 diễn tả chính xác layer_1 đóng góp bao nhiêu cho sự tiên đoán của layer_2. Điều này có nghĩa là những trọng số đó diễn tả chính xác nút tại layer_1 đóng góp bao nhiêu vào lỗi ở layer_2. làm sao bạn có thể sử dụng lượng delta ở layer_2 để chỉ ra về lượng delta cần ở layer_1. Bạn nhân nó với mỗi trọng số tương ứng ở layer_1. Tiến trinh di chuyển tín hiệu delta này gọi là sự lan truyền ngược (back-propagation)

Backpropagation in code

firstly we can define the RELU activation function and its derivative

after that we can define the necessary parameters such as weights, the number of iteration; pay attention to the back propagation in layer 2 and in layer 1

The code for this section can be found at:

https://github.com/ngduyanhece/medium_ai_tutor/blob/master/dl_from_scratch/back_prop_in_code.ipynb

--

--