Skip to content
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

Using callback for CrossVal #94

Open
ksehic opened this issue Feb 4, 2021 · 0 comments
Open

Using callback for CrossVal #94

ksehic opened this issue Feb 4, 2021 · 0 comments

Comments

@ksehic
Copy link

ksehic commented Feb 4, 2021

Hi @QB3 @mathurinm

I was running your CrossVal example with callback function to get MSPE on test data and I got the error message

"ValueError: matmul: Input operand 1 does not have enough dimensions (has 0, gufunc core with signature (n?,k),(k,m?)->(n?,m?) requires 1)"

Thanks!

import numpy as np
import sklearn

from sklearn.linear_model import LassoCV
from sklearn.datasets import make_regression
from sklearn.model_selection import KFold
from sklearn.metrics import mean_squared_error
from sklearn.model_selection import train_test_split

from sparse_ho import ImplicitForward, grad_search
from sparse_ho.models import Lasso
from sparse_ho.criterion import HeldOutMSE, CrossVal
from sparse_ho.optimizers import LineSearch
from sparse_ho.utils import Monitor
from sparse_ho.utils_plot import discrete_cmap


dataset = 'simu'

if dataset == 'rcv1':
    X, y = fetch_libsvm('rcv1_train')
else:
    X, y = make_regression(
        n_samples=500, n_features=1000, noise=40,
        random_state=42)
        
X, X_test, y, y_test = train_test_split(
        X, y, test_size=0.15, random_state=0)

kf = KFold(n_splits=5, shuffle=True, random_state=42)

n_samples = len(y)
alpha_max = np.max(np.abs(X.T.dot(y))) / n_samples

n_alphas = 10
alphas = np.geomspace(alpha_max, alpha_max / 1_000, n_alphas)

tol = 1e-8
max_iter = 1e5

estimator = sklearn.linear_model.Lasso(
    fit_intercept=False, max_iter=1000, warm_start=True, tol=tol)

estimator = sklearn.linear_model.Lasso(
    fit_intercept=False, max_iter=1000, warm_start=True, tol=tol)
model = Lasso()
criterion = HeldOutMSE(None, None)
alpha0 = alpha_max / 10

objs_test = []


def callback(val, grad, mask, dense, alpha):
    # The custom quantity is added at each outer iteration:
    # here the prediction MSE on test data
    objs_test.append(mean_squared_error(X_test[:, mask] @ dense, y_test))

monitor_grad = Monitor(callback=callback)
cross_val_criterion = CrossVal(criterion, cv=kf)
algo = ImplicitForward()
optimizer = LineSearch(n_outer=10, tol=tol)
grad_search(
    algo, cross_val_criterion, model, optimizer, X, y, alpha0,
    monitor_grad)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant