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

Vectorized the _activate function #41

Merged
merged 1 commit into from Aug 16, 2019
Merged

Vectorized the _activate function #41

merged 1 commit into from Aug 16, 2019

Conversation

AustinT
Copy link

@AustinT AustinT commented Aug 15, 2019

Great library, but I noticed that the training code for your SOMs is not vectorized. You use the fast_norm function a lot, which may be faster than linalg.norm for 1D arrays, but iterating over every spot in the SOM is a lot slower than just calling linalg.norm.

This pull request replaces fast_norm with linalg.norm in 2 places where I saw iteration over the whole SOM. Some simple testing with a 100x100 SOM showed ~40x speedup on my laptop.

After making the changes, the unit tests failed, which I believe is caused by incorrectly setting up the testing weights as a 2D array rather than a 3D array. So I changed that too, and now the unit tests pass. I also did a few rough tests of my own, and the results of self.winner(x) and the training seem to be the same as before.

…fixed a bug in the unit tests where the weights were initialized with the wrong shape
@JustGlowing
Copy link
Owner

JustGlowing commented Aug 15, 2019 via email

@AustinT
Copy link
Author

AustinT commented Aug 15, 2019

Hi Austin, Thanks for your contribution! Please report the training time on the Iris and handwritten digits example with and without your change.

On Thu, Aug 15, 2019, 5:37 AM Austin Tripp @.***> wrote: Great library, but I noticed that the training code for your SOMs is not vectorized. You use the fast_norm function a lot, which may be faster than linalg.norm for 1D arrays, but iterating over every spot in the SOM is a lot slower than just calling linalg.norm. This pull request replaces fast_norm with linalg.norm in 2 places where I saw iteration over the whole SOM. Some simple testing with a 100x100 SOM showed ~40x speedup on my laptop. After making the changes, the unit tests failed, which I believe is caused by incorrectly setting up the testing weights as a 2D array rather than a 3D array. So I changed that too. ------------------------------ You can view, comment on, or merge this pull request online at: #41 Commit Summary - Vectorized the _activate function and weight normalization code, and fixed a bug in the unit tests where the weights were initialized with the wrong shape File Changes - M minisom.py https://github.com/JustGlowing/minisom/pull/41/files#diff-0 (15) Patch Links: - https://github.com/JustGlowing/minisom/pull/41.patch - https://github.com/JustGlowing/minisom/pull/41.diff — You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub <#41?email_source=notifications&email_token=ABFTNGPPTFAT3WVMF4DR7NDQETMPLA5CNFSM4IL3AMM2YY3PNVWWK3TUL52HS4DFUVEXG43VMWVGG33NNVSW45C7NFSM4HFLKWFA>, or mute the thread https://github.com/notifications/unsubscribe-auth/ABFTNGOVBZOIBZWG4R4W5PTQETMPLANCNFSM4IL3AMMQ .

Hi @JustGlowing ,

Measuring the training time using the number of iterations per second:
My commit: digits=957, iris=3847
Prev commit: digits=301, iris=1730

Using the %%timeit magic, I get:
My commit: digits=6.12s, iris=1.92s (slower)
Prev commit: digits=21.2s, iris=1.8s

@JustGlowing
Copy link
Owner

I tried to run the unit tests and I got this:

>       self._weights /= linalg.norm(self._weights, axis=-1, keepdims=True)
E       TypeError: norm() got an unexpected keyword argument 'keepdims'

I have the latest version of numpy.

@AustinT
Copy link
Author

AustinT commented Aug 15, 2019

I tried to run the unit tests and I got this:

>       self._weights /= linalg.norm(self._weights, axis=-1, keepdims=True)
E       TypeError: norm() got an unexpected keyword argument 'keepdims'

I have the latest version of numpy.

Are you sure you have the latest version of numpy? Looking at the docs, the keyword keepdims was introduced in version 1.10.0...

@JustGlowing
Copy link
Owner

I managed to let the tests pass, thank for your amazing contribution!

@JustGlowing JustGlowing merged commit c0f602b into JustGlowing:master Aug 16, 2019
@AustinT
Copy link
Author

AustinT commented Aug 16, 2019

I managed to let the tests pass, thank for your amazing contribution!

Happy to contribute to a great library :)

@JustGlowing
Copy link
Owner

JustGlowing commented Aug 16, 2019 via email

@JustGlowing
Copy link
Owner

hey @AustinT

I tried to recognize you publicly but I'm not sure you have a twitter account: https://twitter.com/JustGlowing/status/1169607904896458758

You have also been mentioned in the release note.

@AustinT
Copy link
Author

AustinT commented Sep 5, 2019 via email

Manciukic pushed a commit to Manciukic/xpysom that referenced this pull request Jun 28, 2020
Vectorized the _activate function
@JustGlowing
Copy link
Owner

hi there, I'm thinking about changing the license of Minisom to MIT (or GPL). This will allow Minisom to have a paper on the Journal of Open Source software. This WILL NOT affect the ownership of your contribution and does not imply that I will profit from Minisom. Your contribution was very welcome and I'd like to have your approval.

@AustinT
Copy link
Author

AustinT commented Jul 23, 2020

hi there, I'm thinking about changing the license of Minisom to MIT (or GPL). This will allow Minisom to have a paper on the Journal of Open Source software. This WILL NOT affect the ownership of your contribution and does not imply that I will profit from Minisom. Your contribution was very welcome and I'd like to have your approval.

Hi, I'm more than happy for you to change the license: thanks for asking!

JustGlowing added a commit that referenced this pull request Apr 26, 2021
Vectorized the _activate function
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.

None yet

2 participants