L OSS - AWARE W EIGHT Q UANTIZATION OF D EEP N ETWORKS
A BSTRACT
The huge size of deep networks hinders their use in small computing devices.
In this paper, we consider compressing the network by weight quantization.
We extend a recently proposed loss-aware weight binarization scheme to ternarization, with possibly different scaling parameters for the positive and negative weights, and m -bit (where m > 2 ) quantization.
Experiments on feedforward and recurrent neural networks show that the proposed scheme outperforms state-of-the-art weight quantization algorithms, and is as accurate (or even more accurate) than the full-precision network
I NTRODUCTION
The last decade has witnessed huge success of deep neural networks in various domains. Examples include computer vision, speech recognition, and natural language processing (LeCun et al., 2015).
However, their huge size often hinders deployment to small computing devices such as cell phones and the internet of things.
Many attempts have been recently made to reduce the model size.
One common approach is to prune a trained dense network (Han et al., 2015; 2016).
However, most of the pruned weights may come from the fully-connected layers where computations are cheap, and the resultant time reduction is insigniﬁcant.
Li et al.
(2017b)
and Molchanov et al.
(2017) proposed to prune ﬁlters in the convolutional neural networks based on their magnitudes or signiﬁcance to the loss.
However, the pruned network has to be retrained, which is again expensive.
Another direction is to use more compact models.
GoogleNet (Szegedy et al., 2015) and ResNet (He et al., 2016) replace the fully-connected layers with simpler global average pooling.
However, they are also deeper.
SqueezeNet (Iandola et al., 2016) reduces the model size by replacing most of the 3 × 3 ﬁlters with 1 × 1 ﬁlters.
This is less efﬁcient on smaller networks because the dense 1 ×convolutions are costly.
MobileNet (Howard et al., 2017) compresses the model using separable depth-wise convolution.
ShufﬂeNet (Zhang et al., 2017) utilizes pointwise group convolution and channel shufﬂe to reduce the computation cost while maintaining accuracy.
However, highly optimized group convolution and depth-wise convolution implementations are required.
Alternatively,
Novikov et al.
(2015) compressed the model by using a compact multilinear format to represent the dense weight matrix.
The CP and Tucker decompositions have also been used on the kernel tensor in CNNs (Lebedev et al., 2014;
Kim et al., 2016).
However, they often need expensive ﬁne-tuning.
Another effective approach to compress the network and accelerate training is by quantizing each full-precision weight to a small number of bits.
This can be further divided to two sub-categories, depending on whether pre-trained models are used (Lin et al., 2016a; Mellempudi et al., 2017) or the quantized model is trained from scratch (Courbariaux et al., 2015; Li et al., 2017a).
Some of these also directly learn with low-precision weights, but they usually suffer from severe accuracy deterioration (Li et al., 2017a; Miyashita et al., 2016).
By keeping the full-precision weights during learning, Courbariaux et al.
(2015) pioneered the BinaryConnect algorithm, which uses only one bit for each weight while still achieving state-of-the-art classiﬁcation results.
Rastegari et al.
(2016) further incorporated weight scaling, and obtained better results.
Instead of simply ﬁnding the closest binary approximation of the full-precision weights, a loss-aware scheme is proposed in (Hou et al., 2017).
Beyond binarization, TernaryConnect (Lin et al., 2016b) quantizes each weight to {− 1 , 0 , 1 } .
Li & Liu (2016) and Zhu et al.
(2017) added scaling to the ternarized weights, and
DoReFa-Net
(Zhou et al., 2016) further extended quantization to more than three levels.
However, these methods do not consider the effect of quantization on the loss, and rely on heuristics in their procedures (Zhou et al., 2016; Zhu et al., 2017).
Recently, a loss-aware low-bit quantized neural network is proposed in (Leng et al., 2017).
However, it uses full-precision weights in the forward pass and the extra-gradient method (Vasilyev et al., 2010) for update, both of which are expensive.
In this paper, we propose an efﬁcient and disciplined ternarization scheme for network compression.
Inspired by (Hou et al., 2017), we explicitly consider the effect of ternarization on the loss.
This is formulated as an optimization problem which is then solved efﬁciently by the proximal Newton algorithm.
When the loss surface’s curvature is ignored, the proposed method reduces to that of (Li & Liu, 2016), and is also related to the projection step of (Leng et al., 2017).
Next, we extend it to (i) allow the use of different scaling parameters for the positive and negative weights; and (ii) the use of m bits (where m > 2 ) for weight quantization.
Experiments on both feedforward and recurrent neural networks show that the proposed quantization scheme outperforms state-of-the-art algorithms.
Notations: For a vector x , √ x denotes the element-wise square root (i.e., [ √ x ] i = √ x i ), | x | is the element-wise absolute value, (cid:107) x (cid:107) p
=
( (cid:80) i | x i | p ) 1 p is its p -norm, and Diag ( x ) returns a diagonal matrix with x on the diagonal.
For two vectors x and y , x (cid:12) y denotes the element-wise multiplication and x (cid:11)
y
the element-wise division.
Given a threshold ∆ , I ∆ ( x ) returns a vector such that
[ I ∆ ( x )]
i {+= 
+}
if x i > ∆ , − 1 if x i < − ∆ , and 0 otherwise.
I +∆ ( x ) considers only the positive threshold, i.e., [ I +∆ ( x )] i
= 1 if x i > ∆ , and 0 otherwise.
Similarly, [ I − ∆ ( x )]
i = − 1 if x i < − ∆ , and 0 otherwise.
For a matrix X , vec ( X ) returns a vector by stacking all the columns of X , and diag ( X ) returns a vector whose entries are from the diagonal of X .
R ELATED W ORK
Let the full-precision weights from all L layers be w =
[ w (cid:62) 1 , w (cid:62) 2 , . .
. , w (cid:62) L ] (cid:62) , where w l = vec ( W l ) , and W l is the weight matrix at layer l .
The corresponding quantized weights will be denoted ˆ w =
[ ˆ w (cid:62) 1 , ˆ w (cid:62) 2 , . . .
, ˆ w (cid:62) L ] (cid:62) .
2. W EIGHT B INARIZED N ETWORKS
In BinaryConnect (Courbariaux et al., 2015), each element of w l is binarized to − 1 or +1 by using the sign function: Binarize ( w l ) = sign ( w l ) .
In the Binary-Weight-Network (BWN) (Rastegari et al., 2016), a scaling parameter is also included, i.e., Binarize ( w l ) =
α l b l , where α l > 0 , b l ∈ {− 1 , +1 } n l and n l is the number of weights in w l .
By minimizing the difference between w l and α l b l , the optimal α l , b l have the simple form: α l = (cid:107) w l (cid:107) 1 /n l , and b l = sign ( w l ) .
Instead of simply ﬁnding the best binary approximation for the full-precision weight w tl at iteration t , the loss-aware binarized network (LAB) directly minimizes the loss w.r.t.
the binarized weight α tl b tl (Hou et al., 2017).
Let d t − 1 l be a vector containing the diagonal of an approximate Hessian of the loss.
It can be shown that α tl = (cid:107) d t − 1 l (cid:12) w tl (cid:107) 1 / (cid:107) d t − 1 l (cid:107) 1 and b tl = sign ( w tl ) .
2. W EIGHT T ERNARIZED N ETWORKS
In a weight ternarized network, zero is used as an additional quantized value.
In TernaryConnect (Lin et al., 2016b), each weight value is clipped to [ − 1 , 1] before quantization, and then a non-negative weight [ w tl ] i is stochastically quantized to 1 with probability [ w tl ] i (and 0 otherwise).
When [ w tl ] i is negative, it is quantized to − 1 with probability − [ w tl ] i , and 0 otherwise.
In the ternary weight network (TWN) (Li & Liu, 2016), w tl is quantized to ˆ w tl = α tl I ∆ tl ( w tl ) , where ∆ tl is a threshold (i.e., [ ˆ w tl ] i = α tl if [ w tl ] i > ∆ tl , − α tl if [ w tl ] i < − ∆ tl and 0 otherwise).
To obtain ∆ tl and α tl , TWN minimizes the (cid:96) 2 -distance between the full-precision and ternarized weights, leading to
[Equation]
However, ∆ tl in (1) is difﬁcult to solve.
Instead, TWN simply sets ∆ tl = 0 .
7 · E ( | w tl | ) in practice.
In TWN, one scaling parameter ( α tl ) is used for both the positive and negative weights at layer l .
In the trained ternary quantization (TTQ) network (Zhu et al., 2017), different scaling parameters ( α tl and β tl ) are used.
The weight w tl is thus quantized to ˆ w tl = α tl I +∆ tl ( w tl ) + β tl I − ∆ tl ( w tl ) .
The scaling parameters are learned by gradient descent.
As for ∆ tl , two heuristics are used.
The ﬁrst sets ∆ tl to a constant fraction of max( | w tl | ) , while the second sets ∆ tl such that at all layers are equally sparse.
2. W EIGHT Q UANTIZED N ETWORKS
In a weight quantized network, m bits (where m ≥ 2 ) are used to represent each weight.
Let Q be a set of (2 k + 1) quantized values, where k
= 2 m − 1 − 1 .
The two popular choices of Q are (cid:8) − 1 , − k − 1 k , . . .
, − 1 k , 0 , 1 k , . .
, k − 1 k , 1 (cid:9) (linear quantization), and (cid:8) − 1 , − 12 , . . .
, − 1 2 k − 1 , 0 , 1 2 k − 1 , . .
, 12 , 1 (cid:9) (logarithmic quantization).
By limiting the quantized values to powers of two, logarithmic quantization is advantageous in that expensive ﬂoating-point operations can be replaced by cheaper bit-shift operations.
When m = 2 , both schemes reduce to Q = {− 1 , 0 , 1 } .
In the DoReFa-Net (Zhou et al., 2016), weight w tl is heuristically quantized to m -bit, with: [Equation]
in {− 1 , − 2 m − 2 2 m − 1 , . . . , − 1 2 m − 1 ,
[Equation] 1 , 1 } , where quantize m ( x ) = [Equation] 1) x ) .
Similar to loss-aware binarization (Hou et al., 2017), Leng et al.
(2017) proposed a loss-aware quantized network called low-bit neural network (LBNN).
The alternating direction method of multipliers (ADMM) (Boyd et al., 2011) is used for optimization.
At the t th iteration, the full-precision weight w tl is ﬁrst updated by the method of extra-gradient (Vasilyev et al., 2010):
[Equation]
where L is the augmented Lagrangian in the ADMM formulation, and η t is the stepsize.
Next, w tl is projected to the space of m -bit quantized weights so that ˆ w tl is of the form α l b l , where α l > 0 , and b l ∈ (cid:8) − 1 , − 12 , . .
, − 1 2 k − 1 , 0 , 1 2 k − 1 , . .
, 12 , 1 (cid:9) .
L OSS -A WARE Q UANTIZATION 3. T ERNARIZATION USING P ROXIMAL N EWTON A LGORITHM
In weight ternarization, TWN simply ﬁnds the closest ternary approximation of the full precision weight at each iteration, while TTQ sets the ternarization threshold heuristically.
Inspired by LAB (for binarization), we consider the loss explicitly during quantization and obtain the quantization thresholds and scaling parameter by solving an optimization problem.
As in TWN, the weight w l is ternarized as ˆ w l = α l b l , where α l > 0 and b l ∈ {− 1 , 0 , 1 } n l .
Given a loss function (cid:96) , we formulate weight ternarization as the following optimization problem:
[Equation]
where Q is the set of desired quantized values.
As in LAB, we will solve this using the proximal
Newton method (Lee et al., 2014; Rakotomamonjy et al., 2016).
At iteration t , the objective is replaced by the second-order expansion
[Equation]
[Equation]
where H t − 1 is an estimate of the Hessian of (cid:96) at ˆ w t − 1 .
We use the diagonal equilibration preconditioner (Dauphin et al., 2015), which is robust in the presence of saddle points and also readily available in popular stochastic deep network optimizers such as Adam (Kingma & Ba, 2015).
Let D l be the approximate diagonal Hessian at layer l . We use D =
Diag ([ diag ( D 1 ) (cid:62) , . .
, diag ( D L ) (cid:62) ] (cid:62) ) as an estimate of H .
Substituting (4) into (3), we solve the following subproblem at the t th iteration:
[Equation]
Proposition 3.1 The objective in (5) can be rewritten as
[Equation]
where d t − 1 l ≡ diag ( D t − 1 l ) , and
[Equation]
[Equation]
Obviously, this objective can be minimized layer by layer.
Each proximal Newton iteration thus consists of two steps: (i) Obtain w tl in (7) by gradient descent along ∇ l (cid:96) ( ˆ w t − 1 ) , which is preconditioned by the adaptive learning rate 1 (cid:11) d t − 1 l so that the rescaled dimensions have similar curvatures; (ii) Quantize w tl to ˆ w tl by minimizing the scaled difference between ˆ w tl and w tl in (6). Intuitively, when the curvature
is low ( [ d t − 1 l i is small), the loss is not sensitive to the weight and ternarization error can be less penalized.
When the loss surface is steep, ternarization has to be more accurate.
Though the constraint in (6) is more complicated than that in LAB, interestingly the following simple relationship can still be obtained for weight ternarization.
Proposition 3.2 With Q = {− 1 , 0 , 1 } , and the optimal ˆ w tl in (6) of the form α b .
For a ﬁxed b , α =
[Equation]
1 ; whereas when α is ﬁxed, b = I α/ 2 ( w tl ) .
Equivalently, b can be written as Π Q ( w tl /α ) , where Π Q ( · ) projects each entry of the input argument to the nearest element in Q .
Further discussions on how to solve for α tl will be presented in
Sections 3.1.1 and 3.1.2.
When the curvature is the same for all dimensions at layer l , the following
Corollary shows that the solution above reduces that of TWN.
Corollary 3.1 When D t − 1 l = λ
I , α tl reduces to the TWN solution in (1) with ∆ tl = α tl / 2 .
In other words, TWN corresponds to using the proximal gradient algorithm, while the proposed method corresponds to using the proximal Newton algorithm with diagonal Hessian.
In composite optimization, it is known that the proximal Newton algorithm is more efﬁcient than the proximal gradient algorithm
(Lee et al., 2014; Rakotomamonjy et al., 2016).
Moreover, note that the interesting relationship ∆ tl = α tl / 2 is not observed in TWN, while TTQ completely neglects this relationship.
In LBNN (Leng et al., 2017), its projection step uses an objective which is similar to (6), but without using the curvature information.
Besides, their w tl is updated with the extra-gradient in (2), which doubles the number of forward, backward and update steps, and can be costly.
Moreover, LBNN uses full-precision weights in the forward pass, while all other quantization methods including ours use quantized weights (which eliminates most of the multiplications and thus faster training).
When (i) (cid:96) is continuously differentiable with Lipschitz-continuous gradient (i.e., there exists β >such that (cid:107)∇ (cid:96) ( u ) − ∇ (cid:96) ( v ) (cid:107) 2 ≤ β (cid:107) u − v (cid:107) 2 for any u , v ); (ii) (cid:96) is bounded from below; and (iii)
[ d tl ] k > β ∀ l, k, t , it can be shown that the objective of (3) produced by the proximal Newton algorithm (with solution in Proposition 3.2) converges (Hou et al., 2017).
In practice, it is important to keep the full-precision weights during update (Courbariaux et al., 2015).
Hence, we replace (7) by w tl ← w t − 1 l
[Equation]
The whole procedure, which is called Loss-Aware Ternarization (LAT), is shown in Algorithm 3 of
Appendix B.
It is similar to Algorithm 1 of LAB (Hou et al., 2017), except that α tl and b tl are computed differently.
In step 4, following (Li & Liu, 2016), we ﬁrst rescale input x t − 1 l with α l , so that multiplications in dot products and convolutions become additions.
Algorithm 3 can also be easily extended to ternarize weights in recurrent networks.
Interested
3.1. E XACT SOLUTION OF α tl
To simplify notations, we drop the superscripts and subscripts.
From Proposition 3.2,
[Equation]
We now consider how to solve for α .
First, we introduce some notations.
Given a vector x
=
[ x 1 , x 2 , . . .
, x n ] , and an indexing vector s ∈ R n whose entries are a permutation of { 1 , . . . , n } , perm s ( x ) returns the vector [ x s 1 , x s 2 , . . .
x s n ] , and cum ( x ) =
[ x 1 , (cid:80) 2 i =1 x i , . . .
, (cid:80) ni
=1 x i ] returns partial sums for elements in x .
For example, let a
=
[1 , − 1 , − 2] , and b = [3 , 1 , 2] .
Then, perm b ( a ) =
[ − 2 , 1 , − 1] and cum ( a ) =
[1 , 0 , − 2] .
We sort elements of | w | in descending order, and let the vector containing the sorted indices be s .
For example, if w = [1 , 0 , − 2] , then s =
From (8),
[Equation]
where c = cum ( perm s ( | d (cid:12) w | ))
(cid:11) cum ( perm s ( d )) (cid:11) 2 , and j is the index such that
[Equation]
For simplicity of notations, let the dimensionality of w (and thus also of c ) be n , and the operation ﬁnd ( condition ( x )) returns all indices in x that satisﬁes the condition.
It is easy to see that any j satisfying (10) is in S ≡ ﬁnd ([ perm s ( | w | )]
[1:( n − 1)] − c [1:( n − 1)] ) (cid:12) ([ perm s ( | w | )] [2: n ] − c
[1: n − 1] ) < 0) , where c [1:( n − 1)] is the subvector of c with elements in the index range 1 to n − 1 .
The optimal α ( = 2 c j ) is then the one which yields the smallest objective in (6), which can be simpliﬁed by
Proposition 3.3 below.
The procedure is shown in Algorithm 1.
Proposition 3.
The optimal α tl of (6) equals 2 arg max c j : j ∈S c 2 j · [ cum ( perm s ( d t − 1 l ))]
j .
Algorithm 2: s arg sort w tl | ) ; 3: c = cum
perm s d t − 1 l
w tl cum perm s d t −
; 4: S = ﬁnd perm s w tl )] [1:( n − 1)] − c [1:( n − 1)] )
perm s
| w tl | )] [2: n ] − c [1: n − 1] ) < 0) ; 5: α tl = 2 arg max c j : j ∈S c
3.1. A PPROXIMATE SOLUTION OF α tl
In case the sorting operation in step 2 is expensive, α tl and b tl can be obtained by alternating the iteration in Proposition 3.2 (Algorithm 2).
Empirically, it converges very fast, usually in 5 iterations.
Algorithm 2 Approximate
3. E XTENSION TO T ERNARIZATION WITH T WO S CALING P ARAMETERS
As in TTQ (Zhu et al., 2017), we can use different scaling parameters for the positive and negative weights in each layer.
The optimization subproblem at the t th iteration then becomes:
[Equation]
Proposition 3.4 The optimal ˆ w tl in (5) is of the form ˆ w tl = α tl p tl + β tl q tl , where α tl = [Equation] p
[Equation]
d t − 1 l (cid:12) w tl
[Equation]
, and q tl =
I
[Equation]
tl [Equation] 2 ( w tl ) . The exact and approximate solutions for α tl and β tl can be obtained in a similar way as in Sections 3.1.1 and 3.1.2.
Details are in Appendix C.
3. E XTENSION TO L OW -B IT Q UANTIZATION
For m -bit quantization, we simply change the set Q of desired quantized values in (3) to one with k = 2 m − 1 − 1 quantized values.
The optimization still contains a gradient descent step with adaptive learning rates like LAT, and a quantization step which can be solved efﬁciently by alternating minimization of ( α, b ) (similar to the procedure in Algorithm 2) using the following Proposition.
Proposition 3.5 Let the optimal ˆ w tl in (6) be of the form α b . For a ﬁxed b , α = (cid:107) b (cid:12) d t − 1 l (cid:12) w tl (cid:107) 1 (cid:107) b (cid:12) d t − 1 l (cid:107) 1 ; whereas when α is ﬁxed, b = Π Q ( w tl α ) , where Q = (cid:8) − 1 , − k − 1 k , . .
, − 1 k , 0 , 1 k , . .
, k − 1 k , 1 (cid:9) for linear quantization and Q = (cid:8) − 1 , − 12 , . . .
, − 1 2 k − 1 , 0 , 1 2 k − 1 , . .
, 12 , 1 (cid:9) for logarithmic quantization.
E XPERIMENTS
In this section, we perform experiments on both feedforward and recurrent neural networks.
The following methods are compared: (i) the original full-precision network; (ii) weight-binarized networks, including BinaryConnect (Courbariaux et al., 2015), Binary-Weight-Network (BWN) (Rastegari et al., 2016), and Loss-Aware Binarized network (LAB) (Hou et al., 2017); (iii) weightternarized networks, including Ternary Weight Networks (TWN) (Li & Liu, 2016), Trained Ternary
Quantization (TTQ) 2 (Zhu et al., 2017), the proposed Loss-Aware Ternarized network with exact solution (LATe), approximate solution (LATa), and with two scaling parameters (LAT2e and LAT2a); (iv) m -bit-quantized networks (where m > 2 ), including DoReFa-Netm (Zhou et al., 2016), the proposed loss-aware quantized network with linear quantization (LAQm(linear)), and logarithmic quantization (LAQm(log)).
Since weight quantization can be viewed as a form of regularization (Courbariaux et al., 2015), we do not use other regularizers such as dropout and weight decay.
4. F EEDFORWARD N ETWORKS
In this section, we perform experiments with the multilayer perceptron (on the MNIST data set) and convolutional neural networks (on CIFAR-10 , CIFAR-100 and SVHN ).
For MNIST , CIFAR-10 , and SVHN , the setup is similar to that in (Courbariaux et al., 2015; Hou et al., 2017).
Details can be found in Appendix D. For CIFAR-100 , we use 45 , 000 images for training, another 5 , 000 for validation, and the remaining 10 , 000 for testing.
The testing errors are shown in Table 1.
Ternarization:
On MNIST , CIFAR100 and SVHN , the weight-ternarized networks perform better than weight-binarized networks, and are comparable to the full-precision networks.
Among the weight-ternarized networks, the proposed LAT and its variants have the lowest errors.
On CIFAR-10 ,
LATa has similar performance as the full-precision network, but is outperformed by BinaryConnect.
Figure 1(a) shows convergence of the training loss for LATa on CIFAR-10 , and Figure 1(b) shows the scaling parameter obtained at each CNN layer.
As can be seen, the scaling parameters for the [Table] ﬁrst and last layers (conv1 and linear3, respectively) are larger than the others.
This agrees with the ﬁnding that, to maintain the activation variance and back-propagated gradients variance during the forward and backward propagations, the variance of the weights between the l th and ( l +1)
th layers should roughly follow 2 / ( n l + n l +1 )
(Glorot & Bengio, 2010).
Hence, as the input and output layers are small, larger scaling parameters are needed for their high-variance weights.
[Figure]
Using Two Scaling Parameters: Compared to TTQ, the proposed LAT2 always has better performance.
However, the extra ﬂexibility of using two scaling parameters does not always translate to lower testing error.
As can be seen, it outperforms algorithms with one scaling parameter only on CIFAR-100 .
We speculate this is because the capacities of deep networks are often larger than needed, and so the limited expressiveness of quantized weights may not signiﬁcantly deteriorate performance.
Indeed, as pointed out in (Courbariaux et al., 2015), weight quantization is a form of regularization, and can contribute positively to the performance.
Using More Bits: Among the 3-bit quantization algorithms, the proposed scheme with logarithmic quantization has the best performance.
It also outperforms the other quantization algorithms on
CIFAR-100 and SVHN .
However, as discussed above, more quantization ﬂexibility is useful only when the weight-quantized network does not have enough capacity.
4. R ECURRENT N ETWORKS
In this section, we follow (Hou et al., 2017) and perform character-level language modeling experiments on the long short-term memory (LSTM) (Hochreiter & Schmidhuber, 1997).
The training objective is the cross-entropy loss over all target sequences.
Experiments are performed on three data sets: (i) Leo Tolstoy’s War and Peace ; (ii) source code of the Linux Kernel ; and (iii) Penn Treebank Corpus (Taylor et al., 2003).
For the ﬁrst two, we follow the setting in (Karpathy et al., 2016;
Hou et al., 2017).
For Penn Treebank , we follow the setting in (Mikolov & Zweig, 2012).
In the experiment, we tried different initializations for TTQ and then report the best.
Cross-entropy values on the test set are shown in Table 2.
[Table]
Ternarization: As in Section 4.1, the proposed LATe and LATa outperform the other weight ternarization schemes, and are even better than the full-precision network on all three data sets.
Figureshows convergence of the training and validation losses on War and Peace .
Among the ternarization methods, LAT and its variants converge faster than both TWN and TTQ.
[Figure]
Using Two Scaling Parameters: LAT2e and LAT2a outperform TTQ on all three data sets.
They also perform better than using one scaling parameter on War and Peace and Penn Treebank .
Using More Bits: The proposed LAQ always outperforms DoReFa-Net when 3 or 4 bits are used.
As noted in Section 4.1, using more bits does not necessarily yield better generalization performance, and ternarization (using 2 bits) yields the lowest validation loss on War and Peace and Linux
Kernel .
Moreover, logarithmic quantization is better than linear quantization.
Figure 3 shows distributions of the input-to-hidden (full-precision and quantized) weights of the input gate trained after 20 epochs using LAQ3(linear) and LAQ3(log) (results on the other weights are similar).
As can be seen, distributions of the full-precision weights are bell-shaped.
Hence, logarithmic quantization can give ﬁner resolutions to many of the weights which have small magnitudes.
[Figure] Quantized vs Full-precision Networks: The quantized networks often perform better than the fullprecision networks. We speculate that this is because deep networks often have larger-than-needed capacities, and so are less affected by the limited expressiveness of quantized weights.
Moreover, low-bit quantization acts as regularization, and so contributes positively to the performance.
C ONCLUSION
In this paper, we proposed a loss-aware weight quantization algorithm that directly considers the effect of quantization on the loss.
The problem is solved using the proximal Newton algorithm.
Each iteration consists of a preconditioned gradient descent step and a quantization step that projects fullprecision weights onto a set of quantized values.
For ternarization, an exact solution and an efﬁcient approximate solution are provided.
The procedure is also extended to the use of different scaling parameters for the positive and negative weights, and to m -bit (where m > 2 ) quantization.
Experiments on both feedforward and recurrent networks show that the proposed quantization scheme outperforms the current state-of-the-art.
A CKNOWLEDGMENTS
This research was supported in part by the Research Grants Council of the Hong Kong Special Administrative Region (Grant 614513). We thank the developers of Theano (Theano Development
{+Team, 2016), Pylearn
 (Goodfellow et al., 2013) and Lasagne. We also thank NVIDIA for the gift of GPU card. A P ROOFS A. P ROOF OF P ROPOSITION 3.+}
