Abstract:



Note: Since this article was published, it has received great praise and attention from readers. Therefore, I wrote one

Machine learning test library

Please click the link to check it out!

In the past year, I spent a lot of time on deep learning and made many mistakes. These mistakes not only helped me gain a deeper understanding of machine learning, but also made me learn how to design these systems correctly and reasonably. One of the many design principles I’ve learned while working at Google Brain is that unit testing can make or break algorithms and save weeks of debugging and training.

However, so far, there does not seem to be a reliable tutorial for coding test units for neural network code. Even in the

On OpenAI, it just passes

Look at the code line by line to discover

bug

And then think about what leads to this
bug
What is the cause of

. Obviously, most people don’t want to spend that much time, so I hope after reading this tutorial, you’re ready to start testing your system!

Let’s start with a simple example: Try to find a bug in this code.

Did you find any bugs? In fact, the neural network does not stack. When I wrote the code, I just did slim. Conv2d (…) Lines of code are simply copy-pasted, and the kernel size is modified without actual input.

Slightly awkwardly, this is actually code I wrote last week… This is an important lesson too!

But these bugs are hard to spot for several reasons: 1. The code will never crash, or cause errors, or run slowly. 2. The neural network is still training, and the loss function gets smaller and smaller. 3. After a few hours, it converges to a certain number, and the result is very bad, but you don’t know what to change.

When the only feedback is the final error validation, then you have only one solution — search the entire network architecture. Say no more, what you need is a better network. How do we find this bug after a full day of training with the data? The easiest thing to notice, we find, is that the value of the layer never actually reaches any other tensor outside the function. Therefore, assuming we have some type of loss function and optimizer, these tensors will never be optimized, they will always remain the default values.

Through simple exercises, we compare the results before and after training:

In less than 15 lines of code, we’ve basically verified all the variables we’ve trained.

This test is very simple and practical. Now, assuming we have fixed the last problem, let’s add some batch optimizations and see if we can spot the bug.

See? This is very subtle. In TensorFlow, batch_norm actually defaults is_training to False, so adding this line of code does not normalize the input during training! Thankfully, the last unit test we write will find this problem right away!

Let’s look at another example from
reddit

A post of

: the author wants to create a grouping with output range (0,1). Can you find the bug?

This bug is hard to find and can lead to particularly messy results if not noticed. Basically, this prediction has only one output when you use it
softmax

The cross entropy

, always causes the loss function to be 0.

The easiest way to test this code is to make sure that the loss function is never zero.

This test is similar to our first test, except for the fallback. In this test, you can be sure to train only the variables you want to train. In the case of generative adversarial networks (GAN), a common bug is forgetting which variables were trained during optimization, and such bugs often occur.

The biggest problem is that the optimizer has a default setting to tune all variables. For architectures like adversarial generative networks, this is a death sentence for all training time. Here, you can easily detect these bugs using the following test code:

Similarly, we can write similar tests for discriminators or other reinforcement learning algorithms. Many actor-review models have their own relatively independent networks that need to be optimized with different losses.

In order for you to be better able to take the test after reading this article, I think the following suggestions are important:

1. Ensure the certainty of the test. If you really want to enter data randomly, make sure you enter data randomly so that you can easily complete the test.

2. Keep the test short. Make sure you have unit tests that train convergence and check validation sets, otherwise you’re wasting your time.

3. Be sure to reset the chart before each test.

Anyway, there are many more ways to test these algorithms. Taking an hour to write a test will not only save you time to retrain, but also greatly improve your research results!

How to Unit Test Machine Learning Code

The original link

This article is the original content of the cloud habitat community, shall not be reproduced without permission.