18

How do I decide the optimal number of layers for a neural network (feedforward or recurrent)?

nbro
  • 39,006
  • 12
  • 98
  • 176
v01d
  • 283
  • 2
  • 6
  • See also the question [How to choose the number of hidden layers and nodes in a feedforward neural network?](https://stats.stackexchange.com/q/181/82135) at CrossValidated SE. – nbro Apr 12 '20 at 18:55

2 Answers2

7

There is a technique called Pruning in neural networks, which is used just for this same purpose.

The pruning is done on the number of hidden layers. The process is very similar to the pruning process of decision trees. The pruning process is done as follows:

  • Train a large, densely connected, network with a standard training algorithm
  • Examine the trained network to assess the relative importance of the weights
  • Remove the least important weight(s)
  • retrain the pruned network
  • Repeat steps 2-4 until satisfied

However, there are several optimized methods for pruning neural nets, and it is also a very active area of research.

Dawny33
  • 1,371
  • 13
  • 29
  • 1
    A symmetric approach is the common "grid search" applied to the network architecture. Start small (so fast), and automatically try larger architectures. All this is just brute force, though, so it is expensive. Yet, when we can start small, the first stages can go pretty fast, and give a better idea of what to aim for. – Eric Platon Aug 03 '16 at 06:41
  • 1
    This answer is **incorrect**. Pruning is a method applied onto the weights, not a method to determine the number of layers (a hyperparameter). – k.c. sayz 'k.c sayz' Oct 20 '17 at 02:02
2

You can take a look at bayesian hyperparameter optimization as a general method of optimizing loss (or anything) as a function of the hyperparameters. But note that in general the deeper your network the better, so optimizing loss as a function of number of layers isn't a very fun thing to do.

Grid search and a bit of common sense (as learnt by seeing many examples) should be your best bet.

k.c. sayz 'k.c sayz'
  • 2,061
  • 10
  • 26