BinaryConnect consists of two parts: a way to binarize weights and a training procedure.
Two ways to map weights to ±1
The simplest is deterministic binarization, which uses the sign (Eq. (1) in the paper).
wb={+1−1w≥0otherwise
Here, w is the real-valued weight and wb is the binarized weight. The other is stochastic binarization, which chooses +1 with a probability that depends on the value of the real-valued weight (Eqs. (2) and (3)).
wb={+1−1with probability p=σ(w)with probability 1−p,σ(x)=clip(2x+1, 0, 1)
σ is a piecewise-linear function called the hard sigmoid, chosen because it is cheaper to compute than the ordinary sigmoid. When w lies in [−1,1], the expected value of wb is 1⋅σ(w)+(−1)(1−σ(w))=w. In other words, stochastic binarization rounds in a way that matches the original real-valued weight on average (right side of the figure at the top).
Training procedure
For each mini-batch, training proceeds as follows (Algorithm 1 in the paper).
- Binarize the stored real-valued weights w to obtain wb.
- Run forward propagation from input to output using wb.
- Run backpropagation from output to input using wb to obtain the gradients with respect to each layer's activations.
- Compute the gradient with respect to the binary weights, ∂C/∂wb, and use it to update the real-valued weights w.
- Clip the updated w to the range [−1,1]. For the next mini-batch, this w is binarized again.
Steps 4 and 5 combine into the following equation (η is the learning rate).
wt=clip(wt−1−η∂wb∂C, −1, 1)
The binarization function has zero slope almost everywhere, so gradients would not flow through it as is. BinaryConnect ignores the binarization and uses the gradient with respect to the binary weights directly as the gradient of the real-valued weights. This is the same idea as the straight-through estimator (STE) described in the quantization article.
Step 5 clips to [−1,1] because binarization looks only at the sign of w. Without a bounded range, the real-valued weights could grow arbitrarily large without any effect on the binary weights. Keeping them within the range lets the sign flip quickly when the gradient direction changes.
Weights at inference
The paper lists three ways to use the trained network (Section 2.6).
| Usage | Weights used at inference | Benefit at inference |
|---|
| Use binary weights | Deterministically binarized wb | No multiplications; weight memory drops greatly |
| Use real-valued weights | Real-valued w | Inference is not faster (only training is accelerated) |
| Binarize stochastically many times and average | Many stochastically sampled wb | Ensemble effect (heavy, since it computes many times) |
In the paper's experiments, deterministic binarization is evaluated with the first approach and stochastic binarization with the second. Using real-valued weights at inference in the stochastic case follows the same idea as Dropout removing its noise at inference.