Skip to content

API/REF: Clone before fitting in Incremental.fit - #258

Merged
TomAugspurger merged 11 commits into
dask:masterfrom
TomAugspurger:incremental-clone
Jul 3, 2018
Merged

TomAugspurger merged 11 commits into
dask:masterfrom
TomAugspurger:incremental-clone

Conversation

@TomAugspurger

@TomAugspurger TomAugspurger commented Jul 2, 2018 •

Copy link
Copy Markdown
Member

This changes Incremental.fit to clone before fitting.

Alternative to #207

This implements the ideas from #207 (comment)

This changes Incremental.fit to clone before fitting.

Alternative to dask#207
@TomAugspurger

Copy link
Copy Markdown
Member Author

cc @stsievert

Comment thread tests/test_incremental.py

assert isinstance(result.estimator.coef_, np.ndarray)
np.testing.assert_array_almost_equal(result.estimator.coef_,
assert isinstance(result.estimator_.coef_, np.ndarray)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the API change.

Comment thread tests/test_incremental.py
assert callable(check_scoring(clf, scoring=scoring))
clf.fit(X, y, classes=np.unique(y))
clf.score(X, y)
clf.estimator.score(X, y)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this was doing what we wanted earlier.

When Incremental is created with scoring, clf.score(X, y) will in general be different then clf.estimator_.score(X, y). The latter always uses the "default" scoring for that estimator (e.g. accuracy score). I opted to just remove this line.

Comment thread tests/test_incremental.py
def test_fit_ndarrays():
X = np.ones((10, 5))
y = np.ones(10)
y = np.concatenate([np.zeros(5), np.ones(5)])

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needed a mix of zeros and ones. I'm not entirely sure what changed to necessitate that...

Comment thread tests/test_incremental.py

inc.partial_fit(X, y, classes=[0, 1])
inc.fit(X, y)
inc.fit(X, y, classes=[0, 1])

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

need this, as classes_ no longer leasks through the partial_fit to the fit (a good thing!)

@stsievert

Copy link
Copy Markdown
Member

I think if we implement fit support for Incremental we should loop over the data max_iter times beforehand.

For more complete thoughts on the API, see #207 (comment)

@TomAugspurger

Copy link
Copy Markdown
Member Author

Agreed that having a max_iter parameter would be useful for Incremental. However, exposes a difficulty alluded to in #200 and how Incremental.get/set_params works. It would be ambiguos whether max_iter should go to Incremental.max_iter or Incremental.estimator.max_iter.

The solution is to probably to have Incremental to specify a name when creating the Incremental.

See https://stackoverflow.com/a/35534452/1889400 for how VotingClassifier works.

So either

inc = Incremental(SGDClassifier(), name='sgdclassifier')
inc = Incremental(('sgdclassifier', SGDClassifier())

In the latter case, we would have a make_incremental helper that works like make_pipeline.

Then in a context where you need to get / set params, it'd be

param_grid = {
    'sgdclassifier__alpha': [0.1, 10.0],
    'sgdclassifier__max_iter': [1000, 2000],
    'max_iter': [1, 5],
}

clf = GridSearchCV(inc, param_grid)

@TomAugspurger

TomAugspurger commented Jul 2, 2018 •

Copy link
Copy Markdown
Member Author

Looks like https://github.com/scikit-learn/scikit-learn/blob/master/sklearn/utils/metaestimators.py has some utilities for get / set params in a meta-estimator, though most are private.

@TomAugspurger

Copy link
Copy Markdown
Member Author

55fccf2 has the changes.

Turns out BaseEstimator already does the right thing, we don't have to mess with _BaseComposition.

I'm encouraged by the lines of code we could delete from Incremental.

For now, I haven't made the name configurable. If you want to set the parameters for the underlying estimator, then you use estimator__<parameter>. We can make the estimator part configurable in the future if there's a demand for it.

@TomAugspurger

Copy link
Copy Markdown
Member Author

I'll implement max_iter now.

@stsievert, do you have a feeling for what a good default would be? I'm leaning towards max_iter=1, but am not sure.

@mrocklin

mrocklin commented Jul 2, 2018 via email

Copy link
Copy Markdown
Member

@TomAugspurger

Copy link
Copy Markdown
Member Author

It varies from model to model.

scikit-learn/scikit-learn#1626 is the general API that we would want to follow here, when that is implemented.

@TomAugspurger TomAugspurger changed the title API/REF: Clone before fitting in Incremental.fit API/REF: Clone before fitting in Incremental.fit and implement max_iter Jul 2, 2018
@TomAugspurger

Copy link
Copy Markdown
Member Author

A basic version of max_iter should be working now.

@TomAugspurger

Copy link
Copy Markdown
Member Author

I'm finding this diff somewhat difficult to digest. I'm going to split off the max_iter to a second PR.

@TomAugspurger

Copy link
Copy Markdown
Member Author

OK I think this PR should be good. I feel happy about deleting https://github.com/dask/dask-ml/pull/258/files#diff-eaec819577a74de3b3af2ece0ac4dce9L353, and should have noticed that needing those in the first place was a code smell.

Postponing discussion of max_iter, which will build on this, to #264.

@TomAugspurger TomAugspurger changed the title API/REF: Clone before fitting in Incremental.fit and implement max_iter API/REF: Clone before fitting in Incremental.fit Jul 2, 2018
@stsievert

Copy link
Copy Markdown
Member

I'm on board with this now. I think Incremental should implement fit and have a max_iter param.

do you have a feeling for what a good default would be? I'm leaning towards max_iter=1, but am not sure.

I think I would default to max_iter=5 – that's what sklearn's SGDClassifier max_iter defaults to, at least when no stopping criterion set in <v0.21. They set some stopping criteria in >= v0.21 and increase max_iter. For simple problems I'd say that 5 epochs is good enough.

This should depend on model complexity but I'm okay leaving that. e.g., fitting a linear model with twice as many features will require about sqrt(2) more samples to be seen by the optimization algorithm.

@TomAugspurger
TomAugspurger merged commit d580158 into dask:master Jul 3, 2018
@TomAugspurger
TomAugspurger deleted the incremental-clone branch July 3, 2018 02:26
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

Successfully merging this pull request may close these issues.

3 participants