Answers.Body: <p><strong>tl;dr</strong> I'd say your model has <strong>8 layers</strong> (5 conv, 3 dense), however a lot of people count layers in other ways. From what I've seen this is by far the most <strong>conventional</strong> way for counting layers.</p>
  
  <hr>
  
  <h3>Justification</h3>
  
  <p>This is an interesting question because its quite subjective. In most cases only the convolutional and dense layers would count from your network. Bach norm, dropout and flatten are usually considered as operations to other layers rather than layers of their own (much like activation functions).</p>
  
  <p>Note: It is debatable whether or not pooling layers are considered to be layers (as they have no trainable parameters) but in most cases they are not considered to be so. </p>
  
  <p>Note 2: Batch norm, on the other hand, isn't usually considered a layer even though it has trainable parameters. Clearly the authors didn't <a href="https://arxiv.org/pdf/1502.03167.pdf" rel="nofollow noreferrer">introduce it as a layer</a>, but as a way to normalize, shift and scale the inputs of a layer. This is apparent in some of the examples below which don't count batch norm as an actual layer.</p>
  
  <p>Note 3: Conventionally all networks are considered to have [at least] one input layer but it doesn't count as a layer.</p>
  
  <hr>
  
  <h3>Examples</h3>
  
  <p>Some examples that follow this reasoning when counting layers are the following. I'll also write the pooling layers in each, but they clearly aren't considered as layers be the authors. When available I'll also write the number of layers that keras registers from their <a href="https://keras.io/applications/" rel="nofollow noreferrer">official implementations</a>:</p>
  
  <p>The <a href="https://external-content.duckduckgo.com/iu/?u=https%3A%2F%2Fcv-tricks.com%2Fwp-content%2Fuploads%2F2019%2F07%2FResNet50_architecture-1.png&amp;f=1&amp;nofb=1" rel="nofollow noreferrer">ResNet-50</a> architecture has 50 layers (49 conv, 2 pool, 1 dense), however keras registers it as 177 layers. ResNets also <a href="https://arxiv.org/pdf/1512.03385.pdf" rel="nofollow noreferrer">use batch normalization after each convolution</a> (so 49 batch norms in total), but clearly don't count them as layers.</p>
  
  <p>The <a href="https://external-content.duckduckgo.com/iu/?u=https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F1600%2F1*4tlPOipWjcwIoNUlQ6IWFQ.png&amp;f=1&amp;nofb=1" rel="nofollow noreferrer">Resnet-34</a> has 34 layers (33 conv, 2 pool, 1 dense). Like the previous, this also uses batch norm but doesn't count it.</p>
  
  <p><a href="https://external-content.duckduckgo.com/iu/?u=https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F1600%2F1*4tlPOipWjcwIoNUlQ6IWFQ.png&amp;f=1&amp;nofb=1" rel="nofollow noreferrer">VGG-19</a> has 19 layers (16 conv, 5 pool 3 dense). Keras registers this as 26 layers.</p>
  
  <p><a href="https://en.wikipedia.org/wiki/AlexNet" rel="nofollow noreferrer">AlexNet</a> is considered to have 8 layers (5 conv, 3 pool, 3 dense).</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-01-28T13:07:19.627Z
Answers.LastActivityDate: 2020-01-28T13:07:19.627Z
Answers.UserDisplayName: Djib2011
Body: <p>I have a neural network model defined as below. How many layers exist there? Not sure which ones to count when we are asked about the number.</p>
  
  <pre><code>def create_model():
      channels = 3
      model = Sequential()
      model.add(Conv2D(32, kernel_size = (5, 5), activation='relu', input_shape=(IMAGE_SIZE, IMAGE_SIZE, channels)))
      model.add(MaxPooling2D(pool_size=(2,2)))
      model.add(BatchNormalization())
      model.add(Conv2D(64, kernel_size=(3,3), activation='relu'))
      model.add(MaxPooling2D(pool_size=(2,2)))
      model.add(BatchNormalization())
      model.add(Conv2D(128, kernel_size=(3,3), activation='relu'))
      model.add(MaxPooling2D(pool_size=(2,2)))
      model.add(BatchNormalization())
      model.add(Conv2D(256, kernel_size=(3,3), activation='relu'))
      model.add(MaxPooling2D(pool_size=(2,2)))
      model.add(BatchNormalization())
  
      model.add(Conv2D(64, kernel_size=(3,3), activation='relu'))
      model.add(MaxPooling2D(pool_size=(2,2)))
      model.add(BatchNormalization())
      model.add(Dropout(0.2))
      model.add(Flatten())
      model.add(Dense(256, activation='relu'))
      model.add(Dropout(0.2))
      model.add(Dense(128, activation='relu'))
      model.add(Dense(2, activation = 'softmax'))
  
      return model
  </code></pre>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2020-01-27T21:38:44.950Z
