# Course 4, week 3 Assigment 2, exercise 1

**URL:** <https://dlai.staged-by-discourse.com/t/course-4-week-3-assigment-2-exercise-1/345659>\
**Category:** Convolutional Neural Networks\
**Tags:** coursera-platform\
**Created:** [May 28, 2023, 7:50pm UTC](https://dlai.staged-by-discourse.com/t/course-4-week-3-assigment-2-exercise-1/345659 "2023-05-28T19:50:51Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Anja\_Petrovic](https://avatars.discourse-cdn.com/v4/letter/a/b782af/32.png) [@Anja\_Petrovic](https://dlai.staged-by-discourse.com/u/Anja_Petrovic)\
**Post date:** [May 28, 2023, 7:50pm UTC](https://dlai.staged-by-discourse.com/t/course-4-week-3-assigment-2-exercise-1/345659/1 "2023-05-28T19:50:51Z")

</div>

Hello,

I am a bit confused with this exercise:  
_{Moderator Edit: Solution Code Removed}_  
I dont get why we initialize twice the same convolutional layer.

Also I dont get how we initialize MaxPooling2D and dropout? why are we giving conv as an argument here? what is class and what is object there?  
what is actually happening when we write this: next\_layer = MaxPooling2D(pool\_size=(2,2))(conv)

---

<div class="post-metadata">

**Author:** ![paulinpaloalto](https://sea1.discourse-cdn.com/dlaistaging/user_avatar/dlai.staged-by-discourse.com/paulinpaloalto/32/187_2.png) [@paulinpaloalto](https://dlai.staged-by-discourse.com/u/paulinpaloalto)\
**Post date:** [May 29, 2023, 4:16am UTC](https://dlai.staged-by-discourse.com/t/course-4-week-3-assigment-2-exercise-1/345659/2 "2023-05-29T04:16:46Z")

</div>

Have another look at the architecture of the network that we are implementing here. When you cascade two conv layers back to back, just because they have the same hyperparameters does not mean that it doesn’t make sense or that it’s a NOP. You are applying another conv layer to the output you get from the first conv layer.

Then we optionally have a dropout layer and also optionally a max pooling layer.

> [@Anja\_Petrovic](#):
>
> what is actually happening when we write this: next\_layer = MaxPooling2D(pool\_size=(2,2))(conv)

The point is that Keras “Layer” functions (subclasses of `Layer`) take a set of hyperparameters and then return you an instance of the actual function, which you then invoke with a tensor as input and it produces a tensor as output. In this instance the result of this call:

`MaxPooling2D(pool_size=(2,2))`

is a function. Then you invoke that function with the input tensor `conv` and it returns an output tensor.

Please see [this thread](http://dlai.staged-by-discourse.com/t/tips-for-troubles-with-sequential-and-functional-api-syntax/52202) for a good explanation of how to use both the Keras Sequential and Functional models.

---

<div class="post-metadata">

**Author:** ![saifkhanengr](https://sea1.discourse-cdn.com/dlaistaging/user_avatar/dlai.staged-by-discourse.com/saifkhanengr/32/152858_2.png) [@saifkhanengr](https://dlai.staged-by-discourse.com/u/saifkhanengr)\
**Post date:** [May 29, 2023, 5:07am UTC](https://dlai.staged-by-discourse.com/t/course-4-week-3-assigment-2-exercise-1/345659/3 "2023-05-29T05:07:50Z")

</div>

Hello @Anja_Petrovic!

I am deleting the image you posted as it has solution code and sharing it is not allowed. Also, you posted this in the General Category but it belongs to DLS course 4, right? Consider moving it to the correct category. Read [this](http://dlai.staged-by-discourse.com/t/faq-frequently-asked-questions-for-all-dls-courses/74#i-accidentally-posted-my-topic-in-the-wrong-category-how-can-i-move-it-to-the-right-category-16) on how to do that.

Best,  
Saif.

---

<div class="post-metadata">

**Author:** ![ai\_curious](https://sea1.discourse-cdn.com/dlaistaging/user_avatar/dlai.staged-by-discourse.com/ai_curious/32/128003_2.png) [@ai\_curious](https://dlai.staged-by-discourse.com/u/ai_curious)\
**Post date:** [May 29, 2023, 9:52am UTC](https://dlai.staged-by-discourse.com/t/course-4-week-3-assigment-2-exercise-1/345659/4 "2023-05-29T09:52:30Z")

</div>

> [@Anja\_Petrovic](#):
>
> what is actually happening when we write this: next\_layer = MaxPooling2D(pool\_size=(2,2))(conv)

As you go further on the deep learning journey, it probably makes sense at some point to begin answering questions like this by looking directly at the source code, which is available on github. Specifically, [https://github.com/keras-team/keras/blob/v2.12.0/keras/layers/pooling/max\_pooling2d.py#L26](https://github.com/keras-team/keras/blob/v2.12.0/keras/layers/pooling/max_pooling2d.py#L26)

Here is how I would decompose and answer your question using that reference material.

`MaxPooling2D` is a Python class, a defined type. It extends the class `Pooling2D`, which in turn extends the class `Layer`. MaPooling2D is-a Pooling2D is-a Layer.

Of interest to us for this discussion, `MaxPooling2D` has an `init()` function, and a `call()` function in its public API.

`MaxPooling2D(pool_size=(2,2))` invokes the `init()` function, which looks like this

```auto
    def __init__ (
        self,
        pool_size=(2, 2),
        strides=None,
        padding="valid",
        data_format=None,
        **kwargs
    ):

```

Here the `pool_size` parameter is being passed in explicitly, even though it is the same as the default, which is `(2, 2)`. The `init()` function immediately calls the base `Pooling2D` class `init()` function which immediately calls the base `Layer` class `init()` function (and so on up the inheritance hierarchy). The result of this chain of _init_ calls is a fully specified and initialized object instance. You can think of it as doing this:

```auto
max_pool_2d_instance = tf.keras.layers.MaxPooling2D(pool_size=(2, 2))

```

Now that you have an object instance, you can invoke its `call()` function. Here’s what the source of that looks like…

```auto
    def call(self, inputs):
        if self.data_format == "channels_last":
            pool_shape = (1,) + self.pool_size + (1,)
            strides = (1,) + self.strides + (1,)
        else:
            pool_shape = (1, 1) + self.pool_size
            strides = (1, 1) + self.strides
        outputs = self.pool_function(
            inputs,
            ksize=pool_shape,
            strides=strides,
            padding=self.padding.upper(),
            data_format=conv_utils.convert_data_format(self.data_format, 4),
        )
        return outputs

```

tl;dr is you invoke the `call()` function and pass the `inputs`, getting back the `outputs`. In Python …

```auto
max_pooling_layer_inputs = assume_this_was_defined_already

max_pooling_outputs = max_pool_2d_instance(max_pooling_layer_inputs)

```

It turns out that Python allows you write expressions tersely, and combine multiple steps into a single expression. In this case, you can combine the steps for invoking `init()` and `call()` into a single line of code. And since in this exercise the `max_pooling_inputs` in none other than the outputs from the conv layer, you can write the entire thing as …

```auto
max_pooling_outputs = MaxPooling2D(pool_size = (2, 2))(conv)

```

For convenience of writing (though not of reading) you often see Keras Functional models using the single variable `x` for both the input and the output variables. Think of that as `x` on the left hand side of the assignment is an output, which is then used on the right hand side of the assignment as an input to a subsequent layer.

```auto
conv_layer_output = Conv2D.(...)(conv_layer_input)
pooling_layer_output = MaxPooling2D(...)(conv_layer_output)
dropout_later_output = Dropout(...)(pooling_layer_output)

```

is often written instead as

```auto
x = InputLayer(...)
x = Conv2D(...)(x)
x = MaxPooling2D(...)(x)
x = Dropout(...)(x)

```

HTH
