Choosing the number of layers in your neural network is an important factor in determining how well it will perform your task. Too many layers can lead to overfitting, where the model learns complicated features that only apply to the training set and do not transfer to new situations.

On the other hand, one too few layers may fail to capture all of the patterns needed to classify the data.

In this article we will discuss some strategies for choosing the layer size of your net! Stay tuned till the end for our top choice winner. I’ll also go into more detail about what types of networks use different numbers of layers, and why.

By now you should have a good idea of what deep learning is and whether or not that framework is appropriate for your application domain.

Do some research

deep learning how to choose number of layers

There are many ways to choose your number of layers in a neural network. You can do it by looking at past examples, experimenting with different architectures, or using tools that use architecture as a basis for choosing numbers.

Some software packages will automatically pick an appropriate number of layers for you depending on the task the algorithm is being set to perform.

And finally, you can just take a guess! If you feel confident about how many layers your current model has, go with that. Sometimes people get lazy and add another layer here and there to see what happens.

This article will talk more about how to determine if your model needs more than one hidden layer before the output.

Test your hypothesis

deep learning how to choose number of layers

A very common way to evaluate the performance of a neural network model is to test how well it fits the data. This approach has two benefits. First, you can use this method even if you have no initial idea about which architecture works best. You can compare different architectures using this testing technique to determine what is working better.

Second, this approach allows you to check not only whether there is improvement by adding more layers or widths and depths of each layer, but also whether increasing either one of these factors improves the accuracy of the model.

You should always start with just one hidden layer because it is usually enough to capture most of the patterns in the training dataset. As you add additional layers, however, the networks begin to overfit the data. Overfitting occurs when the model uses too many parameters to fit the data, making the model work almost perfectly on the train set but poorly on new examples that are not represented in the train set.

Always use the most number of layers

When it comes down to it, you will not find many papers that claim there is one best architecture for deep learning. Almost every paper will suggest their own model with a specific numbers of layers or blocks.

This can be confusing because different people may have different intuitions about how much depth an NN needs to work.

Some might believe that fewer layers are better, while others feel that more are! It really depends on what you want the network to learn and how well these networks perform in your problem domain.

If we look at very large scale computer vision applications like self-driving cars, then less layers make sense since you do not need as much spatial understanding of the surroundings.

For other domains such as natural language processing (NLP), having lots of layers allows each layer to focus on higher level concepts. By adding several convolutional layers, you can gain some semantic information from the layers before it.

There are no rules

deep learning how to choose number of layers

Choosing the number of layers in your network is an important hyperparameter that can have a significant impact on how well your model will perform your task. Too many layers can cause overfitting, while too few may not sufficiently generalize the data you are training it on.

There are no hard and fast guidelines for determining the optimal layer size for any specific problem domain and dataset. The best way to determine this is by experimenting with different configurations and observing the test accuracy on your experimental set-up!

By trying out various numbers of layers and their potential configs such as batch sizes, learning rates etc., you can find the one that works the best in terms of both accuracy and train time. Because there are so many variables at play here, there are very little hard and clear recommendations about what settings work best across all problems in the field.

General trends exist, however. For example, most models these days seem to converge around having 5–7 convolutional layers before a fully connected layer. Beyond that, things get pretty fuzzy. Some people even put a limit of 2 or 3 fully connected layers! So, unfortunately, we cannot give you definitive answers about ideal layer configuration beyond “try lots of them out” and “do whatever seems to improve performance the most on your experiment setup”.

Commit to learning

deep learning how to choose number of layers

There is no clear best number of layers for deep convolutional networks, so you can try different configurations and see which one works better for your data! If one seems too slow or not working well, you can always reduce the depth by a few layers until it runs faster.

The most important thing about choosing the number of layers in a network is that you need to be confident that they will work before investing time into developing them. Test out as many variations as possible, and don’t hesitate to start with very shallow networks if needed!

We have provided some helpful tips above, but here are our top three recommendations: use batch size 64 samples per GPU, use at least two GPUs to train, and use ReLUs (Rectified Linear Units) instead of tanh activations.

Focus on accuracy

deep learning how to choose number of layers

When it comes down to it, how many layers your model has doesn’t matter too much unless you care about accuracy. If you do, go for as many as possible!

In fact, some studies have shown that models with more than 100 million parameters can actually hurt test accuracy instead of helping it. This is because overfitting happens when a neural network learns patterns in the data itself rather than using features external to the dataset.

Overfitting usually occurs due to excessive number of weights being adjusted during training, or when there are too many potential combinations of weight settings. Both of these cause the neural net to get “caught up” in local minima, making it fail to generalize correctly.

Generalization refers to predicting outcomes outside of the examples given during training. A good example of this would be if I told you that most people like apple pie but not chocolate chip cookies. Then asked you what kind of cookie someone makes with apples and chocolate chips, you could probably come close and predict that it will be an oatmeal raisin cookie.

However, I could then show you a list of all the ingredients for those cookies and you wouldn’t know which one to pick! Generalizing beyond the instances used for training is something active networks should strive towards.

Try new things

deep learning how to choose number of layers

As mentioned earlier, there is no one best way to choose your number of layers for your network. It really depends on what you are trying to accomplish and how much data you have available.

One important factor in choosing depth is memory. More layers require more RAM to store all of the weights and other variables.

The more layers you have, the larger the neural networks get! This can make it difficult to train the model due to limited GPU or CPU resources.

Luckily, GPUs now cost around 200-500 dollars which is relatively cheap, so don’t worry about that! If possible, having more than two GPUs is ideal as then you can use parallelization when training to reduce time spent learning.

However, just because something costs money does not mean it is better, be careful about buying too many pieces equipment before testing out different configurations. Sometimes companies will add overhead to justify their price, so do some research and test out settings yourself before investing.

Challenge your assumptions

deep learning how to choose number of layers

When it comes down to it, how many layers an ML model has is not that important. This is not to say that layer thicknesses do not matter, but only slightly!

The number of layers in a neural network impacts performance marginally at best. Depending on what you are trying to train, even one more or less layer can make a slight difference in accuracy.

Furthermore, some research shows that larger networks usually result in better overall performance, which may outweigh any benefits from thinner models.

Thinned out DNNs are much faster to run than thicker ones with equivalent results, making them preferable when running limited resources. On top of that, they require fewer GPU cores to reach similar levels of performance, reducing cost as well!

Summary: The most crucial factor when choosing the depth of a NN is cost-effectiveness.

Caroline Shaw is a blogger and social media manager. She enjoys blogging about current events, lifehacks, and her experiences as a millennial working in New York.