# C4\_W3\_A2: idea behind tf.math.reduce\_max(mask, ...)

**URL:** <https://community.deeplearning.ai/t/c4-w3-a2-idea-behind-tf-math-reduce-max-mask/864494>\
**Category:** Convolutional Neural Networks\
**Tags:** week-module-3\
**Created:** [August 1, 2025, 6:14am UTC](https://community.deeplearning.ai/t/c4-w3-a2-idea-behind-tf-math-reduce-max-mask/864494 "2025-08-01T06:14:46Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![AKazak](https://avatars.discourse-cdn.com/v4/letter/a/91b2a8/32.png) [@AKazak](https://community.deeplearning.ai/u/AKazak)\
**Post date:** [August 1, 2025, 6:14am UTC](https://community.deeplearning.ai/t/c4-w3-a2-idea-behind-tf-math-reduce-max-mask/864494/1 "2025-08-01T06:14:46Z")

</div>

Greetings!

What is the idea behind the highlighted line of code below?  
I see that mask image also has 3 channels (RGB), but why do we use the max value of all the 3 original channels?

Thank you.

 ![image](https://global.discourse-cdn.com/dlai/original/3X/d/a/dabc64c5f1e0392b670fac9bc0a8985f416342f5.png)

---

<div class="post-metadata">

**Author:** ![gent.spah](https://sea1.discourse-cdn.com/dlai/user_avatar/community.deeplearning.ai/gent.spah/32/136423_2.png) [@gent.spah](https://community.deeplearning.ai/u/gent.spah)\
**Post date:** [August 1, 2025, 8:17am UTC](https://community.deeplearning.ai/t/c4-w3-a2-idea-behind-tf-math-reduce-max-mask/864494/2 "2025-08-01T08:17:10Z")

</div>

This line of code takes the maximum mask value among all 3 RGB channels, to obtain a mask (greyscale) with 1 channel.

---

<div class="post-metadata">

**Author:** ![AKazak](https://avatars.discourse-cdn.com/v4/letter/a/91b2a8/32.png) [@AKazak](https://community.deeplearning.ai/u/AKazak)\
**Post date:** [August 1, 2025, 8:35am UTC](https://community.deeplearning.ai/t/c4-w3-a2-idea-behind-tf-math-reduce-max-mask/864494/3 "2025-08-01T08:35:39Z")

</div>

I see.

But will this method mail for the following two pixels of the image mask?  
[255, 0, 0] and [255, 1, 2] # [R,G,B]

As I understood, these two pixels originally corresponding to the different classes will be recognized as a single class. Right?

---

<div class="post-metadata">

**Author:** ![gent.spah](https://sea1.discourse-cdn.com/dlai/user_avatar/community.deeplearning.ai/gent.spah/32/136423_2.png) [@gent.spah](https://community.deeplearning.ai/u/gent.spah)\
**Post date:** [August 1, 2025, 8:54am UTC](https://community.deeplearning.ai/t/c4-w3-a2-idea-behind-tf-math-reduce-max-mask/864494/4 "2025-08-01T08:54:29Z")

</div>

I don’t remember the assignment now, but at that code line, you will have a mask result of 255 for both!

---

<div class="post-metadata">

**Author:** ![AKazak](https://avatars.discourse-cdn.com/v4/letter/a/91b2a8/32.png) [@AKazak](https://community.deeplearning.ai/u/AKazak)\
**Post date:** [August 1, 2025, 9:14am UTC](https://community.deeplearning.ai/t/c4-w3-a2-idea-behind-tf-math-reduce-max-mask/864494/5 "2025-08-01T09:14:53Z")

</div>

I did.  
However I still haven’t got the idea…

---

<div class="post-metadata">

**Author:** ![Girijesh](https://sea1.discourse-cdn.com/dlai/user_avatar/community.deeplearning.ai/girijesh/32/37283_2.png) [@Girijesh](https://community.deeplearning.ai/u/Girijesh)\
**Post date:** [August 1, 2025, 3:50pm UTC](https://community.deeplearning.ai/t/c4-w3-a2-idea-behind-tf-math-reduce-max-mask/864494/6 "2025-08-01T15:50:49Z")

</div>

> img = tf.io.read\_file(image\_path) # Reads the image file as bytes  
> img = tf.image.decode\_png(img, channels=3) # Decodes PNG to a tensor with 3 color channels (RGB)  
> img = tf.image.convert\_image\_dtype(img, tf.float32) # Converts to float32 and scales values to [0, 1]

–  
_Keep Learning AI with DeepLearning.AI_ - Girijesh

---

<div class="post-metadata">

**Author:** ![TMosh](https://sea1.discourse-cdn.com/dlai/user_avatar/community.deeplearning.ai/tmosh/32/9321_2.png) [@TMosh](https://community.deeplearning.ai/u/TMosh)\
**Post date:** [August 1, 2025, 4:33pm UTC](https://community.deeplearning.ai/t/c4-w3-a2-idea-behind-tf-math-reduce-max-mask/864494/7 "2025-08-01T16:33:42Z")

</div>

> [@AKazak](#):
>
> However I still haven’t got the idea…

I recommend you try some experiments to see what that line of code does.

---

<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:** [August 5, 2025, 1:07am UTC](https://community.deeplearning.ai/t/c4-w3-a2-idea-behind-tf-math-reduce-max-mask/864494/8 "2025-08-05T01:07:56Z")

</div>

Hello, @AKazak,

Here is how I looked into that:

 ![image](https://global.discourse-cdn.com/dlai/original/3X/0/4/04b38a20decc12c62eb33fa152a380cf73405078.png)

I added the code cell above right after the notebook’s 4th code cell which is sampling the unmasked and masked version of an image in the dataset.

First, if you check, you will find that each `mask` has 4 channels and if you list all unique values for each channel, you will see that the 2nd to the 4th channels are not informative because they have the same values across all pixels.

Then you can look at the part of the code you shared in this thread, after your own investigation, you would find that the 4th channel is simply ignored, leaving only the first 3 for our consideration on what the `reduce_max` was doing.

If, for all masks, the 2nd and the 3rd channel were always zeros, then taking the `reduce_max` is no different from ignoring them, and this makes sense because it only leaves the first channel which represents the class labels.

However, are the 2nd and the 3rd channels always zeros? That will be for you to confirm, because I am only demonstrating how I would look into that, so it is just a starting 😉 😉

Keep learning, and cheers,  
Raymond

---

<div class="post-metadata">

**Author:** ![AKazak](https://avatars.discourse-cdn.com/v4/letter/a/91b2a8/32.png) [@AKazak](https://community.deeplearning.ai/u/AKazak)\
**Post date:** [August 5, 2025, 2:19pm UTC](https://community.deeplearning.ai/t/c4-w3-a2-idea-behind-tf-math-reduce-max-mask/864494/9 "2025-08-05T14:19:50Z")

</div>

Thank you for pushing this forward!

This is exactly what I notice. So we have out 4 channels only 1 channel containing the valuable information. If this is the case, then what is the point of running **reduce\_max**?

Taking just a 0th channel seems to be more logical in this case.

---

<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:** [August 6, 2025, 1:08am UTC](https://community.deeplearning.ai/t/c4-w3-a2-idea-behind-tf-math-reduce-max-mask/864494/10 "2025-08-06T01:08:03Z")

</div>

If it is always that the 0th channel contains the labels, then we can, as you said, just index it. These are just alternatives for achieving the same goal ;), although I prefer indexing it more, too.

Cheers,  
Raymond

---

<div class="post-metadata">

**Author:** ![AKazak](https://avatars.discourse-cdn.com/v4/letter/a/91b2a8/32.png) [@AKazak](https://community.deeplearning.ai/u/AKazak)\
**Post date:** [August 6, 2025, 3:52pm UTC](https://community.deeplearning.ai/t/c4-w3-a2-idea-behind-tf-math-reduce-max-mask/864494/11 "2025-08-06T15:52:52Z")

</div>

I think that , in general case, among all the alternatives one should give a preference to the most simple and straightforward one.

---

<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:** [August 6, 2025, 7:34pm UTC](https://community.deeplearning.ai/t/c4-w3-a2-idea-behind-tf-math-reduce-max-mask/864494/12 "2025-08-06T19:34:39Z")

</div>

Yes, I agree with you!
