Use k-d tree to optimize performance in visual function #19
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.
The
visualfunction, which finds the winner neuron for an arbitrary "specimen" input, was using a brute force search to find the nearest neighbor among the neuron codes.A k-d tree search is usually faster than a brute force search, especially when doing a large number of queries over a large number of neurons.
The disadvantage of a k-d tree is that the initial construction of the tree is costly, but once constructed, search queries are fast. That's why we can't use the k-d tree in the training algorithm - we would have to reconstruct after every step.
But for functions on an already-trained SOM, like
mapToSOM, a k-d tree can be advantageous, in most practical use cases.Example of a benchmark I have done
I have included an optimized version of the
visualfunction. The old function is renamedvisualGenericbecause it uses multiple dispatch infindWinnerto decide which search algorithm to choose.A test is also included, to test whether
visualandvisualGenericreturn the same result.PS. Caching the k-d tree in the SOM struct might also be a good idea.