-
-
Notifications
You must be signed in to change notification settings - Fork 26k
Fix sample weight handling in SAG(A) #31675
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
Running the test as follows import numpy as np
from scipy.stats import kstest
from sklearn.linear_model.tests.test_sag import sag, squared_dloss, get_step_size
from sklearn.datasets import make_regression, make_classification
from sklearn.utils._testing import assert_allclose_dense_sparse
alpha=1
n_features = 6
rng = np.random.RandomState(0)
X, y = make_classification(n_samples=1000,random_state=77,n_features=n_features)
weights = rng.randint(0,5,size=X.shape[0])
X_repeated = np.repeat(X,weights,axis=0)
y_repeated = np.repeat(y,weights,axis=0)
weights_w_all = np.zeros([n_features,50])
weights_r_all = np.zeros([n_features,50])
step_size_w=get_step_size(X,alpha,True,sample_weight=weights)
step_size_r= get_step_size(X_repeated,alpha,True)
for random_state in np.arange(50):
weights_w, int_w = sag(X,y,step_size=step_size_w,sample_weight=weights,alpha=alpha,dloss=squared_dloss,random_state=random_state)
weights_w_all[:,random_state] = weights_w
weights_r, int_r = sag(X_repeated,y_repeated,step_size=step_size_r,alpha=alpha,dloss=squared_dloss,random_state=random_state)
weights_r_all[:,random_state] = weights_r
print(kstest(weights_r_all[0],weights_w_all[0])) Now gives the result
|
@snath-xoc at the meeting, you mentioned that the statistical test would not pass for some datasets. Could you please post an example and add a TODO item to the PR to investigate this problem. Also, whenever penalty is non-zero, the problem is strictly convex and the solution show be unique. So it should be possible to write deterministic tests (with various random seed values) instead of statistical tests to:
This might require setting |
if sample_weight is not None: | ||
gradient *= sample_weight[idx] | ||
|
||
update = entry * gradient + alpha * weights |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You should keep the gradient
multiplied by the sample_weight
even if you change the sampling of idx, otherwise you are not computing the correct loss gradient.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The loss with weights
coefficient of the linear model (skipping the intercept for now):
can be put in several ways as the finite sum minimized by sag(a):
Let
-
$f_i(w) = s_i l_i(w) + s_i \frac{\alpha}{2} \Vert w \Vert^2 $ . The gradient updated and stored in memory is thenupdate = sample_weight[idx] * (entry * gradient + alpha * weights)
-
$f_i(w) = s_i l_i(w) + \frac{S}{n} \frac{\alpha}{2} \Vert w \Vert^2$ . This gives the code in main where only the data$i$ loss term is weighted. -
$f_i(w) = s_i l_i(w)$ . The gradient updated and stored in memory is thenupdate = sample_weight[idx] * entry * gradient
, ie only the data$i$ loss term. The regularizer is then taken into account when doing the gradient descent step onweights
, for exampleweights -= alpha*step_size*weights
.
I feel 2. (the current code) is a bit weird, for instance there is maybe a weird scaling of alpha
as weights
at the end, so it shouldn't matter too much.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think I have a slight preference for option 1. with the meaning of adding a term proportional to n_seen
as the weighted sum of seen indices, which will converge to
def get_step_size(X, alpha, fit_intercept, classification=True, sample_weight=None): | ||
if sample_weight is None: | ||
X_prod = np.sum(X * X, axis=1) | ||
else: | ||
X = X[sample_weight != 0] | ||
X_prod = np.sum(X * X, axis=1) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think removing the zero sample_weight is enough.
If I understood correctly the sag(a) paper the recommended step size is step_size = 1 / L
where
It will depend on how we decompose the weighted sum into individual
-
$L_i = s_i \kappa \Vert x_i \Vert^2 + s_i \alpha$ . -
$L_i = s_i \kappa \Vert x_i \Vert^2 + \frac{S}{n} \alpha$ .
where
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the end, it depends on how we allocate the regularizer:
- evenly wrt the indices, each
$i$ has the same regularizer$\frac{S}{n} \frac{\alpha}{2}\Vert w \Vert^2$ - evenly wrt the
sample_weight
, each$i$ has a regularizer$s_i \frac{\alpha}{2}\Vert w \Vert^2$ proportional to itssample_weight
.
Maybe we should try both strategies and see which one leads to better convergence ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we first need to figure out the step size computation before trying non-uniform sampling, for example by following 5.5 Effect of non-uniform sampling of the sag paper.
Reference Issues/PRs
Fixes issue on sample weight handling within SAG(A) #31536.
What does this implement/fix? Explain your changes.
SAG(A) now accounts for sample weights by:
TO DO:
get_auto_step_size