Sparse Online Learning via Truncated Gradient
John Langford, Lihong Li, Tong Zhang
Introduction
We are concerned with machine learning over large datasets. As an example, the largest dataset we use here has over sparse examples and features using about bytes. In this setting, many common approaches fail, simply because they cannot load the dataset into memory or they are not sufficiently efficient. There are roughly two approaches which can work:
Parallelize a batch learning algorithm over many machines (e.g., ).
Stream the examples to an online learning algorithm (e.g., , , , and ).
This paper focuses on the second approach.
Typical online learning algorithms have at least one weight for every feature, which is too much in some applications for a couple reasons:
Space constraints. If the state of the online learning algorithm overflows RAM it can not efficiently run. A similar problem occurs if the state overflows the L2 cache.
Test time constraints on computation. Substantially reducing the number of features can yield substantial improvements in the computational time required to evaluate a new sample.
This paper addresses the problem of inducing sparsity in learned weights while using an online learning algorithm. There are several ways to do this wrong for our problem. For example:
Simply adding regularization to the gradient of an online weight update doesn’t work because gradients don’t induce sparsity. The essential difficulty is that a gradient update has the form where and are two floats. Very few float pairs add to (or any other default value) so there is little reason to expect a gradient update to accidentally produce sparsity.
Simply rounding weights to is problematic because a weight may be small due to being useless or small because it has been updated only once (either at the beginning of training or because the set of features appearing is also sparse). Rounding techniques can also play havoc with standard online learning guarantees.
Black-box wrapper approaches which eliminate features and test the impact of the elimination are not efficient enough. These approaches typically run an algorithm many times which is particularly undesirable with large datasets.
The Lasso algorithm is commonly used to achieve regularization for linear regression. This algorithm does not work automatically in an online fashion. There are two formulations of regularization. Consider a loss function which is convex in , where is an input/output pair. One is the convex constraint formulation
where is a tunable parameter. The other is soft-regularization, where
With appropriately chosen , the two formulations are equivalent. The convex constraint formulation has a simple online version using the projection idea in . It requires the projection of weight into an ball at every online step. This operation is difficult to implement efficiently for large-scale data where we have examples with sparse features and a large number of features. In such situation, we require that the number of operations per online step to be linear with respect to the number of nonzero features, and independent of the total number of features. Our method, which works with the soft-regularization formulation (2), satisfies the requirement. Additional details can be found in Section 5. In addition to regularization formulation (2), the family of online algorithms we consider in this paper also include some nonconvex sparsification techniques.
The Forgetron algorithm is an online learning algorithm that manages memory use. It operates by decaying the weights on previous examples and then rounding these weights to zero when they become small. The Forgetron is stated for kernelized online algorithms, while we are concerned with the simple linear setting. When applied to a linear kernel, the Forgetron is not computationally or space competitive with approaches operating directly on feature weights.
2 What We Do
At a high level, the approach we take is weight decay to a default value. This simple approach enjoys a strong performance guarantee, as discussed in section 3. For instance, the algorithm never performs much worse than a standard online learning algorithm, and the additional loss due to sparsification is controlled continuously with a single real-valued parameter. The theory gives a family of algorithms with convex loss functions for inducing sparsity—one per online learning algorithm. We instantiate this for square loss and show how to deal with sparse examples efficiently in section 4.
As mentioned in the introduction, we are mainly interested in sparse online methods for large scale problems with sparse features. For such problems, our algorithm should satisfy the following requirements:
The algorithm should be computationally efficient: the number of operations per online step should be linear in the number of nonzero features, and independent of the total number of features.
The algorithm should be memory efficient: it needs to maintain a list of active features, and can insert (when the corresponding weight becomes nonzero) and delete (when the corresponding weight becomes zero) features dynamically.
The implementation details, showing that our methods satisfy the above requirements, are provided in section 5.
Theoretical results stating how much sparsity is achieved using this method generally require additional assumptions which may or may not be met in practice. Consequently, we rely on experiments in section 6 to show that our method achieves good sparsity practice. We compare our approach to a few others, including regularization on small data, as well as online rounding of coefficients to zero.
Online Learning with GD
In the setting of standard online learning, we are interested in sequential prediction problems where repeatedly from :
We make a prediction based on existing weights .
We observe , let , and incur some known loss convex in parameter .
We update weights according to some rule: .
We want to come up with an update rule , which allows us to bound the sum of losses
as well as achieving sparsity. For this purpose, we start with the standard stochastic gradient descent rule, which is of the form:
where is a sub-gradient of with respect to the first variable . The parameter is often referred to as the learning rate. In our analysis, we only consider constant learning rate with fixed for simplicity. In theory, it might be desirable to have a decaying learning rate which becomes smaller when increases to get the so called no-regret bound without knowing in advance. However, if is known in advance, one can select a constant accordingly so that the regret vanishes as . Since our focus is on sparsity, not how to choose learning rate, for clarity, we use a constant learning rate in the analysis because it leads to simpler bounds.
The above method has been widely used in online learning such as and . Moreover, it is argued to be efficient even for solving batch problems where we repeatedly run the online algorithm over training data multiple times. For example, the idea has been successfully applied to solve large-scale standard SVM formulations . In the scenario outlined in the introduction, online learning methods are more suitable than some traditional batch learning methods.
However, a main drawback of (3) is that it does not achieve sparsity, which we address in this paper. Note that in the literature, this particular update rule is often referred to as gradient descent (GD) or stochastic gradient descent (SGD). There are other variants, such as exponentiated gradient descent (EG). Since our focus in this paper is sparsity, not GD versus EG, we shall only consider modifications of (3) for simplicity.
Sparse Online Learning
In this section, we examine several methods for achieving sparsity in online learning. The first idea is simple coefficient rounding, which is the most natural method. We will then consider its full online implementation, and another method which is the online counterpart of regularization in batch learning. As we shall see, all these ideas are closely related.
In order to achieve sparsity, the most natural method is to round small coefficients (that are no larger than a threshold ) to zero after every online steps. That is, if is not an integer, we use the standard GD rule in (3); if is an integer, we modify the rule as:
where for a vector , and a scalar , , with
That is, we first perform a standard stochastic gradient descent rule, and then round the updated coefficients toward zero. The effect is to remove nonzero and small components in the weight vector.
In general, we should not take , especially when is small, since each step modifies by only a small amount. If a coefficient is zero, it remains small after one online update, and the rounding operation pulls it back to zero. Consequently, rounding can be done only after every steps (with a reasonably large ); in this case, nonzero coefficients have sufficient time to go above the threshold . However, if is too large, then in the training stage, we will need to keep many more nonzero features in the intermediate steps before they are rounded to zero. In the extreme case, we may simply round the coefficients in the end, which does not solve the storage problem in the training phase. The sensitivity in choosing appropriate is a main drawback of this method; another drawback is the lack of theoretical guarantee for its online performance.
The above mentioned issues motivate us to consider more principled sparse online learning methods. In section 3.3, we derive an online version of rounding using an idea called truncated gradient for which regret bounds hold.
2 A Sub-gradient Algorithm for L1L_{1} Regularization
In our experiments, we combine rounding-in-the-end-of-training with a simple online sub-gradient method for regularization with a regularization parameter :
3 Truncated Gradient
In order to obtain an online version of the simple rounding rule in (4), we observe that the direct rounding to zero is too aggressive. A less aggressive version is to shrink the coefficient to zero by a smaller amount. We call this idea truncated gradient.
The amount of shrinkage is measured by a gravity parameter :
where for a vector , and a scalar , , with
Again, the truncation can be performed every online steps. That is, if is not an integer, we let ; if is an integer, we let for a gravity parameter . This particular choice is equivalent to (4) when we set such that . This requires a large when is small. In practice, one should set a small, fixed , as implied by our regret bound developed later.
In general, the larger the parameters and are, the more sparsity is incurred. Due to the extra truncation , this method can lead to sparse solutions, which is confirmed in our experiments described later. In those experiments, the degree of sparsity discovered varies with the problem.
A special case, which we use in the experiment, is to let in (6). In this case, we can use only one parameter to control sparsity. Since when is small, the truncation operation is less aggressive than the rounding in (4). At first sight, the procedure appears to be an ad-hoc way to fix (4). However, we can establish a regret bound for this method, showing that it is theoretically sound.
Another important special case of (6) is setting . This leads to the following update rule for every -th online step
where for a vector , and a scalar , , with
The method is a modification of the standard sub-gradient online method for regularization in (5). The parameter controls the sparsity that can be achieved with the algorithm. Note that when , the update rule is identical to the standard stochastic gradient descent rule. In general, we may perform a truncation every steps. That is, if is not an integer, we let ; if is an integer, we let for a gravity parameter . The reason for doing so (instead of a constant ) is that we can perform a more aggressive truncation with gravity parameter after each steps. This can potentially lead to better sparsity.
The procedure in (7) can be regarded as an online counterpart of regularization in the sense that it approximately solves an regularization problem in the limit of . Truncated gradient descent for regularization is different from the naive application of stochastic gradient descent rule (3) with an added regularization term. As pointed out in the introduction, the latter fails because it rarely leads to sparsity. Our theory shows that even with sparsification, the prediction performance is still comparable to that of the standard online learning algorithm. In the following, we develop a general regret bound for this general method, which also shows how the regret may depend on the sparsification parameter .
4 Regret Analysis
Throughout the paper, we use for -norm, and for -norm. For reference, we make the following assumption regarding the loss function:
We assume that is convex in , and there exist non-negative constants and such that for all and .
For linear prediction problems, we have a general loss function of the form . The following are some common loss functions with corresponding choices of parameters and (which are not unique), under the assumption that .
Logistic: ; and . This loss is for binary classification problems with .
SVM (hinge loss): ; and . This loss is for binary classification problems with .
Least squares (square loss): ; and . This loss is for regression problems.
Our main result is Theorem 3.1 that is parameterized by and . The proof is left to the appendix. Specializing it to particular losses yields several corollaries. The one that can be applied to the least squares loss will be given later in Corollary 4.1.
(Sparse Online Regret) Consider sparse online update rule (7) with and . If Assumption 3.1 holds, then for all we have
where for vectors and , we let
where is the set indicator function.
We state the theorem with a constant learning rate . As mentioned earlier, it is possible to obtain a result with variable learning rate where decays as increases. Although this may lead to a no-regret bound without knowing in advance, it introduces extra complexity to the presentation of the main idea. Since our focus is on sparsity rather than optimizing learning rate, we do not include such a result for clarity. If is known in advance, then in the above bound, one can simply take and the regret is of order .
In the above theorem, the right-hand side involves a term that depends on which is not easily estimated. To remove this dependency, a trivial upper bound of can be used, leading to penalty . In the general case of , we cannot remove the dependency because the effective regularization condition (as shown on the left-hand side) is the non-convex penalty . Solving such a non-convex formulation is hard both in the online and batch settings. In general, we only know how to efficiently discover a local minimum which is difficult to characterize. Without a good characterization of the local minimum, it is not possible for us to replace on the right-hand side by because such a formulation would have implied that we could efficiently solve a non-convex problem with a simple online update rule. Still, when , one naturally expects that the right-hand side penalty is much smaller than the corresponding penalty , especially when has many components that are close to . Therefore the situation with can potentially yield better performance on some data. This is confirmed in our experiments.
Theorem 3.1 also implies a trade-off between sparsity and regret performance. We may simply consider the case where is a constant. When is small, we have less sparsity but the regret term on the right-hand side is also small. When is large, we are able to achieve more sparsity but the regret on the right-hand side also becomes large. Such a trade-off (sparsity versus prediction accuracy) is empirically studied in Section 6. Our observation suggests that we can gain significant sparsity with only a small decrease of accuracy (that is, using a small ).
Now consider the case and . When , if we let and , then Theorem 3.1 implies that
In other words, if we let be the regularized loss, then the regularized regret is small when and . This implies that our procedure can be regarded as the online counterpart of -regularization methods. In the stochastic setting where the examples are drawn iid from some underlying distribution, the sparse online gradient method proposed in this paper solves the regularization problem.
5 Stochastic Setting
Stochastic-gradient-based online learning methods can be used to solve large-scale batch optimization problems, often quite successfully . In this setting, we can go through training examples one-by-one in an online fashion, and repeat multiple times over the training data. In this section, we analyze the performance of such a procedure using Theorem 3.1.
To simplify the analysis, instead of assuming that we go through the data one by one, we assume that each additional data point is drawn from the training data randomly with equal probability. This corresponds to the standard stochastic optimization setting, in which observed samples are iid from some underlying distributions. The following result is a simple consequence of Theorem 3.1. For simplicity, we only consider the case with and constant gravity .
Consider a set of training data for , and let
be the regularized loss over training data. Let , and define recursively for
where each is drawn from uniformly at random. If Assumption 3.1 holds, then at any time , the following inequalities are valid for all :
Proof. Note that the recursion of implies that
Because is convex in , the first inequality follows directly from Jensen’s inequality.
In the following we only need to prove the second inequality. Theorem 3.1 implies the following:
The second inequality is obtained by taking the expectation with respect to in (8).
If we let and , the bound in Theorem 3.2 becomes
That is, on average approximately solves the regularization problem
If we choose a random stopping time , then the above inequalities says that on average also solves this regularization problem approximately. Therefore in our experiment, we use the last solution instead of the aggregated solution .
Since 1-norm regularization is frequently used to achieve sparsity in the batch learning setting, the connection to 1-norm regularization can be regarded as an alternative justification for the sparse-online algorithm developed in this paper.
Truncated Gradient Algorithm for Least Squares
This may give a more aggressive truncation (thus sparsity) after every -th iteration. Since we do not have a theorem formalizing how much more sparsity one can gain from this idea, its effect will only be examined through experiments.
In many online learning situations (such as web applications), only a small subset of the features have nonzero values for any example . It is thus desirable to deal with sparsity only in this small subset rather than all features, while simultaneously inducing sparsity on all feature weights. Moreover, it is important to store only features with non-zero coefficients (if the number of features is so large that it cannot be stored in memory, this approach allows us to use a hashtable to track only the nonzero coefficients). We describe how this can be implemented efficiently in the next section.
For reference, we present a specialization of Theorem 3.1 in the following corollary that is directly applicable to Algorithm 1.
(Sparse Online Square Loss Regret) If there exists such that for all , , then for all , we have
where is the weight vector used for prediction at the -th step of Algorithm 1; is the data point observed at the -step.
This corollary explicitly states that the average square loss incurred by the learner (left term) is bounded by the average square loss of the best weight vector , plus a term related to the size of which decays as and an additive offset controlled by the sparsity threshold and the gravity parameter .
Efficient Implementation
We altered a standard gradient-descent implementation (Vowpal Wabbit ) according to algorithm 1. Vowpal Wabbit optimizes square loss on a linear representation via gradient descent (3) with a couple caveats:
The prediction is normalized by the square root of the number of nonzero entries in a sparse vector, . This alteration is just a constant rescaling on dense vectors which is effectively removable by an appropriate rescaling of the learning rate.
The prediction is clipped to the interval $$, implying that the loss function is not square loss for unclipped predictions outside of this dynamic range. Instead the update is a constant value, equivalent to the gradient of a linear loss function.
The learning rate in Vowpal Wabbit is controllable, supporting decay as well as a constant learning rate (and rates in-between). The program operates in an entirely online fashion, so the memory footprint is essentially just the weight vector, even when the amount of data is very large.
As mentioned earlier, we would like the algorithm’s computational complexity to depend linearly on the number of nonzero features of an example, rather than the total number of features. The approach we took was to store a time-stamp for each feature . The time-stamp was initialized to the index of the example where feature was nonzero for the first time. During online learning, we simply went through all nonzero features of example , and could “simulate” the shrinkage of after in a batch mode. These weights are then updated, and their time stamps are reset to . This lazy-update idea of delaying the shrinkage calculation until needed is the key to efficient implementation of truncated gradient. Specifically, instead of using update rule (6) for weight , we shrunk the weights of all nonzero feature differently by the following:
We note that such a lazy-update trick by maintaining the time-stamp information can be applied to the other two algorithms given in section 3. In the coefficient rounding algorithm (4), for instance, for each nonzero feature of example , we can first perform a regular gradient descent on the square loss, and then do the following: if is below the threshold and , we round to and set to .
This implementation shows that the truncated gradient method satisfies the following requirements needed for solving large scale problems with sparse features.
The algorithm is computationally efficient: the number of operations per online step is linear in the number of nonzero features, and independent of the total number of features.
The algorithm is memory efficient: it maintains a list of active features, and a feature can be inserted when observed, and deleted when the corresponding weight becomes zero.
Empirical Results
We applied Vowpal Wabbit with the efficiently implemented sparsify option, as described in the previous section, to a selection of datasets, including eleven datasets from the UCI repository , the much larger dataset rcv1 , and a private large-scale dataset Big_Ads related to ad interest prediction. While UCI datasets are useful for benchmark purposes, rcv1 and Big_Ads are more interesting since they embody real-world datasets with large numbers of features, many of which are less informative for making predictions than others. The datasets are summarized in Table 1.
The UCI datasets we used do not have many features, and it is expected that a large fraction of these features are useful for making predictions. For comparison purposes as well as to better demonstrate the behavior of our algorithm, we also added random binary features to those datasets. Each feature has value with probability and otherwise.
In the first set of experiments, we are interested in how much reduction in the number of features is possible without affecting learning performance significantly; specifically, we require the accuracy be reduced by no more than for classification tasks, and the total square loss be increased by no more than % for regression tasks. As common practice, we allowed the algorithm to run on the training data set for multiple passes with decaying learning rate. For each dataset, we performed -fold cross validation over the training set to identify the best set of parameters, including the learning rate , the sparsification rate , number of passes of the training set, and the decay of learning rate across these passes. This set of parameters was then used to train Vowpal Wabbit on the whole training set. Finally, the learned classifier/regressor is evaluated on the test set. We fixed and in these experiments, and will study the effects of and in later subsections.
Figure 1 shows the fraction of reduced features after sparsification is applied to each dataset. For UCI datasets with randomly added features, Vowpal Wabbit is able to reduce the number of features by a fraction of more than , except for the ad dataset in which only reduction is observed. This less satisfying result might be improved by a more extensive parameter search in cross validation. However, if we can tolerate decrease in accuracy (instead of as for other datasets) during cross validation, Vowpal Wabbit is able to achieve reduction, indicating that a large reduction is still possible at the tiny additional cost of accuracy loss. With this slightly more aggressive sparsification, the test-set accuracy drops from (when only loss in accuracy is allowed in cross validation) to , while the accuracy without sparsification is .
Even for the original UCI datasets without artificially added features, Vowpal Wabbit manages to filter out some of the less useful features while maintaining the same level of performance. For example, for the ad dataset, a reduction of is achieved. Compared to the results above, it seems the most effective feature reductions occur on datasets with a large number of less useful features, exactly where sparsification is needed.
For rcv1, more than of features are removed after the sparsification process, indicating the effectiveness of our algorithm in real-life problems. We were not able to try many parameters in cross validation because of the size of rcv1. It is expected that more reduction is possible when a more thorough parameter search is performed.
The previous results do not exercise the full power of the approach presented here because they are applied to datasets where standard Lasso regularization is or may be computationally viable. We have also applied this approach to a large non-public dataset Big_Ads where the goal is predicting which of two ads was clicked on given context information (the content of ads and query information). Here, accepting a increase in classification error allows us to reduce the number of features from about to about , a factor of decrease in the number of features.
For classification tasks, we also study how our sparsification solution affects AUC (Area Under the ROC Curve), which is a standard metric for classification. We use AUC here and in later subsections because it is insensitive to threshold, which is unlike accuracy. Using the same sets of parameters from -fold cross validation described above, we find that the criterion is not affected significantly by sparsification and in some cases, they are actually slightly improved. The reason may be that our sparsification method remove some of the features that could have confused Vowpal Wabbit . The ratios of the AUC with and without sparsification for all classification tasks are plotted in Figures 2. It is often the case that these ratios are above .
2 The Effects of KK
As we argued before, using a value larger than may be desired in truncated gradient and the rounding algorithms. This advantage is empirically demonstrated here. In particular, we try , , and in both algorithms. As before, cross validation is used to select parameters in the rounding algorithm, including learning rate , number of passes of data during training, and learning rate decay over training passes.
Figures 3 and 4 give the AUC vs. number-of-feature plots, where each data point is generated by running respective algorithm using a different value of (for truncated gradient) and (for the rounding algorithm). We used in truncated gradient.
For truncated gradient, the performances with or are at least as good as those with , and for the spambase dataset further feature reduction is achieved at the same level of performance, reducing the number of features from (when ) to (when or ) with of an AUC of about .
Such an effect is even more remarkable in the rounding algorithm. For instance, in the ad dataset the algorithm using achieves an AUC of with features, while and features are needed using and , respectively.
3 The Effects of θ\theta in Truncated Gradient
In this subsection, we empirically study the effect of in truncated gradient. The rounding algorithm is also included for comparison due to its similarity to truncated gradient when . As before, we used cross validation to choose parameters for each value tried, and focused on the AUC metric in the eight UCI classification tasks, except the degenerate one of wpbc. We fixed in both algorithm.
Figure 5 gives the AUC vs. number-of-feature plots, where each data point is generated by running respective algorithms using a different value of (for truncated gradient) and (for the rounding algorithm). A few observations are in place. First, the results verify the observation that the behavior of truncated gradient with is similar to the rounding algorithm. Second, these results suggest that, in practice, it may be desired to use in truncated gradient because it avoids the local minimum problem.
4 Comparison to Other Algorithms
The next set of experiments compares truncated gradient descent to other algorithms regarding their abilities to tradeoff feature sparsification and performance. Again, we focus on the AUC metric in UCI classification tasks except wpdc. The algorithms for comparison include:
The truncated gradient algorithm: We fixed and , used crossed-validated parameters, and altered the gravity parameter .
The rounding algorithm described in section 3.1: We fixed , used cross-validated parameters, and altered the rounding threshold .
The subgradient algorithm described in section 3.2: We fixed , used cross-validated parameters, and altered the regularization parameter .
The Lasso for batch regularization: We used a publicly available implementation .
Note that we do not attempt to compare these algorithms on rcv1 and Big_Ads simply because their sizes are too large for the Lasso and subgradient descent (c.f., section 5).
Figure 6 gives the results. First, it is observed that truncated gradient is consistently competitive with the other two online algorithms and significantly outperformed them in some problems. This suggests the effectiveness of truncated gradient.
Second, it is interesting to observe that the qualitative behavior of truncated gradient is often similar to that of LASSO, especially when very sparse weight vectors are allowed (the left sides in the graphs). This is consistent with theorem 3.2 showing the relation between these two algorithms. However, LASSO usually has worse performance when the allowed number of nonzero weights is set too large (the right side of the graphs). In this case, LASSO seems to overfit. In contrast, truncated gradient is more robust to overfitting. The robustness of online learning is often attributed to early stopping, which has been extensively discussed in the literature (e.g., in ).
Finally, it is worth emphasizing that the experiments in this subsection try to shed some light on the relative strengths of these algorithms in terms of feature sparsification. For large datasets such as Big_Ads only truncated gradient, coefficient rounding, and the sub-gradient algorithms are applicable to large-scale problems with sparse features. As we have shown and argued, the rounding algorithm is quite ad hoc and may not work robustly in some problems, and the sub-gradient algorithm does not lead to sparsity in general during training.
Conclusion
This paper covers the first sparsification technique for large-scale online learning with strong theoretical guarantees. The algorithm, truncated gradient, is the natural extension of Lasso-style regression to the online-learning setting. Theorem 3.1 proves that the technique is sound: it never harms performance much compared to standard stochastic gradient descent in adversarial situations. Furthermore, we show that the asymptotic solution of one instance of the algorithm is essentially equivalent to Lasso regression, and thus justifying the algorithm’s ability to produce sparse weight vectors when the number of features is intractably large.
The theorem is verified experimentally in a number of problems. In some cases, especially for problems with many irrelevant features, this approach achieves a one or two order of magnitude reduction in the number of features.
References
Appendix A Proof of Theorem 3.1
The following lemma is the essential step in our analysis.
For update rule (6) applied to weight vector on example with gravity parameter , resulting in a weight vector . If Assumption 3.1 holds, then for all , we have
Then the update equation implies the following:
The third inequality follows from the definition of sub-gradient of a convex function, which implies that
for all and . The fourth inequality follows from Assumption 3.1. Rearranging the above inequality leads to the desired bound.
Proof. (of theorem 3.1) Apply Lemma A.1 to the update on trial , we have
Now summing over , we obtain
The first equality follows from the telescoping sum and the second inequality follows from the initial condition (all weights are zero) and dropping negative quantities. The theorem follows by dividing with respect to and rearranging terms.