Skip to content

Multiprocessing fix for dictionary learning main loop #4773

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

Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion sklearn/decomposition/dict_learning.py
Original file line number Diff line number Diff line change
Expand Up @@ -669,8 +669,9 @@ def dict_learning_online(X, n_components=2, alpha=1, n_iter=100,
print ("Iteration % 3i (elapsed time: % 3is, % 4.1fmn)"
% (ii, dt, dt / 60))

# Setting n_jobs = 1 to avoid creating workers at each iteration
this_code = sparse_encode(this_X, dictionary.T, algorithm=method,
alpha=alpha, n_jobs=n_jobs).T
alpha=alpha, n_jobs=1).T
Copy link
Member

Choose a reason for hiding this comment

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

are you disabling joblib then?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Using sparse_encode with n_jobs=1 actually disable joblib.Parallel. I think this is the only way if we do not want to manage workers manually

Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

There is a gain in the transform / sparse_encode function,once the dictionary has been computed. However, during the alternative process of online dictionary learning (sparse_encode -> _update_dict), using n_jobs > 1 actually slows down the process (see #4769). This is a joblib inherent issue, as new workers are set up at each iteration

However this should probably depends on the batch dimension : if it is large, multiprocessing should still improve performance


# Update the auxiliary variables
if ii < batch_size - 1:
Expand Down