Binarizing the Weights (Binary-Weight-Network)
Treat the weights of one filter as a vector W of n values, and approximate it as W≈αB with a binary vector B∈{+1,−1}n and a positive scale α. These are chosen to minimize the approximation error (Eq. (2) in the paper).
α∗,B∗=α,Bargmin∥W−αB∥2
Expanding this expression, and noting that B⊤B=n is a constant, the optimal B and α are obtained as follows (Eqs. (4)–(6)).
B∗=sign(W),α∗=nW⊤sign(W)=n1i∑∣Wi∣
In other words, the binary values are the signs of the weights, and the scale is the mean absolute value of the weights. A convolution then becomes a multiplication-free convolution (⊕) of the input I with the binary filter, followed by a single multiplication by α (Eq. (1)).
I∗W≈(I⊕B)α
Binarizing the Inputs Too (XNOR-Net)
To binarize the inputs as well, the dot product between a patch of the input X and the weights W is approximated as X⊤W≈βH⊤αB (H,B∈{+1,−1}n, β,α>0). Solving in the same way as for the weights gives H∗=sign(X), B∗=sign(W), and β∗≈n1∥X∥ℓ1 (Eqs. (7)–(10)).
In a convolution, the input patch changes with each position the filter is applied to, so β is needed for each position. To avoid recomputing overlapping regions, the scales are computed all at once as follows (Section 3.2).
- Average the absolute value of the input I along the channel dimension to form the matrix A=∑i∣I:,:,i∣/c.
- Convolve A with a filter k whose elements are all 1/(w×h) to obtain the per-position scale K=A∗k (w×h is the filter size).
- Convolve the input sign sign(I) with the weight sign sign(W) using XNOR and bitcount.
- Multiply the result by the per-position K and the per-filter α.
Put together, this gives the following expression (Eq. (11); ⊛ is convolution via XNOR and bitcount, and ⊙ is the element-wise product).
I∗W≈(sign(I)⊛sign(W))⊙Kα
The product of two ±1 values becomes an XNOR because, if +1 is encoded as bit 1 and −1 as bit 0, the product is +1 (XNOR gives 1) when the two match and −1 (XNOR gives 0) when they differ. The sum of n products is 2p−n, where p is the number of 1s in the XNOR result. A CPU can process 64 bits in a single instruction, so 64 products are computed at once.
Training
As in BinaryConnect, training keeps real-valued weights and uses binary values only for propagation (Algorithm 1 in the paper).
- For each filter in each layer, compute α=n1∥W∥ℓ1 and B=sign(W), and form W~=αB.
- Run the forward pass using B and α (convolution via Eq. (1) or Eq. (11)).
- Run backpropagation using W~ to obtain the gradients.
- Update the real-valued weights W with those gradients using SGD, Adam, or a similar optimizer.
For the gradient of the sign function, an approximation (the straight-through estimator) passes a gradient of 1 only where ∣r∣≤1 and 0 elsewhere. After training, the real-valued weights can be discarded, keeping only the binary weights and scales.