Skip to content

ENH: add an example with dask-ml's Incremental - #15

Merged
mrocklin merged 18 commits into
dask:masterfrom
stsievert:incremental
Jul 5, 2018
Merged

mrocklin merged 18 commits into
dask:masterfrom
stsievert:incremental

Conversation

@stsievert

Copy link
Copy Markdown
Member

I'd appreciate a review of this. I tried to point out the features and limitations of Incremental, and walked through a basic training + testing example. I included a note about dask/dask-ml#247, and how Incremental is responsible for data management and sklearn is responsible for the computation on the data.

A rendered notebook is available at https://gist.github.com/stsievert/fbd2da97924e827bdb2f015aff7fe9b1

cc @TomAugspurger @mrocklin

@stsievert stsievert changed the title ENH: add an example with dask-ml's Incremental WIP: ENH: add an example with dask-ml's Incremental Jun 30, 2018
@stsievert

Copy link
Copy Markdown
Member Author

I've made some edits, viewable on the same gist: https://gist.github.com/stsievert/fbd2da97924e827bdb2f015aff7fe9b1

This is ready for review now.

@stsievert stsievert changed the title WIP: ENH: add an example with dask-ml's Incremental ENH: add an example with dask-ml's Incremental Jun 30, 2018

@TomAugspurger TomAugspurger left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Looks great, thanks!

Could you create a distriubted Client, and encourage people to look at the UI while this is running?

Could you add a concluding thoughts section, reiterating

  1. We used Dask to manage the data. You write normal NumPy-like code, and don't have to mess with writing a for loop to page chunks of data in
  2. The training is sequential, and done entirely be scikit-learn.
  3. The sequential training isn't a huge hindrance, as we can do parallelism at a higher level (GridSearchCV).

Comment thread machine-learning-scikit-learn.ipynb Outdated
"cell_type": "markdown",
"metadata": {},
"source": [
"Scikit-learn is a popular machine learning library, and implements the minimal API required for training machine learning models. Their library encompasses a wide variety of problem formulations, ranging from simple linear models to more complex clustering or decomposition formulations. Scikit-learn is hindered by computation (what machine library isn't?). This has even gotten a page in their user guide, \"[Strategies to scale computationally: bigger data][strat]\".\n",

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

minimal API required

could be interpreted positively or negatively :)

Perhaps something like "whose API has inspired many other libraries"?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

"Their library" -> "It"

It's OSS, it's everyone's library :)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Scikit-learn is hindered by computation

I like to phrase this as "Scikit-learn works great for problems that fit on a single machine, but struggles for larger problem"

Comment thread machine-learning-scikit-learn.ipynb Outdated
"\n",
"Using scikit-learn estimators with Dask-ML helps scale to large datasets. All the computation remains on scikit-learn's shoulders but the data management is handled by Dask. This allows scaling to large datasets distributed across many machines, or to datasets that do not fit in memory.\n",
"\n",
"This example wraps a scikit-learn estimator with a Dask-ML estimator suited for incremental learning. This Dask-ML estimator manages feeding data to Scikit-learn, and leaves the computation to Scikit-learn. This example will not cover the other algorithms inside of Dask-ML that are well-suited for Dask's distributed architecture.\n",

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This Dask-ML estimator manages feeding data to Scikit-learn, and leaves the computation to Scikit-learn.

redudentant with the previous paragraph.

Comment thread machine-learning-scikit-learn.ipynb Outdated
"\n",
"This example will show\n",
"\n",
"* wrapping a scikit-learn estimator that implement `partial_fit` with `dask_ml`\n",

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

implement -> implements.

You could maybe strike scikit-learn. It's work on any estimator that implements partial_fit (and the rest of the estimator API)

Comment thread machine-learning-scikit-learn.ipynb Outdated
"metadata": {},
"source": [
"## Data creation\n",
"We will create many synthetic data. We are more interested in showing the plumbing this data can flow through than cool insights we can glean from the data.\n",

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Team plural data!

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

"showing the plumbing this" -> "showing how this data flows through the estimator"?

Comment thread machine-learning-scikit-learn.ipynb Outdated
"import numpy as np\n",
"n, d = int(100e3), 100\n",
"\n",
"X = da.random.normal(size=(n, d), chunks=n // 10)\n",

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

dask/dask-ml#255 would cover this right?

dsak_ml.datasets.make_classification has some knobs over n_informative, etc. I'm not sure if all of them are implemented.

I agree that for these examples, the actual data are irrelevant, so ideally this would be as simple as possible.

@stsievert stsievert Jul 1, 2018 •

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.

dask/dask-ml#255 would cover this right?

It would help. I use make_classification now – I think I was creation of my own matrices for ground truth, but I don't actually use it later.

Comment thread machine-learning-scikit-learn.ipynb Outdated
"metadata": {},
"outputs": [],
"source": [
"y_hat = inc.predict(X_test)\n",

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think scikit-learn uses y_pred, so people may be more familiar with that (though I'm partial to y_hat, in this case it's best to go where the people are).

Comment thread machine-learning-scikit-learn.ipynb Outdated
"outputs": [],
"source": [
"y_hat = inc.predict(X_test)\n",
"errors = np.abs(y_hat - y_test) / 2 # divide by 2 to get 0-1 labels\n",

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

dask_ml.metrics.accuracy_score

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 was using raw dask arrays to provide some basics on how to use them. I've tried to clarify that, and re-ordered too.

Comment thread machine-learning-scikit-learn.ipynb Outdated
"cell_type": "markdown",
"metadata": {},
"source": [
"This accuracy might be the best possible, but there a lot of parameters to feed `SGDClassifier` that can drastically change the performance. We'll consider two of the most basic parameters, specifying a different loss function and different amount of penalty. This is the problem of hyperparameter optimization, something Dask-ML supports: https://dask-ml.readthedocs.io/en/latest/hyper-parameter-search.html\n",

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Is the first phrase missing a "not"? Somethying like

"This accuracy might not be the best possible; there are a lot of parameters..."

Comment thread machine-learning-scikit-learn.ipynb Outdated
"outputs": [],
"source": [
"from dask_ml.model_selection import GridSearchCV\n",
"params = {'alpha': np.logspace(-6, 0, num=10),\n",

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I would write this like:

# small parameter grid for the example
params = {
    'alpha': [0.1, 10],
    'loss': ['hingle', 'log'],
}

# An example of a larger parameter grid.
# params = {
#    'alpha': np.logspace(-6, 0, num=10),
#    'loss': ['hinge', 'log', 'modified_huber', 'squared_hinge', 'perceptron']
# }

Comment thread machine-learning-scikit-learn.ipynb Outdated
"metadata": {},
"outputs": [],
"source": [
"import seaborn as sns\n",

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I slightly prefer

g = sns.factorplot(x='param_alpha', y='mean_test_score', data=df,
                   aspect=3,
                   hue='param_loss')

for visualizing these.

gh

ticklabels would need to be cleaned up.

will also need to ensure seaborn is available on the binder worker.

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 greatly prefer this instead of the heatmap.

@TomAugspurger

Copy link
Copy Markdown
Member

Hmm I had a review pending, but apparently lost it.

Overall, the content here is solid. We may want to wait on the resolution to dask/dask-ml#258, as that'll be an API breaking change.

@stsievert stsievert changed the title ENH: add an example with dask-ml's Incremental WIP: ENH: add an example with dask-ml's Incremental Jul 2, 2018
@stsievert stsievert changed the title WIP: ENH: add an example with dask-ml's Incremental ENH: add an example with dask-ml's Incremental Jul 3, 2018
@stsievert

Copy link
Copy Markdown
Member Author

Could I have another review on this PR? I'm satisfied with the prose and the performance (which has spawned a couple other issues and PRs)

@stsievert

Copy link
Copy Markdown
Member Author

I've pushed the notebook with outputs. I can strip them before removal depending on #16.

@mrocklin

mrocklin commented Jul 3, 2018

Copy link
Copy Markdown
Member

Some comments, mostly language related. My apologies for not going in-line:

  • Avoid words like plethora that may not be known to non-native english speakers
  • Similarly with phrases like "ever hear of teaching to the test?" This can be confusing if you don't know what it means, which is possibly most of our readers. This then diminishes leads you into a tangent of trying to figure out if teaching is another term for training/fitting, etc..
  • More generally, we should probably avoid jokes and cleverness in all prose except perhaps blogposts. If you were to reduce the amount of prose in this notebook by a factor of two or three I think that that would be for the best. A user probably wants to move through this in a couple minutes. We should endeavor to give them the minimum amount of information that they need to move on. They will appreciate that we respect their time.
  • Remove phrases like "as usual" and "of course" which assume that the user knows what's going on. A wide variety of users will read these examples, and for many of them all of this will be new.
  • I recommend removing the manual computation of accuracy. I get that it can be useful to show them dask.arrays, but they can also look at the dask array examples for this if that's their interest. Lets focus in on the use of Incremental
  • I also recommend removing the GridSearch section for the same reason. Lets get them in and out quickly.
  • Calling foo.persist(); bar.persist() is sort of an anti-pattern. It's nicer to call them all at once with the dask.persist function.

As an example, here is what I recommend for how to handle persist and da.unique. I recommend the following cells directly after creating the training data.

### Persist data in memory

This dataset is small enough to fit in distributed memory, so we call `dask.persist` now.

    X_train, y_train, X_test, y_test = dask.persist(X_train, y_train, X_test, y_test)

If you are working in a situation where your dataset does not fit in memory then you should skip this step.  Everything will still work, but will be slower and use less memory.

### Pre-compute classes

We pre-compute the classes from our training data

    classes = da.unique(y_train).compute()
    classes

...

### Create Scikit-Learn Estimator
 
Your explanation looks good (other than "as usual")

    sgd = ...

### Wrap with Dask-ML Incremental 

Incremental is a meta-estimator that ...

I've found that this level of prose/code is easily digestible by peole with short attention spans (most readers). Separating things out by section headers with brief informative names helps them to understand the scope of the step of the computation that they are about to learn about.

Comment thread machine-learning-scikit-learn.ipynb Outdated
"cell_type": "markdown",
"metadata": {},
"source": [
"Scikit-learn is a popular machine learning library whose API is easy to use because it has been inspired by other machine learning libraries. It encompasses a wide variety of problem formulations, ranging from simple linear models to more complex clustering or decomposition formulations. Scikit-learn works great for problems that fit on a single machine, but struggles for larger problems. This has even gotten a page in their user guide, \"[Strategies to scale computationally: bigger data][strat]\".\n",

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I'm not sure about the origins of scikit-learn's design, but I often see it cited as the inspiration for other APIs (e.g. https://spark.apache.org/docs/latest/ml-pipeline.html#main-concepts-in-pipelines)

Comment thread machine-learning-scikit-learn.ipynb Outdated
"cell_type": "markdown",
"metadata": {},
"source": [
"Creating a distributed scheduler will provide good feedback because we can view the dask.distributed dashboard. This will provide progress and performance metrics, including visualization of jobs that are running."

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think this is redundant with the previous.

Comment thread machine-learning-scikit-learn.ipynb Outdated
"cell_type": "markdown",
"metadata": {},
"source": [
"Now, let's call `partial_fit` multiple times.\n",

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I would remove this block, since implementing max_iter for Incremental will I think make it obsolete?

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 want to leave this block we're show casing Incremental, which will very likely be used for other tools to get some idea or progress/etc. I would imagine that the data dictionary I use will be used, and probably with more data.

I have restructered a bit, to move the persisting data section below the "calling partial_fit" header.

Comment thread machine-learning-scikit-learn.ipynb Outdated
"cell_type": "markdown",
"metadata": {},
"source": [
"There a lot of parameters to feed `SGDClassifier`, and this accuracy depends on those parameters, and it may not be as high as it can be. We'll consider two of the most basic parameters, specifying a different loss function and different amount of penalty. This is the problem of hyperparameter optimization, something Dask-ML supports: https://dask-ml.readthedocs.io/en/latest/hyper-parameter-search.html\n",

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Typo "There a".

@stsievert

stsievert commented Jul 5, 2018 •

Copy link
Copy Markdown
Member Author

I have addressed both @mrocklin and @TomAugspurger's comments, and made some more edits. I've pushed the notebook with the outputs again.

I also recommend removing the GridSearch section for the same reason. Lets get them in and out quickly.

Good idea. I've removed the model selection section. I have briefly mentioned this alongside the Dask joblib integration in a "Learn more" section.

@TomAugspurger

TomAugspurger commented Jul 5, 2018 via email

Copy link
Copy Markdown
Member

@mrocklin

mrocklin commented Jul 5, 2018

Copy link
Copy Markdown
Member

I'm taking a look through this now. I may make a few edits directly if that's ok.

@stsievert

Copy link
Copy Markdown
Member Author

I may make a few edits directly if that's ok.

Thank you, it's appreciated. That's more than okay.

mrocklin added 3 commits July 5, 2018 10:41
1.  Change title to focus on incremental, rather than all of
scikit-learn
2.  Remove a lot of general content in the header
3.  Persist data early on, briefly explain why
4.  Increase the dataset size by an order of magnitude to make the
    examples somewhat heavier.  This targets the 1-2GB dataset size for
    the Binder environment
5.  Replace final learn more section with links from the notebook
This isn't only about joblib, and moving notebook links will cause
existing links in the wild to break.
@mrocklin

mrocklin commented Jul 5, 2018

Copy link
Copy Markdown
Member

@stsievert I've changed things to make them in line with my preferences. Can you look things over and make sure that they're still consistent with your intent?

@stsievert stsievert left a comment

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 looks pretty good, but the introduction could use some work. The biggest edits I made were adding some motivation for calling dask.persist.

" df = pandas.read_csv(filename)\n",
" x, y = ... # get data from CSV using the Pandas API\n",
" est.partial_fit(x, y)\n",
"```\n",

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.

Do you have any examples for "this is a common operation"? The first thing I would do is shuffle the dataset before calling the optimizer on each block.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Are you referring to this sentence:

A common approach to training large datasets is to incrementally train a model on many batches of data sequentially.

This was my understanding, but I could definitely be incorrect.

The first thing I would do is shuffle the dataset before calling the optimizer on each block.

My intent here in the first section is just to give the reader a quick idea of what's going on, not to go into detail on anything. Hence the liberal use of ...

However, it may be that I'm showing people anti-patterns. If so perhaps we can remove these pseudo-examples entirely and just refer people to scikit-learn docs. This intro seems a bit long regardless, there isn't any activity for the user above the fold.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The biggest edits I made were adding some motivation for calling dask.persist.

Sounds fine to me. As a general note, there are always going to be lots of things that we'll do in any of these examples that are important that we won't be able to tell them about. We'll necessarily skip a lot of potential wisdom we could deliver in order to focus on the topic at hand.

In this case though I agree that including some information about persist is good, especially because it's nice and concise.

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've pushed another example that removes the examples and tries to trim down the intro.

@mrocklin
mrocklin dismissed TomAugspurger’s stale review July 5, 2018 21:41

He is out of contact

@mrocklin
mrocklin merged commit 0a9a49c into dask:master Jul 5, 2018
@mrocklin

mrocklin commented Jul 5, 2018

Copy link
Copy Markdown
Member

This is in. Thanks @stsievert !

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