Repository navigation
Conversation
|
I am not convinced by this test, at all. Was it AI generated? To name one reason alone (there are more): Same penalty for different X means a different optimisation problem. |
Well in that case I believe it's the same optimization problem in the sense of being mathematically equivalent. And with f64 precision, they do get the same solution. But maybe you meant it's not the same optimization landscape or something like that? Still, one could expect that a solver avoids silently returning a solution off by several order of magnitude (well ideally, I do get it's not an easy-to-meet expectation!). |
|
I don’t see a compelling reason why the compared coefficients should match. |
|
Well because it's the same argmin? 😅 Sorry, I don't get what doesn't convince you. Coefficients do match for all linear models for small enough offset/variance ratio. And for Ridge/LinearRegression/Lasso/ElasticNet, they basically always match. So it looks like when they do not match it's some kind of numerical stability artifact. |
|
Ok clear thanks! Here is the explanation, I thought it was a common "trick" for linear models, so I thought you would find it obvious (I'm just getting familiar with linear models, and I'm relying on AI to understand things, when it explained this, I thought it looked as a well known fact on linear models): For any fixed slope β, shifting
Given that the intercept is not penalized (except for liblinear I believe), the penalized objective as a function of β is the same curve for That's why you could expect this test to pass (and it does for many similar cases/models, which is empirically convincing IMO). |
|
Almost:
If it's still unclear, let's have a call. |
Edit: ah you meant the intercept, not the slope. Sorry. The intercept is not penalized so there is no
Yes, I agree the intercept changes, but not the slope/coefficients |
|
Changed my mind. You are right for OLS and Ridge. Once the optimization over the intercept is carried out analytically, the same optimisation problem remains, with or without centering X. To my knowledge, the centering trick does not work for GLMs. And you use |
|
I'm not sure what you mean about the centering trick here. If it's what I do in #34793 indeed I don't think you can do the same thing for But the identity |
I don’t think so. Let’s have a call and have a break from written discussion. |
|
Again, let's talk (my preferred resolution). Apart from that, interesting things happen: import numpy as np
from sklearn._loss import HalfBinomialLoss
from sklearn.linear_model._linear_loss import LinearModelLoss
from sklearn.linear_model import LogisticRegression
rng = np.random.RandomState(0)
n_samples, n_features = 5000, 5
X_centered = rng.normal(size=(n_samples, n_features)) * 1e-5
#X_centered -= X_centered.mean(axis=0)[None, :]
X = X_centered + 3
true_coef = rng.normal(size=n_features)
proba = 1 / (1 + np.exp(-(X_centered @ true_coef)))
y = (rng.uniform(size=n_samples) < proba).astype(int)
lml = LinearModelLoss(base_loss=HalfBinomialLoss(), fit_intercept=True)
def obj(model, X, y):
n_samples = X.shape[0]
coef = np.r_[model.coef_[0], model.intercept_]
l1_reg = model.l1_ratio / (model.C * n_samples)
l2_reg = (1 - model.l1_ratio) / (model.C * n_samples)
loss = lml.loss(
coef, X=X.astype(np.float64), y=y.astype(np.float64),
l1_reg_strength=l1_reg, l2_reg_strength=l2_reg
)
return float(loss)
params = dict(C=1, tol=1e-12, max_iter=20000)
m1 = LogisticRegression(solver="lbfgs", **params).fit(X_centered, y)
m2 = LogisticRegression(solver="lbfgs", **params).fit(X, y)
m3 = LogisticRegression(solver="newton-cholesky", **params).fit(X_centered, y)
m4 = LogisticRegression(solver="newton-cholesky", **params).fit(X, y)
print(m1.coef_)
print(m2.coef_)
print(m3.coef_)
print(m4.coef_)print(obj(m1, X_centered, y))
print(obj(m2, X, y))
print(obj(m3, X_centered, y))
print(obj(m4, X, y))What happens to float32 LogisticRegression(solver="newton-cholesky", **params, verbose=4).fit(X.astype(np.float32), y) |
NOT FOR REVIEW
Reference Issues/PRs
Emanated from numerical stability discussions in #34793
AI usage disclosure
I used AI assistance for: