<p>The number of layers, or depth, of a neural network is one of its <em>hyperparameters</em>.</p>

<p>This means that it is a quantity that can not be learned from the data, but you should choose it <strong>before</strong> trying to fit your dataset. According to <a href="http://arxiv.org/abs/1206.5533" rel="nofollow">Bengio</a>, </p>

<blockquote>
  <p>We define a hyper-
  parameter for a learning algorithm A as a variable to
  be set prior to the actual application of A to the data,
  one that is not directly selected by the learning algo-
  rithm itself.</p>
</blockquote>

<p>There are three main approaches to find out the optimal value for an hyperparameter. The first two are well explained in the paper I linked.</p>

<ul>
<li>Manual search. Using well-known black magic, the researcher choose the optimal value through try-and-error.</li>
<li>Automatic search. The researcher relies on an automated routine in order to speed up the search.</li>
<li><a href="https://github.com/HIPS/Spearmint" rel="nofollow">Bayesian optimization</a>.</li>
</ul>

<p>More specifically, adding more layers to a deep neural network is likely to improve the performance (reduce generalization error), up to a certain number when it overfits the training data.</p>

<p>So, in practice, you should train your ConvNet with, say, 4 layers, try adding one hidden layer and train again, until you see some overfitting. Of course, some strong regularization techniques (such as <strong>dropout</strong>) is required.</p>
How to find dynamically the depth of a network in Convolutional Neural Network
<p>I was looking for an automatic way to decide how many layers should I apply to my network depends on data and computer configuration. I searched in web, but I could not find anything. Maybe my keywords or looking ways are wrong.</p>

<p>Do you have any idea?</p>