# Inserting noise section

**URL:** <https://community.deeplearning.ai/t/inserting-noise-section/220784>\
**Category:** Build Better Generative Adversarial Networks\
**Tags:** week-module-3\
**Created:** [October 21, 2022, 8:35pm UTC](https://community.deeplearning.ai/t/inserting-noise-section/220784 "2022-10-21T20:35:14Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![Joshua\_Kessler](https://avatars.discourse-cdn.com/v4/letter/j/f0a364/32.png) [@Joshua\_Kessler](https://community.deeplearning.ai/u/Joshua_Kessler)\
**Post date:** [October 21, 2022, 8:35pm UTC](https://community.deeplearning.ai/t/inserting-noise-section/220784/1 "2022-10-21T20:35:14Z")

</div>

is there possibly an error for the noise insertion section of the style gan components assignment. I in one section of the grader all of the checks are greater than except one.

assert torch.abs((inject\_noise(fake\_images) - fake\_images).std(0)).mean() \> 1e-4  
assert torch.abs((inject\_noise(fake\_images) - fake\_images).std(1)).mean() \< 1e-4  
assert torch.abs((inject\_noise(fake\_images) - fake\_images).std(2)).mean() \> 1e-4  
assert torch.abs((inject\_noise(fake\_images) - fake\_images).std(3)).mean() \> 1e-4

# Check that the per-channel change is roughly normal

---

<div class="post-metadata">

**Author:** ![paulinpaloalto](https://sea1.discourse-cdn.com/dlai/user_avatar/community.deeplearning.ai/paulinpaloalto/32/187_2.png) [@paulinpaloalto](https://community.deeplearning.ai/u/paulinpaloalto)\
**Post date:** [October 21, 2022, 8:51pm UTC](https://community.deeplearning.ai/t/inserting-noise-section/220784/2 "2022-10-21T20:51:02Z")

</div>

It’s been a while since I looked at this assignment, but I think the point is that not all 4 of the dimensions are handled the same way there. Look at how dimension 1 is treated in noise injection: the size of that dimension is 1, but the other dimensions are all non-trivial. What effect will that have in the resulting behavior? 🤓

---

<div class="post-metadata">

**Author:** ![Joshua\_Kessler](https://avatars.discourse-cdn.com/v4/letter/j/f0a364/32.png) [@Joshua\_Kessler](https://community.deeplearning.ai/u/Joshua_Kessler)\
**Post date:** [October 21, 2022, 9:04pm UTC](https://community.deeplearning.ai/t/inserting-noise-section/220784/3 "2022-10-21T21:04:41Z")

</div>

ok, thanks. At least know what direction to look in now. Thanks!

---

<div class="post-metadata">

**Author:** ![Joshua\_Kessler](https://avatars.discourse-cdn.com/v4/letter/j/f0a364/32.png) [@Joshua\_Kessler](https://community.deeplearning.ai/u/Joshua_Kessler)\
**Post date:** [October 21, 2022, 9:11pm UTC](https://community.deeplearning.ai/t/inserting-noise-section/220784/4 "2022-10-21T21:11:45Z")

</div>

do you think randn is the right function to use to set the weights?

torch.randn([1, channels, 1, 1])

---

<div class="post-metadata">

**Author:** ![paulinpaloalto](https://sea1.discourse-cdn.com/dlai/user_avatar/community.deeplearning.ai/paulinpaloalto/32/187_2.png) [@paulinpaloalto](https://community.deeplearning.ai/u/paulinpaloalto)\
**Post date:** [October 21, 2022, 9:27pm UTC](https://community.deeplearning.ai/t/inserting-noise-section/220784/5 "2022-10-21T21:27:59Z")

</div>

Yes, that’s the way I did it. Mind you, I worked this assignment \> 2 years ago, so I don’t know if they’ve subsequently updated the assignment in any way.

---

<div class="post-metadata">

**Author:** ![paulinpaloalto](https://sea1.discourse-cdn.com/dlai/user_avatar/community.deeplearning.ai/paulinpaloalto/32/187_2.png) [@paulinpaloalto](https://community.deeplearning.ai/u/paulinpaloalto)\
**Post date:** [October 21, 2022, 9:45pm UTC](https://community.deeplearning.ai/t/inserting-noise-section/220784/6 "2022-10-21T21:45:06Z")

</div>

I just got the latest version of the notebook and there is only one minor change since 2 years ago in the template code that they provide. So I think that weight initialization should still be correct.

---

<div class="post-metadata">

**Author:** ![Joshua\_Kessler](https://avatars.discourse-cdn.com/v4/letter/j/f0a364/32.png) [@Joshua\_Kessler](https://community.deeplearning.ai/u/Joshua_Kessler)\
**Post date:** [October 21, 2022, 10:07pm UTC](https://community.deeplearning.ai/t/inserting-noise-section/220784/7 "2022-10-21T22:07:18Z")

</div>

ok, thanks again. There are only two coding blocks in this section so I have no clue what I could be missing. Using the image shape as the noise shape.

---

<div class="post-metadata">

**Author:** ![paulinpaloalto](https://sea1.discourse-cdn.com/dlai/user_avatar/community.deeplearning.ai/paulinpaloalto/32/187_2.png) [@paulinpaloalto](https://community.deeplearning.ai/u/paulinpaloalto)\
**Post date:** [October 21, 2022, 11:17pm UTC](https://community.deeplearning.ai/t/inserting-noise-section/220784/8 "2022-10-21T23:17:59Z")

</div>

What error are you getting?

Note that the noise shape is not the same as the image shape, right? That was what I was referring to earlier about why the test for dimension 1 is different. Oh, ok, that was part of the code you need to write, so that is probably where your bug is. The shape of the noise is the same as the shape of the image in every dimension except dimension 1. Dimension 1 has only 1 element for noise.

---

<div class="post-metadata">

**Author:** ![Joshua\_Kessler](https://avatars.discourse-cdn.com/v4/letter/j/f0a364/32.png) [@Joshua\_Kessler](https://community.deeplearning.ai/u/Joshua_Kessler)\
**Post date:** [October 22, 2022, 12:16am UTC](https://community.deeplearning.ai/t/inserting-noise-section/220784/9 "2022-10-22T00:16:32Z")

</div>

That’s what I think I’m trying, but still failing at least one of the tests;

noise\_shape = (1, image.shape[1], image.shape[2], image.shape[3])

---

<div class="post-metadata">

**Author:** ![paulinpaloalto](https://sea1.discourse-cdn.com/dlai/user_avatar/community.deeplearning.ai/paulinpaloalto/32/187_2.png) [@paulinpaloalto](https://community.deeplearning.ai/u/paulinpaloalto)\
**Post date:** [October 22, 2022, 12:21am UTC](https://community.deeplearning.ai/t/inserting-noise-section/220784/10 "2022-10-22T00:21:34Z")

</div>

But indexing in python is 0-based, so dimension 1 is the second dimension, right?

---

<div class="post-metadata">

**Author:** ![paulinpaloalto](https://sea1.discourse-cdn.com/dlai/user_avatar/community.deeplearning.ai/paulinpaloalto/32/187_2.png) [@paulinpaloalto](https://community.deeplearning.ai/u/paulinpaloalto)\
**Post date:** [October 22, 2022, 12:25am UTC](https://community.deeplearning.ai/t/inserting-noise-section/220784/11 "2022-10-22T00:25:56Z")

</div>

Or to put it another way, which dimension is the “channel” dimension? It is index 1, which is the second dimension.

---

<div class="post-metadata">

**Author:** ![Joshua\_Kessler](https://avatars.discourse-cdn.com/v4/letter/j/f0a364/32.png) [@Joshua\_Kessler](https://community.deeplearning.ai/u/Joshua_Kessler)\
**Post date:** [October 22, 2022, 12:28am UTC](https://community.deeplearning.ai/t/inserting-noise-section/220784/12 "2022-10-22T00:28:55Z")

</div>

oh wow, I tried every dimension being one but that one. I was sure that one had to be the channel number, but it makes sense now. Thank you !

---

<div class="post-metadata">

**Author:** ![MGriff](https://avatars.discourse-cdn.com/v4/letter/m/ad7895/32.png) [@MGriff](https://community.deeplearning.ai/u/MGriff)\
**Post date:** [March 26, 2024, 9:08pm UTC](https://community.deeplearning.ai/t/inserting-noise-section/220784/13 "2024-03-26T21:08:30Z")

</div>

I used the above function for the weights.  
I also indexed the dimensions 1-3 of the input image, following 1 for the n\_samples.  
The preceding checks are fine.  
I have also restarted the kernel.  
However it fails at the first assert of the block:  
assert torch.abs((inject\_noise(fake\_images) - fake\_images).std(0)).mean() \> 1e-4

Can anyone advise?

Many thanks,  
Mark

---

<div class="post-metadata">

**Author:** ![paulinpaloalto](https://sea1.discourse-cdn.com/dlai/user_avatar/community.deeplearning.ai/paulinpaloalto/32/187_2.png) [@paulinpaloalto](https://community.deeplearning.ai/u/paulinpaloalto)\
**Post date:** [March 27, 2024, 2:30am UTC](https://community.deeplearning.ai/t/inserting-noise-section/220784/14 "2024-03-27T02:30:35Z")

</div>

> [@MGriff](#):
>
> Can anyone advise?

Have you actually read through this thread? I’m guessing your `noise_shape` looks like the [erroneous one shown earlier](https://community.deeplearning.ai/t/inserting-noise-section/220784/9) …

---

<div class="post-metadata">

**Author:** ![MGriff](https://avatars.discourse-cdn.com/v4/letter/m/ad7895/32.png) [@MGriff](https://community.deeplearning.ai/u/MGriff)\
**Post date:** [March 27, 2024, 11:56am UTC](https://community.deeplearning.ai/t/inserting-noise-section/220784/15 "2024-03-27T11:56:44Z")

</div>

Sorry you are absolutely right. Thanks. The channels should be set to 1 for the noise, so that each channel is calculated differently, as people have said. The multiplication of the noise vector and the weights should be of the same size as the image. This was in the instructions, which make it very clear that this is what we are doing.

---

<div class="post-metadata">

**Author:** ![Dennis\_Sinitsky](https://sea1.discourse-cdn.com/dlai/user_avatar/community.deeplearning.ai/dennis_sinitsky/32/77331_2.png) [@Dennis\_Sinitsky](https://community.deeplearning.ai/u/Dennis_Sinitsky)\
**Post date:** [March 28, 2024, 3:29pm UTC](https://community.deeplearning.ai/t/inserting-noise-section/220784/16 "2024-03-28T15:29:48Z")

</div>

Haha, it’s funny that everyone gets stuck on the same things! So useful to read older posts! Saves a lot of debugging time and sifting through print statements. 😄
