-
Notifications
You must be signed in to change notification settings - Fork 18
Feature/skmeans #99
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
Merged
Merged
Feature/skmeans #99
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
5d54eb0
add pca whitening
g-roma 1251b99
actually add pca whitening
g-roma 17f8b08
add spherical kmeans
g-roma f55870c
actually add spherical kmeans
g-roma 3fd2b93
merge dev into feature/skmeans
g-roma ee73722
SKMeans fixes, change KMeans getDistances to transform
g-roma db6753e
Merge branch 'dev' into feature/skmeans
tremblap 56496d9
adding RT query class
tremblap 3db0531
Merge branch 'dev' into feature/skmeans
tremblap 2875cdf
Merge branch 'dev' into feature/skmeans
tremblap 969638b
<fit>transform<point> -> <fit>encode<point>
tremblap File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,121 @@ | ||
| /* | ||
| Part of the Fluid Corpus Manipulation Project (http://www.flucoma.org/) | ||
| Copyright 2017-2019 University of Huddersfield. | ||
| Licensed under the BSD-3 License. | ||
| See license.md file in the project root for full license information. | ||
| This project has received funding from the European Research Council (ERC) | ||
| under the European Union’s Horizon 2020 research and innovation programme | ||
| (grant agreement No 725899). | ||
| */ | ||
|
|
||
| #pragma once | ||
|
|
||
| #include "../public/KMeans.hpp" | ||
| #include "../util/FluidEigenMappings.hpp" | ||
| #include "../../data/FluidDataSet.hpp" | ||
| #include "../../data/FluidIndex.hpp" | ||
| #include "../../data/FluidTensor.hpp" | ||
| #include "../../data/TensorTypes.hpp" | ||
| #include <Eigen/Core> | ||
| #include <queue> | ||
| #include <string> | ||
|
|
||
| namespace fluid { | ||
| namespace algorithm { | ||
|
|
||
| class SKMeans : public KMeans | ||
| { | ||
|
|
||
| public: | ||
| void train(const FluidDataSet<std::string, double, 1>& dataset, index k, | ||
| index maxIter) | ||
| { | ||
| using namespace Eigen; | ||
| using namespace _impl; | ||
| assert(!mTrained || (dataset.pointSize() == mDims && mK == k)); | ||
| MatrixXd dataPoints = asEigen<Matrix>(dataset.getData()); | ||
| MatrixXd dataPointsT = dataPoints.transpose(); | ||
| if (mTrained) { mAssignments = assignClusters(dataPointsT);} | ||
| else | ||
| { | ||
| mK = k; | ||
| mDims = dataset.pointSize(); | ||
| initMeans(dataPoints); | ||
| } | ||
|
|
||
| while (maxIter-- > 0) | ||
| { | ||
| mEmbedding = mMeans.matrix() * dataPointsT; | ||
| auto assignments = assignClusters(mEmbedding); | ||
| if (!changed(assignments)) { break; } | ||
| else | ||
| mAssignments = assignments; | ||
| updateEmbedding(); | ||
| computeMeans(dataPoints); | ||
| } | ||
| mTrained = true; | ||
| } | ||
|
|
||
|
|
||
| void encode(RealMatrixView data, RealMatrixView out, | ||
| double alpha = 0.25) const | ||
| { | ||
| using namespace Eigen; | ||
| MatrixXd points = _impl::asEigen<Matrix>(data).transpose(); | ||
| MatrixXd embedding = (mMeans.matrix() * points).array() - alpha; | ||
| embedding = (embedding.array() > 0).select(embedding, 0).transpose(); | ||
| out <<= _impl::asFluid(embedding); | ||
| } | ||
|
|
||
| private: | ||
|
|
||
| void initMeans(Eigen::MatrixXd& dataPoints) | ||
| { | ||
| using namespace Eigen; | ||
| mMeans = ArrayXXd::Zero(mK, mDims); | ||
| mAssignments = | ||
| ((0.5 + (0.5 * ArrayXd::Random(dataPoints.rows()))) * (mK - 1)) | ||
| .round() | ||
| .cast<int>(); | ||
| mEmbedding = MatrixXd::Zero(mK, dataPoints.rows()); | ||
| for (index i = 0; i < dataPoints.rows(); i++) | ||
| mEmbedding(mAssignments(i), i) = 1; | ||
| computeMeans(dataPoints); | ||
| } | ||
|
|
||
| void updateEmbedding() | ||
| { | ||
| for (index i = 0; i < mAssignments.cols(); i++) | ||
| { | ||
| double val = mEmbedding(mAssignments(i), i); | ||
| mEmbedding.col(i).setZero(); | ||
| mEmbedding(mAssignments(i), i) = val; | ||
| } | ||
| } | ||
|
|
||
|
|
||
| Eigen::VectorXi assignClusters(Eigen::MatrixXd& embedding) const | ||
| { | ||
| Eigen::VectorXi assignments = Eigen::VectorXi::Zero(embedding.cols()); | ||
| for (index i = 0; i < embedding.cols(); i++) | ||
| { | ||
| Eigen::VectorXd::Index maxIndex; | ||
| embedding.col(i).maxCoeff(&maxIndex); | ||
| assignments(i) = static_cast<int>(maxIndex); | ||
| } | ||
| return assignments; | ||
| } | ||
|
|
||
|
|
||
| void computeMeans(Eigen::MatrixXd& dataPoints) | ||
| { | ||
| mMeans = mEmbedding * dataPoints; | ||
| mMeans.matrix().rowwise().normalize(); | ||
| } | ||
|
|
||
|
|
||
| private: | ||
| Eigen::MatrixXd mEmbedding; | ||
| }; | ||
| } // namespace algorithm | ||
| } // namespace fluid | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
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.
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.
Are we sure about baking in the encoding scheme from Coates and Ng here?
I guess the argument in favour is that it enables recreating their feature learning scheme with the fewest objects. The arguments against would be that it's not strictly part of skmeans, but was a separate step used by C&N specifically in the feature learning setting, and they do discuss alternatives.
Obviously having it here doesn't preclude using an alternative scheme, because
alphacan be set to 0. However, the other question is whether this encoder could be useful for other things if it were factored out, e.g. using NMF for feature learning?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.
The soft thresholding function is similar to neural network activation functions, so NNFuncs is where it would fit best, but I don't think these functions would deserve their own client, so in practice I would still see this as part of the SKMeans client. So it would not help with code duplication. An interesting idea (maybe for the future?) could be to have a feature learning client that could use different learning techniques and encodings. For the moment, maybe it can be introduced as an option for skmeans. We can also use an MLP as autoencoder for feature learning.
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.
Ok, let's roll with what we have and see how we get on. I like the idea of some future feature learning object that could make it easy to explore options and manage some of the complexity / fiddliness.