Deep-ML | Rectifier Networks: One Training Step with an L1 Penalty on the ActivationsRectifier Networks: One Training Step with an L1 Penalty on the Activations
Rectifier Networks: One Training Step with an L1 Penalty on the Activations
```python
def r(o):
q = lambda v: round(v, 6)
return (q(o['loss']), q(o['nll']), q(o['penalty']), [round(s, 4) for s in o['sparsity']],
[[[q(v) for v in row] for row in W] for W in o['weights']], [[q(v) for v in b] for b in o['biases']])
Ws = [[[1.0, -1.0], [0.5, 0.5], [-1.0, 0.25], [0.3, 0.8]], [[1.0, -1.0, 0.5, 0.2], [-0.5, 0.4, 1.0, -1.0]]]
bs = [[0.1, 0.0, -0.2, 0.05], [0.0, 0.1]]
print(r(train_step([[1.0, 0.5], [-0.5, 2.0]], [0, 1], Ws, bs, 0.1)))
```
The first two hidden units keep their sign and the last two are negated, so each example's hidden vector mixes positive and negative values. The penalty is 0.001 times the average over the two examples of the summed absolute activations. Two of the eight hidden outputs are exactly zero, which gives the sparsity of 0.25, and a unit's incoming weights only receive gradient from the examples on which that unit is active.
Rectifier Networks: One Training Step with an L1 Penalty on the Activations
Glorot, Bordes and Bengio (2011) train deep rectifier networks with a softmax output, the negative log-likelihood of the correct class, mini-batch stochastic gradient descent, and two extras: an L1 penalty on the hidden activations with coefficient 0.001, and a variant of the rectifier in which half of each layer's units have their outputs multiplied by −1 so the layer's mean activation cancels out. Implement one training step with this cost.
Implement train_step(X, y, weights, biases, lr, l1=0.001).
X: shape (B, n_0); y: B class labels in 0 .. C-1.
weights[l] shape (n_{l+1}, n_l) and biases[l] shape (n_{l+1},) for l=0,…,L−1, L≥2. Layers 0..L−2 are hidden layers; layer L−1 is the softmax output layer with nL=C units.
Hidden layer l with d units computes z=Wlh+bl and hk′=skmax(0,zk), where sk=+1 for k<⌊d/2⌋ and sk=−1 for k≥⌊d/2⌋.
The output layer computes scores a=WL−1h+bL−1 and probabilities P=softmax(a).
Take one gradient step on every weight and bias: θ←θ−lr⋅∂loss/∂θ. Treat the derivative of max(0,z) as 0 at z=0.
Output: a dict with "loss", "nll", "penalty" (floats, before the step), "sparsity" (for each hidden layer, the fraction of its B×d activations that are exactly 0, before the step), and "weights", "biases" (the updated parameters as nested lists).
Notes
Do not modify the inputs. Do not round.
Scores can be large: compute the softmax and its logarithm stably.