Comments.Text: I think the answer to this question is indeed subjective because some people may or not consider the activations, dropout and batch normalization separate layers. However, convolutions, pooling and dense are almost always considered standalone layers. You can also use `model.layers` to get the layers of the network, according to Keras.
Comments.UserDisplayName: nbro
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2020-01-27T22:11:18.683Z
Comments.Text: Exactly. My impression was I should just count CONV layers!
Comments.UserDisplayName: Mary
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2020-01-27T22:31:06.763Z
Comments.Text: If this is a homework question, I would provide some comments regarding this issue. By the way, you may also say that it may depend on the implementation. For example, in Keras, the activations may or not always be implemented as a separate layer (note that I actually don't know if that's the case or not)!
Comments.UserDisplayName: nbro
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2020-01-28T05:27:52.310Z
Comments.Text: If its a subjective question, how about you list all the layers i.e, 5 `Convolutional Layers`, 2 `Dense Layers` and so on. Another widely used practice is to define the network with blocks where a `Convolutional Block` will include `Conv2D`, `MaxPooling`, `BatchNormalizarion` and `Flatten`. However, if you want a number according to how the network/graph will be structured, then number of layers only includes `Conv` and `Dense`.
Comments.UserDisplayName: Sharan
Tags: <deep-learning><convolutional-neural-networks><python><training><deep-neural-networks>
Title: How many layers exists in my neural network?
UserDisplayName: Mary
ThreadText: # How many layers exists in my neural network?
  
  (Asked by: username_0 on 2020-01-27)
  
  I have a neural network model defined as below. How many layers exist there? Not sure which ones to count when we are asked about the number.
  
  ```
  def create_model():
      channels = 3
      model = Sequential()
      model.add(Conv2D(32, kernel_size = (5, 5), activation='relu', input_shape=(IMAGE_SIZE, IMAGE_SIZE, channels)))
      model.add(MaxPooling2D(pool_size=(2,2)))
      model.add(BatchNormalization())
      model.add(Conv2D(64, kernel_size=(3,3), activation='relu'))
      model.add(MaxPooling2D(pool_size=(2,2)))
      model.add(BatchNormalization())
      model.add(Conv2D(128, kernel_size=(3,3), activation='relu'))
      model.add(MaxPooling2D(pool_size=(2,2)))
      model.add(BatchNormalization())
      model.add(Conv2D(256, kernel_size=(3,3), activation='relu'))
      model.add(MaxPooling2D(pool_size=(2,2)))
      model.add(BatchNormalization())
  
      model.add(Conv2D(64, kernel_size=(3,3), activation='relu'))
      model.add(MaxPooling2D(pool_size=(2,2)))
      model.add(BatchNormalization())
      model.add(Dropout(0.2))
      model.add(Flatten())
      model.add(Dense(256, activation='relu'))
      model.add(Dropout(0.2))
      model.add(Dense(128, activation='relu'))
      model.add(Dense(2, activation = 'softmax'))
  
      return model
  
  ```
  
  **Tags:** deep-learning, convolutional-neural-networks, python, training, deep-neural-networks
  
  > **Comments:**
  > **username_1**: I think the answer to this question is indeed subjective because some people may or not consider the activations, dropout and batch normalization separate layers. However, convolutions, pooling and dense are almost always considered standalone layers. You can also use `model.layers` to get the layers of the network, according to Keras.  
  > **username_0**: Exactly. My impression was I should just count CONV layers!  
  > **username_1**: If this is a homework question, I would provide some comments regarding this issue. By the way, you may also say that it may depend on the implementation. For example, in Keras, the activations may or not always be implemented as a separate layer (note that I actually don't know if that's the case or not)!  
  > **username_2**: If its a subjective question, how about you list all the layers i.e, 5 `Convolutional Layers`, 2 `Dense Layers` and so on. Another widely used practice is to define the network with blocks where a `Convolutional Block` will include `Conv2D`, `MaxPooling`, `BatchNormalizarion` and `Flatten`. However, if you want a number according to how the network/graph will be structured, then number of layers only includes `Conv` and `Dense`.  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_3 on 2020-01-28. Score: 3
  
  **tl;dr** I'd say your model has **8 layers** (5 conv, 3 dense), however a lot of people count layers in other ways. From what I've seen this is by far the most **conventional** way for counting layers.
  
  ---
  
  ### Justification
  
  This is an interesting question because its quite subjective. In most cases only the convolutional and dense layers would count from your network. Bach norm, dropout and flatten are usually considered as operations to other layers rather than layers of their own (much like activation functions).
  
  Note: It is debatable whether or not pooling layers are considered to be layers (as they have no trainable parameters) but in most cases they are not considered to be so.
  
  Note 2: Batch norm, on the other hand, isn't usually considered a layer even though it has trainable parameters. Clearly the authors didn't [introduce it as a layer](https://arxiv.org/pdf/1502.03167.pdf), but as a way to normalize, shift and scale the inputs of a layer. This is apparent in some of the examples below which don't count batch norm as an actual layer.
  
  Note 3: Conventionally all networks are considered to have [at least] one input layer but it doesn't count as a layer.
  
  ---
  
  ### Examples
  
  Some examples that follow this reasoning when counting layers are the following. I'll also write the pooling layers in each, but they clearly aren't considered as layers be the authors. When available I'll also write the number of layers that keras registers from their [official implementations](https://keras.io/applications/):
  
  The [ResNet-50](https://external-content.duckduckgo.com/iu/?u=https%3A%2F%2Fcv-tricks.com%2Fwp-content%2Fuploads%2F2019%2F07%2FResNet50_architecture-1.png&f=1&nofb=1) architecture has 50 layers (49 conv, 2 pool, 1 dense), however keras registers it as 177 layers. ResNets also [use batch normalization after each convolution](https://arxiv.org/pdf/1512.03385.pdf) (so 49 batch norms in total), but clearly don't count them as layers.
  
  The [Resnet-34](https://external-content.duckduckgo.com/iu/?u=https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F1600%2F1*4tlPOipWjcwIoNUlQ6IWFQ.png&f=1&nofb=1) has 34 layers (33 conv, 2 pool, 1 dense). Like the previous, this also uses batch norm but doesn't count it.
  
  [VGG-19](https://external-content.duckduckgo.com/iu/?u=https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F1600%2F1*4tlPOipWjcwIoNUlQ6IWFQ.png&f=1&nofb=1) has 19 layers (16 conv, 5 pool 3 dense). Keras registers this as 26 layers.
  
  [AlexNet](https://en.wikipedia.org/wiki/AlexNet) is considered to have 8 layers (5 conv, 3 pool, 3 dense).
  
  
  ---
  
  