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

Feature/lazy get #4358

Merged
merged 1 commit into from
Jul 8, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 5 additions & 5 deletions examples/meta/src/clustering/kmeans.sg
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
File f_feats_train = csv_file("../../data/classifier_binary_2d_linear_features_train.dat")
Math:init_random(1)
File f_feats_train = csv_file("../../data/classifier_binary_2d_linear_features_train.dat")

#![create_features]
Features features_train = features(f_feats_train)
Expand All @@ -10,18 +10,18 @@ Distance d = distance("EuclideanDistance", lhs=features_train, rhs=features_trai
#![choose_distance]

#![create_instance_lloyd]
KMeans kmeans(2, d)
Machine kmeans = machine("KMeans", k=2, distance=d)
#![create_instance_lloyd]

#![train_dataset]
kmeans.train()
#![train_dataset]

#![extract_centers_and_radius]
RealMatrix c = kmeans.get_cluster_centers()
RealVector r = kmeans.get_radiuses()
RealMatrix c = kmeans.get_real_matrix("cluster_centers")
RealVector r = kmeans.get_real_vector("radiuses")
#![extract_centers_and_radius]

#![create_instance_mb]
KMeansMiniBatch kmeans_mb(k=2, distance=d, batch_size=4, max_iter=1000)
Machine kmeans_mini_batch = machine("KMeansMiniBatch", k=2, distance=d, batch_size=4, max_iter=100)
#![create_instance_mb]
5 changes: 5 additions & 0 deletions src/shogun/base/SGObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -716,6 +716,11 @@ class CSGObject
}

#ifndef SWIG
/** Puts a pointer to a (lazily evaluated) function into the parameter map.
*
* @param name name of the parameter
* @param method pointer to the method
*/
template <typename T, typename S>
void watch_method(const std::string& name, T (S::*method)() const)
{
Expand Down
6 changes: 4 additions & 2 deletions src/shogun/clustering/KMeansBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ SGVector<float64_t> CKMeansBase::get_radiuses()
return R;
}

SGMatrix<float64_t> CKMeansBase::get_cluster_centers()
SGMatrix<float64_t> CKMeansBase::get_cluster_centers() const
{
if (!R.vector)
return SGMatrix<float64_t>();
Expand Down Expand Up @@ -362,6 +362,8 @@ void CKMeansBase::init()
SG_ADD(&max_iter, "max_iter", "Maximum number of iterations", MS_AVAILABLE);
SG_ADD(&k, "k", "k, the number of clusters", MS_AVAILABLE);
SG_ADD(&dimensions, "dimensions", "Dimensions of data", MS_NOT_AVAILABLE);
SG_ADD(&R, "R", "Cluster radiuses", MS_NOT_AVAILABLE);
SG_ADD(&R, "radiuses", "Cluster radiuses", MS_NOT_AVAILABLE);

watch_method("cluster_centers", &CKMeansBase::get_cluster_centers);
}

2 changes: 1 addition & 1 deletion src/shogun/clustering/KMeansBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ class CKMeansBase : public CDistanceMachine
*
* @return cluster centers or empty matrix if no radiuses are there (not trained yet)
*/
SGMatrix<float64_t> get_cluster_centers();
SGMatrix<float64_t> get_cluster_centers() const;

/** get dimensions
*
Expand Down