With w tl in (7), the objective in (5) can be rewritten as
[Equation]
where c 1 = − 12 ( √ d t − 1 l
[Equation]
(cid:11)
[Equation]
)) 2 is independent of α tl and b tl .
A. P ROOF OF P ROPOSITION 3.
To simplify notations
[Equation]
we drop the subscript and superscript.
Considering one particular layer, problem (6) is of the form:
[Equation]
[Equation]
When α is ﬁxed,
[Equation]
When b is ﬁxed,
[Equation]
A. P ROOF OF C OROLLARY 3.
When D t − 1 l = λ I , i.e., the curvature is the same for all dimensions in the l th layer, From Proposition 3.2, α [Equation] ∆ [Equation]
(cid:107)
[Equation]
(cid:12) [Equation] (cid:107)
[Equation] (cid:107) [Equation] tl / 2 (cid:107)= arg max ∆
[Equation]
(cid:107) I ∆ ( w tl ) (cid:107)  (cid:88)
i :[ w tl ] i > ∆ | [ w tl ] i | 
 .
This is the same as the TWN solution in (1).
A. P ROOF OF P ROPOSITION 3.
For simplicity of notations [Equation] we drop the subscript and superscript.
For each layer, we have an optimization problem of the form
[Equation]
[Equation]
where the second equality holds as b = I α/ 2 ( w ) .
From (9), we have
[Equation] [Equation]
=
[Equation]
[ cum ( perm s ( d ))] j =
[Equation]
c [Equation]
j · [ [Equation] ( perm s ( d ))] j .
Thus, the α that minimizes ( √ d (cid:62) ( α b − w )) 2 is α = 2 arg max c j ,j ∈S c 2 j · [ cum ( perm s ( d ))]
j .
A. P ROOF FOR P ROPOSITION 3.
For simplicity of notations, we drop the subscript and superscript, and consider the optimization problem:
[Equation]
0 , + α } .
Let f ( ˆ w i ) =
( ˆ w i − w i ) 2 .
Then, f ( α ) =
( α − w i ) 2 , f (0) =
w 2 i , and f ( − β ) =
( β + w i ) 2 .
It is easy to see that (i) if w i > α/ 2 , f ( α ) is the smallest; (ii) if w i < − β/ 2 , f ( − 1) is the smallest; (iii) if − β/ 2 ≤ w i ≤ α/ 2 , f (0) is the smallest.
In other words, the optimal ˆ w i satisﬁes
[Equation]
or equivalently, ˆ w =
α p + β q , where p =
I + α/ 2 ( w ) , and q =
I − β ( w ) .
Deﬁne w + and w − such that [ w + ] i = (cid:26) w i w i > otherwise , and [ w − ] i = (cid:26) w i w i < otherwise . .
Then,
[Equation]
The objective in (12) has two parts, and each part can be viewed as a special case of the ternarization step in Proposition 3.1 (considering only with positive or negative weights).
Similar to the proof for
Proposition 3.2, we can obtain that the optimal ˆ w = α p + β q satisﬁes
[Equation]
A. P ROOF OF P ROPOSITION 3.
For simplicity of notations, we drop the subscript and superscript.
Since 12 ( √ d (cid:62) ( α b − w )) 2 = 12 (cid:80) ni =1 d i ( αb i − w i ) 2 for each layer, we simply consider the optimization problem: min
[Equation]
When α is ﬁxed,
[Equation]
When b is ﬁxed,
[Equation]
[Table]
B L OSS -A WARE T ERNARIZATION A LGORITHM (LAT)
The whole procedure of LAT is shown in Algorithm 3.
C E XACT AND A PPROXIMATE S OLUTIONS FOR T ERNARIZATION WITH T WO S CALING P ARAMETERS
Let there be n 1 positive elements and n 2 negative elements in w l .
For a n -dimensional vector x = [ x 1 , x 2 , . . .
, x n ] , deﬁne inverse ( x ) =
[ x n , x n − 1 , . . .
, x 1 ] .
As is shown in (12), the objective can be separated into two parts, and each part can be viewed as a special case of ternarization step in Proposition 3.1, dealing only with positive or negative weights.
Thus the exact and approximate solutions for α tl and β tl can separately be derived in a similar way as that of using one scaling parameter.
The exact and approximate solutions for α tl and β tl for layer- l at the t th time step are shown in Algorithms 4 and 5.
D E XPERIMENTAL D ETAILS D. S ETUP FOR F EEDFORWARD N ETWORKS
The setup for the four data sets are as follows:
MNIST :
This contains 28 × 28 gray images from 10 digit classes.
We use 50 , 000 images for training, another 10 , 000 for validation, and the remaining 10 , 000 for testing.
We use the 4-layer model:
[Equation]
where FC is a fully-connected layer, and SV M is a (cid:96) 2 -SVM output layer using the square hinge loss.
Batch normalization with a minibatch size 100 , is used to accelerate learning.
The maximum number of epochs is 50 .
The learning rate starts at 0 .
01 , and decays by a factor of 0 .
1 at epochs 15 and 25 .
m tl β 1 m t − 1 l + (1 − β 1 )
∇ l (cid:96) ( ˆ w t ) v tl = β 2 v t − 1 l + (1 − β 2 )( ∇ l (cid:96) ( ˆ w t ) (cid:12) ∇ l (cid:96) ( ˆ w t )) compute m tl = m tl / (1 − β tl /
− β
21: compute current curvature matrix d tl = 1 η t (cid:16) (cid:15) 1 + (cid:112) ˆ v tl (cid:17) ; 22: update full-precision weights w t +1 l = w tl − ˆ m tl (cid:11) d tl ; 23: update learning rate η t +1 =
UpdateLearningrate ( η t , t + 1) ; 24: end for
)] [2: n 2 ] − [ c 2 ] [1: n 2 − 1] ) < 0) ; 10: β tl = 2 arg max c i ,i ∈S 2 [ c 2 ] 2 i (cid:12) [ cum ( perm s 2 ( | d t − 1 l | ))]
i ; 11: q tl =
I − β/ 2 ( w tl ) ; 12: Output: ˆ w tl = α tl p tl + β tl q tl .
CIFAR-10 :
This contains 32 × 32 color images from 10 object classes.
We use 45 ,images for training, another 5 , 000 for validation, and the remaining 10 , 000 for testing.
The images are preprocessed with global contrast normalization and ZCA whitening.
We use the VGG-like architecture:
[Equation]
where C 3 is a 3 × 3 ReLU convolution layer, and MP 2 is a 2 × 2 max-pooling layer.
Batch normalization with a minibatch size of 50 , is used.
The maximum number of epochs is 200 .
The learning rate for the weight-binarized network starts at 0 .
03 while for all the other networks starts at 0 .
002 , and decays by a factor of 0 .
5 after every 15 epochs.
CIFAR-100 : This contains 32 × 32 color images from 100 object classes.
We use 45 ,images for training, another 5 , 000 for validation, and the remaining 10 , 000 for testing.
The images are preprocessed with global contrast normalization and ZCA whitening.
We use the VGG-like architecture:
[Equation]
FC )
− 100 SV M. Batch normalization with a minibatch size of 100 , is used.
The maximum number of epochs is 200 .
The learning rate starts at 0 . 0005 , and decays by a factor of 0 . 5 after everyepochs.
SVHN : This contains 32 × 32 color images from 10 digit classes.
We use 598 , 388 images for training, another 6 , 000 for validation, and the remaining 26 , 032 for testing.
The images are preprocessed with global and local contrast normalization.
The model used is: (2 × 64 C 3) − MP 2 − (2 × 128 C 3) − MP 2 − (2 × 256 C 3) − MP 2 − (2 × 1024 FC ) − 10 SV M. Batch normalization with a minibatch size of 50 , is used.
The maximum number of epochs is 50 .
The learning rate starts at 0 . 001 for the weight-binarized network, and 0 .
for the other networks. It then decays by a factor of 0 .
1 at epochs 15 and 25 .
D. S ETUP FOR R ECURRENT N ETWORKS
The setup for the three data sets are as follows:
Leo Tolstoy’s War and Peace : It consists of 3258K characters of almost entirely English text with minimal markup and a vocabulary size of 87 .
We use the same training/validation/test set split as in (Karpathy et al., 2016; Hou et al., 2017).
The source code of the Linux Kernel : This consists of 621K characters and a vocabulary size of 101 .
We use the same training/validation/test set split as in (Karpathy et al., 2016; Hou et al., 2017).
The Penn Treebank data set (Taylor et al., 2003): This has been frequently used for language modeling.
It contains 50 different characters, including English characters, numbers, and punctuations.
We follow the setting in (Mikolov & Zweig, 2012), with 5,017K characters for training, 393K for validation, and 442K characters for testing.
We use a one-layer LSTM with 512 cells.
The maximum number of epochs is 200 , and the number of time steps is 100 .
The initial learning rate is 0 .
After 10 epochs, it is decayed by a factor of 0 .
98 after each epoch.
The weights are initialized uniformly in [0 . 08 , 0 . 08] .
After each iteration, the gradients are clipped to the range [ − 5 , 5] .
All the updated weights are clipped to [ − 1 , 1] for binarization and ternarization methods, but not for m -bit (where m > 2 ) quantization methods.