Skip to content

Instantly share code, notes, and snippets.

Created June 21, 2013 10:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/5830320 to your computer and use it in GitHub Desktop.
Save anonymous/5830320 to your computer and use it in GitHub Desktop.
#include <vector>
#include <iostream>
#include <shogun/base/init.h>
#include <shogun/features/DenseFeatures.h>
#include <shogun/clustering/GMM.h>
using namespace shogun;
struct gauss {
SGVector< double > mean;
SGMatrix< double > covariance;
double coefficient;
};
std::vector<double> getLocations(size_t num_locations, size_t dim) {
std::vector<double> locations;
for (int i=0; i < (num_locations * dim); ++i) {
locations.push_back(rand() % 50);
}
return locations;
}
void print_message(FILE* target, const char* str) {
fprintf(target, "%s", str);
}
int main(int argc, char **argv) {
init_shogun(&print_message, &print_message, &print_message);
int32_t num_components = 7;
int32_t dim_features = 2;
int32_t num_locations = rand() % 100;
std::vector<double> locations = getLocations(num_locations, dim_features);
std::cout << "Locations: " << std::endl;
for (std::vector<double>::iterator it = locations.begin(); it != locations.end(); ++it) {
std::cout << *it << std::endl;
}
SGMatrix<double> data(dim_features, num_locations);
for (std::vector<double>::iterator it = locations.begin(); it != locations.end(); ++it) {
data.matrix[std::distance(locations.begin(), it)] = *it;
}
CDenseFeatures<double>* features = new CDenseFeatures<double>();
features->set_feature_matrix(data);
CGMM* gm = new CGMM(num_components);
SG_REF(gm);
gm->train(features);
gm->train_em();
std::vector<gauss> results;
for (size_t i=1; i < num_components; ++i) {
gauss g;
g.covariance = gm->get_nth_cov(i);
g.mean = gm->get_nth_mean(i);
g.coefficient = gm->get_coef()[i];
results.push_back(g);
}
for (std::vector<gauss>::iterator it = results.begin(); it != results.end(); ++it) {
SGVector<double>::display_vector(it->mean, it->mean.vlen, "Mean");
SGMatrix<double>::display_matrix(it->covariance, "Covariance");
std::cout << "Coefficient: " << it->coefficient << std::endl;
}
SG_UNREF(gm);
SG_UNREF(features);
exit_shogun();
return 0;
}
==24022==
==24022== HEAP SUMMARY:
==24022== in use at exit: 60,895 bytes in 287 blocks
==24022== total heap usage: 188,942 allocs, 188,655 frees, 3,514,988 bytes allocated
==24022==
==24022== 192 bytes in 6 blocks are definitely lost in loss record 231 of 282
==24022== at 0x4C244E8: malloc (vg_replace_malloc.c:236)
==24022== by 0x564CCA8: shogun::sg_malloc(unsigned long) (memory.cpp:153)
==24022== by 0x514BDF6: shogun::CGaussian::get_cov() (memory.h:79)
==24022== by 0x5492C60: shogun::CGMM::get_nth_cov(int) (GMM.cpp:678)
==24022== by 0x401B27: main (in /home/stepan/shoguntest.out)
==24022==
==24022== 60,623 (176 direct, 60,447 indirect) bytes in 1 blocks are definitely lost in loss record 282 of 282
==24022== at 0x4C244E8: malloc (vg_replace_malloc.c:236)
==24022== by 0x565BEFD: operator new(unsigned long) (memory.cpp:84)
==24022== by 0x549307B: shogun::CGMM::alpha_init(shogun::SGMatrix<double>) (GMM.cpp:727)
==24022== by 0x5495BFF: shogun::CGMM::train_em(double, int, double) (GMM.cpp:144)
==24022== by 0x401AD3: main (in /home/stepan/shoguntest.out)
==24022==
==24022== LEAK SUMMARY:
==24022== definitely lost: 368 bytes in 7 blocks
==24022== indirectly lost: 60,447 bytes in 277 blocks
==24022== possibly lost: 0 bytes in 0 blocks
==24022== still reachable: 80 bytes in 3 blocks
==24022== suppressed: 0 bytes in 0 blocks
==24022== Reachable blocks (those to which a pointer was found) are not shown.
==24022== To see them, rerun with: --leak-check=full --show-reachable=yes
==24022==
==24022== For counts of detected and suppressed errors, rerun with: -v
==24022== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 4 from 4)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment