Implement a Gaussian noise injection layer in NumPy

Quick Overview

Implement an additive Gaussian noise layer in NumPy with a seeded generator, a training mode that adds scaled standard-normal noise and an eval mode that returns the input. Tests reproducible random streams, input validation, avoiding in-place mutation, and why the backward pass is the identity.

Implement a Gaussian noise injection layer in NumPy

Role: Machine Learning Engineer

Category: Machine Learning

Difficulty: easy

Interview Round: Other

Implement `GaussianNoise(std, seed)` in NumPy. In training, `forward(x, training=True)` returns `x + std * noise` with standard-normal noise from a seeded generator. In eval mode it is the identity, and `backward(grad_out)` passes the gradient through unchanged. Requirements: - Create one `np.random.default_rng(seed)` in `__init__` and draw noise with `rng.standard_normal(size=x.shape)`. - Successive training calls continue the same stream. Eval calls consume no draws. - A negative or non-finite `std` raises `ValueError` in the constructor. With `std=0` the layer returns the input unchanged. - Never mutate `x` or `grad_out`. Return new `float64` arrays of the same shape. Example: ```python layer = GaussianNoise(std=0.5, seed=7) out = layer.forward(x, training=True) # == x + 0.5 * np.random.default_rng(7).standard_normal(size=x.shape) layer.forward(x, training=False) # == x ``` ```hint Check every return path Two paths hand back data without doing any arithmetic: eval mode and `backward`. Look at what `np.asarray` returns when the input is already a `float64` array, and ask whether the caller could end up holding its own array. ``` ### What a Strong Answer Covers - A single generator created once in the constructor, so the noise stream is reproducible, continues across training calls and is untouched by eval calls. - Validation of `std` at construction time that catches negative values, NaN and infinity. - Return paths that always produce a new `float64` array of the input's shape, with no in-place change to `x` or `grad_out`. - Why additive noise that does not depend on `x` gives an identity gradient, so `backward` needs nothing cached from `forward`. ### Follow-up Questions - Why is `backward` the identity even though noise was added? - How would you make the noise multiplicative instead of additive, and what would `backward` need then? - For a linear model trained with squared error, how does adding Gaussian noise to the inputs relate to an L2 penalty on the weights?

Overview: Implement an additive Gaussian noise layer in NumPy with a seeded generator, a training mode that adds scaled standard-normal noise and an eval mode that returns the input. Tests reproducible random streams, input validation, avoiding in-place mutation, and why the backward pass is the identity.

PracHub logo
General
Nov 25, 2026
easyMachine Learning EngineerOtherMachine Learning
0
0

Implement GaussianNoise(std, seed) in NumPy. In training, forward(x, training=True) returns x + std * noise with standard-normal noise from a seeded generator. In eval mode it is the identity, and backward(grad_out) passes the gradient through unchanged.

Requirements:

  • Create one np.random.default_rng(seed) in __init__ and draw noise with rng.standard_normal(size=x.shape) .
  • Successive training calls continue the same stream. Eval calls consume no draws.
  • A negative or non-finite std raises ValueError in the constructor. With std=0 the layer returns the input unchanged.
  • Never mutate x or grad_out . Return new float64 arrays of the same shape.

Example:

layer = GaussianNoise(std=0.5, seed=7)
out = layer.forward(x, training=True)
# == x + 0.5 * np.random.default_rng(7).standard_normal(size=x.shape)
layer.forward(x, training=False)  # == x

What a Strong Answer Covers Guidance

  • A single generator created once in the constructor, so the noise stream is reproducible, continues across training calls and is untouched by eval calls.
  • Validation of std at construction time that catches negative values, NaN and infinity.
  • Return paths that always produce a new float64 array of the input's shape, with no in-place change to x or grad_out .
  • Why additive noise that does not depend on x gives an identity gradient, so backward needs nothing cached from forward .

Follow-up Questions Guidance

  • Why is backward the identity even though noise was added?
  • How would you make the noise multiplicative instead of additive, and what would backward need then?
  • For a linear model trained with squared error, how does adding Gaussian noise to the inputs relate to an L2 penalty on the weights?