# How to choose number of hidden layers?

**URL:** <https://community.deeplearning.ai/t/how-to-choose-number-of-hidden-layers/272200>\
**Category:** AI Discussions\
**Tags:** ai-discussions\
**Created:** [February 11, 2023, 5:42am UTC](https://community.deeplearning.ai/t/how-to-choose-number-of-hidden-layers/272200 "2023-02-11T05:42:59Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Devarsh\_Mavani](https://avatars.discourse-cdn.com/v4/letter/d/db5fbb/32.png) [@Devarsh\_Mavani](https://community.deeplearning.ai/u/Devarsh_Mavani)\
**Post date:** [February 11, 2023, 5:42am UTC](https://community.deeplearning.ai/t/how-to-choose-number-of-hidden-layers/272200/1 "2023-02-11T05:42:59Z")

</div>

When we are making a neural network, how do we decide on number of hidden layers and number of units inside of hidden layers? For example recently I was working on the loan dataset where I had to predict if the customer will be able to pay the loan. There were about 15+ features (Such as salary, age, previous loans, etc).  
I am curious what is the deciding factor for number of hidden layers. I completed the ML Specialization recently, but did not found answer to this in any of its course.

---

<div class="post-metadata">

**Author:** ![Christian\_Simonis](https://sea1.discourse-cdn.com/dlai/user_avatar/community.deeplearning.ai/christian_simonis/32/142583_2.png) [@Christian\_Simonis](https://community.deeplearning.ai/u/Christian_Simonis)\
**Post date:** [February 11, 2023, 8:12am UTC](https://community.deeplearning.ai/t/how-to-choose-number-of-hidden-layers/272200/2 "2023-02-11T08:12:06Z")

</div>

Hi @Devarsh_Mavani

welcome to the community!

Here you should find a similar thread where the question is answered: [Dimensioning a neural network - #2 by AbdElRhaman\_Fakhry](https://community.deeplearning.ai/t/dimensioning-a-neural-network/266094/2)

In summary:

there is no right or wrong, but you can think about if the dimensional space which is spanned by the neurons would be sufficient to solve your problem. Finally it’s your task to design a good architecture, also with trial and error. I have seen it often, that ML engineers rather tend to increase the feature dimensions (or number of neurons) in the first hidden layer compared to the input layer which can be interpreted as giving the net more opportunity to learn complex and abstract behaviour.

[Source](https://community.deeplearning.ai/t/shallow-vs-deep-neural-network/249269/2)

> [@Devarsh\_Mavani](#):
>
> There were about 15+ features (Such as salary, age, previous loans, etc).

For example you can:

- check and quantify if these feature contain mutual information resp. remove redundant information (w/ Principal Component Analysis (PCA) or Partial Least Square transformation (PLS)) to enhance your ratio of data to features;
- also check the feature importance to focus on the most meaningful ones!

More details on PCA, PLS, importance calculation etc. can be found here: [https://github.com/christiansimonis/CRISP-DM-AI-tutorial/blob/master/Classic\_ML.ipynb](https://github.com/christiansimonis/CRISP-DM-AI-tutorial/blob/master/Classic_ML.ipynb)

Please let me know if you have any open questions.

Best regards  
Christian

---

<div class="post-metadata">

**Author:** ![Isaak\_Kamau](https://sea1.discourse-cdn.com/dlai/user_avatar/community.deeplearning.ai/isaak_kamau/32/124242_2.png) [@Isaak\_Kamau](https://community.deeplearning.ai/u/Isaak_Kamau)\
**Post date:** [February 11, 2023, 9:08am UTC](https://community.deeplearning.ai/t/how-to-choose-number-of-hidden-layers/272200/3 "2023-02-11T09:08:46Z")

</div>

Hello @Devarsh_Mavani  
In addition to what @Christian_Simonis has said, DeepLearning.AI still has another interesting course that explains **Hyperparameters tuning and searching for the best model architecture** to use. Check it out [Machine Learning Modeling Pipelines in Production](https://www.coursera.org/learn/machine-learning-modeling-pipelines-in-production)

---

<div class="post-metadata">

**Author:** ![Devarsh\_Mavani](https://avatars.discourse-cdn.com/v4/letter/d/db5fbb/32.png) [@Devarsh\_Mavani](https://community.deeplearning.ai/u/Devarsh_Mavani)\
**Post date:** [February 11, 2023, 10:12am UTC](https://community.deeplearning.ai/t/how-to-choose-number-of-hidden-layers/272200/4 "2023-02-11T10:12:00Z")

</div>

Thanks for your reply @Christian_Simonis.  
I went through the links and what I understood is that there is no mathematical way of arriving at no. of layers and neurons. It is completely based on trial and error. If it is the case, Then won’t it be computationally expensive to make something as powerful as chatGPT? and also time consuming

---

<div class="post-metadata">

**Author:** ![Isaak\_Kamau](https://sea1.discourse-cdn.com/dlai/user_avatar/community.deeplearning.ai/isaak_kamau/32/124242_2.png) [@Isaak\_Kamau](https://community.deeplearning.ai/u/Isaak_Kamau)\
**Post date:** [February 11, 2023, 10:45am UTC](https://community.deeplearning.ai/t/how-to-choose-number-of-hidden-layers/272200/5 "2023-02-11T10:45:31Z")

</div>

@Devarsh_Mavani For such massive models, there is a technique used called **Neural Architecture Search**  
Which utilizes **KerasTuner**

> KerasTuner is an easy-to-use, scalable hyperparameter optimization framework that solves the pain points of hyperparameter search. Easily configure your search space with a define-by-run syntax, then leverage one of the available search algorithms to find the best hyperparameter values for your models. KerasTuner comes with Bayesian Optimization, Hyperband, and Random Search algorithms built-in, and is also designed to be easy for researchers to extend in order to experiment with new search algorithms.

You can read more about it in this papers

- [Neural Architecture Search](https://arxiv.org/pdf/1808.05377.pdf)

- [Neural Architecture Search with Reinforcement Learning](https://arxiv.org/pdf/1611.01578.pdf)

- [Progressive Neural Architecture Search](https://arxiv.org/pdf/1712.00559.pdf)

---

<div class="post-metadata">

**Author:** ![ai\_curious](https://sea1.discourse-cdn.com/dlai/user_avatar/community.deeplearning.ai/ai_curious/32/128003_2.png) [@ai\_curious](https://community.deeplearning.ai/u/ai_curious)\
**Post date:** [February 11, 2023, 2:06pm UTC](https://community.deeplearning.ai/t/how-to-choose-number-of-hidden-layers/272200/6 "2023-02-11T14:06:22Z")

</div>

> [@Isaak\_Kamau](#):
>
> For such massive models, there is a technique used called **Neural Architecture Search**  
> Which utilizes **KerasTuner**

@rmwkwok might be interested in this as NAS came up recently in a different thread

---

<div class="post-metadata">

**Author:** ![rmwkwok](https://sea1.discourse-cdn.com/dlai/user_avatar/community.deeplearning.ai/rmwkwok/32/126977_2.png) [@rmwkwok](https://community.deeplearning.ai/u/rmwkwok)\
**Post date:** [February 12, 2023, 12:47am UTC](https://community.deeplearning.ai/t/how-to-choose-number-of-hidden-layers/272200/7 "2023-02-12T00:47:06Z")

</div>

Thank you @ai_curious!
