<p>The addition of noise affects the backward pass through that layer. For example, for quantization layers, people typically use straight-through estimator (which is basically just gradient clipping if I remember correctly).</p>
<p>Please note that when clipping gradients they should be done when passing through noise layers. This paper shows how to clip the gradient and then adds noise. <em>See section 3.1</em> <strong>Differentially Private SGD Algorithm</strong>, &quot;<em><strong>Algorithm 1</strong></em>&quot; on <em>page 3</em>: <a href="https://arxiv.org/pdf/1607.00133.pdf" rel="nofollow noreferrer">https://arxiv.org/pdf/1607.00133.pdf</a></p>
<p>But to answer your question, this is the code you will need to add noise:</p>
<pre><code>class GaussianNoise(nn.Module):
&quot;&quot;&quot;Gaussian noise regularizer.

Args:
    sigma (float, optional): relative standard deviation used to generate the
        noise. Relative means that it will be multiplied by the magnitude of
        the value your are adding the noise to. This means that sigma can be
        the same regardless of the scale of the vector.
    is_relative_detach (bool, optional): whether to detach the variable before
        computing the scale of the noise. If `False` then the scale of the noise
        won't be seen as a constant but something to optimize: this will bias the
        network to generate vectors with smaller values.
&quot;&quot;&quot;
def __init__(self, sigma=0.1, is_relative_detach=True):
    super().__init__()
    self.sigma = sigma
    self.is_relative_detach = is_relative_detach
    self.register_buffer('noise', torch.tensor(0))

def forward(self, x):
    if self.training and self.sigma != 0:
        scale = self.sigma * x.detach() if self.is_relative_detach else self.sigma * x
        sampled_noise = self.noise.expand(*x.size()).float().normal_() * scale
        x = x + sampled_noise
    return x 
</code></pre>
<p>*<em>You might also want to scroll through this thread as well:</em> <a href="https://discuss.pytorch.org/t/how-to-add-noise-to-mnist-dataset-when-using-pytorch/59745/17" rel="nofollow noreferrer">https://discuss.pytorch.org/t/how-to-add-noise-to-mnist-dataset-when-using-pytorch/59745/17</a></p>
a method to add noise when apply maddpg to particalenv
<p>accroding to paper by OpenAI(<a href="https://arxiv.org/pdf/1706.02275.pdf" rel="nofollow noreferrer">https://arxiv.org/pdf/1706.02275.pdf</a>),we should add noise to policy to ensure exploration. While in an example code, there is a method to add noise:</p>
<p><code>u = torch.rand_like(model_out)</code></p>
<p><code>policy = F.softmax(model_out - torch.log(-torch.log(u)), dim=-1)</code></p>
<p>It works very well with simple_spread env,while when I simply add a scaler of gaussian noise to  model_out, the time of covergence become quite long.
How it works?</p>