Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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
making kmeans take AbstractMatrix instead of Matrix #138
making kmeans take AbstractMatrix instead of Matrix #138
Changes from 1 commit
57c172a
4c7f218
2e95afb
be3575d
d3f167a
a8ea8c7
c5bda5b
3f8b15b
0d272e9
9cb02f7
16ae803
192694a
603422d
File filter
Filter by extension
Conversations
Jump to
There are no files selected for viewing
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess this
*
(plus the one below) was the reason for regression in weighted case as it creates the new vector..+= xj .* wj
should avoid that.I'm a little bit hesitant about converting everything to explicit loops as it defeats the purpose of having broadcast support in the language.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm happy with opening another PR for this kind of stuff (with tests & benchmarks) after this one gets merged so that we can isolate changes related to the initial problem from other changes?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1. Then I'd rather not introduce explicit loop conversions in this PR to keep it as small as possible.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(FWIW, I just tried exactly what you said and it increases the allocation by a lot and doubles the time. There may be better things to do with
copyto!
etc but I must admit I'd rather get this PR done with first...)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought
centers[:, cj] .+=
callssetindex!()
directly, but it actually createscenters[:, cj]
vector behind the scenes (centers[:, cj] .=
doesn't). So the better version iscej = view(centers, :, cj); cej .+= xj .* wj
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not 100% sure, but back in the days
/
was more expensive than*
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
with /= -->
132.276ms; 76.272ms
with *= 1 / ... -->
132.628ms; 77.268ms
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note that the current code computes
1/cweights[j]
only once, and then does the multiplication for eachi
. IIUC the timings for*= 1 /
recompute the division it for eachi
, which isn't faster.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, I think LLVM is smart enough to calculate
1/wj
only once (at least that's what I see in@code_llvm
for a toy example). But I still prefer the broadcast version (cej = view(centers, :, j); cej .*= 1/cweights[j]
), where this would be definitely done once. :)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree, the broadcasted version was nicer. Better use
view
. Same above, whereview
was used but you removed it: usexj .* wj
and everything should be OK.