DropConnect Linear Layer Forward Pass
Role: Machine Learning Engineer
Category: Machine Learning
Difficulty: medium
Interview Round: Other
Implement DropConnect for a fully connected layer in NumPy. Write two functions:
- `dropconnect_mask(shape, p, seed)` returns a mask that drops each weight independently with probability `p` and scales the kept weights by `1/(1-p)`.
- `dropconnect_linear(x, W, b, p, training, seed)` returns `x @ W_eff.T + b` as a float64 array of shape `(n, d_out)`.
The output must be reproducible, so follow these rules exactly:
- Mask: draw `u = np.random.default_rng(seed).random(shape)` and keep an entry if and only if `u >= p`. Every mask entry is either `0` or `1/(1-p)`.
- Training (`training=True`): `W_eff = W * dropconnect_mask(W.shape, p, seed)`. One mask is shared across all rows of the batch.
- Eval (`training=False`): `W_eff = W`.
- The bias `b` is never masked.
- Both functions raise `ValueError` unless `0 <= p < 1`.
Example:
```python
W = np.arange(12, dtype=float).reshape(3, 4)
b = np.array([0.5, -1.0, 2.0])
x = np.zeros((2, 4))
dropconnect_linear(x, W, b, 0.9, True, 1)
# -> [[0.5, -1.0, 2.0], [0.5, -1.0, 2.0]] (x is all zeros, so only the unmasked bias remains)
x2 = np.ones((1, 4))
dropconnect_linear(x2, W, b, 0.0, True, 99)
# -> [[6.5, 21.0, 40.0]] (with p = 0 every weight is kept with scale 1, so this equals x2 @ W.T + b)
dropconnect_linear(x2, W, b, 0.5, False, 7)
# -> [[6.5, 21.0, 40.0]] (eval mode uses W unchanged)
```
```hint Where the mask lives
The mask has the shape of `W`, so it does not depend on the batch size. Build `W_eff` once, then do a single matrix product.
```
```hint Check the scaling
Work out the expected value of one mask entry after scaling. If it is not 1, training and eval outputs disagree on average.
```
### Constraints & Assumptions
- `x` has shape `(n, d_in)`, `W` has shape `(d_out, d_in)`, `b` has shape `(d_out,)`.
- `seed` is an integer and `shape` is a tuple.
- The result is a float64 array of shape `(n, d_out)`.
### What a Strong Answer Covers
- Builds the mask from the seeded generator exactly as specified, with dropped entries at 0 and kept entries at `1/(1-p)`.
- Applies one mask to `W` in training, uses `W` unchanged in eval, and never masks the bias.
- Validates `p` in both functions, including eval mode where no mask is drawn, and returns float64 output of the right shape.
- Explains why inverted scaling makes the expected training output of a linear layer equal the eval output.
- States the cost: one matrix product plus a mask the size of `W`.
### Follow-up Questions
- How does DropConnect differ from standard dropout in what it masks?
- How would you sample a separate mask per example, and what does it cost?
- Is the eval-time output with full weights exactly the mean of the training outputs once a nonlinearity follows the layer?
Overview: Implement a DropConnect forward pass for a fully connected layer in NumPy, with a seeded weight mask, inverted scaling, and separate training and eval behavior. It tests how weight-level masking differs from dropout and why the scaling keeps the expected output of the layer unchanged.