From be56dfb5d007e4b5660fe0f7625ea8a563b680f7 Mon Sep 17 00:00:00 2001
From: Parikshit Ram
Date: Wed, 23 Jan 2008 07:39:06 +0000
Subject: [PATCH] changes made as per ravi's suggestion and added fastexec
magic
---
fastlib/u/pram/mog_em/math_functions.h | 144 +++++++++++++++++++++----
fastlib/u/pram/mog_em/mog.cc | 2 +-
fastlib/u/pram/mog_em/mog.h | 6 ++
fastlib/u/pram/mog_em/mog_em_main.cc | 22 ++--
4 files changed, 144 insertions(+), 30 deletions(-)
diff --git a/fastlib/u/pram/mog_em/math_functions.h b/fastlib/u/pram/mog_em/math_functions.h
index 107293cfbb..27c5576508 100644
--- a/fastlib/u/pram/mog_em/math_functions.h
+++ b/fastlib/u/pram/mog_em/math_functions.h
@@ -2,29 +2,137 @@
* @author pram
* @file math_functions.h
*
+ * This file has certain functions that find the
+ * highest or lowest element in an array or
+ * in a row of a matrix and returns them
+ *
*/
+
#include "fastlib/fastlib.h"
+#include "fastlib/fastlib_int.h"
-/* Finds the index of the minimum element in each row of a matrix */
-void min_element( Matrix& element, index_t *indices ){
+/**
+ * Finds the index of the minimum element
+ * in each row of a matrix
+ *
+ * Example use:
+ * @code
+ * Matrix& mat;
+ * index_t indices[mat.n_rows()];
+ *
+ * ...
+ *
+ * min_element(mat, indices);
+ * @endcode
+ */
+
+void min_element(Matrix& element, index_t *indices) {
- index_t last = element.n_cols() - 1;
- index_t first, lowest;
- index_t i;
+ index_t last = element.n_cols() - 1;
+ index_t first, lowest;
+ index_t i;
- for( i = 0; i < element.n_rows(); i++ ){
+ for (i = 0; i < element.n_rows(); i++) {
- first = lowest = 0;
- if(first == last){
- indices[ i ] = last;
- }
- while(++first <= last){
- if( element.get( i , first ) < element.get( i , lowest ) ){
- lowest = first;
- }
- }
- indices[ i ] = lowest;
- }
- return;
+ first = lowest = 0;
+ if (first == last) {
+ indices[i] = last;
+ }
+ while (++first <= last) {
+ if (element.get(i, first) < element.get(i, lowest)) {
+ lowest = first;
+ }
+ }
+ indices[i] = lowest;
+ }
+ return;
}
+
+/**
+ * Returns the index of the maximum element
+ * in a float array 'array' of length 'length'.
+ *
+ * Example use:
+ * @code
+ * index_t length, index;
+ * float array[length];
+ * ...
+ * index = max_element_index(array, length);
+ * @endcode
+ */
+int max_element_index(float *array, int length) {
+
+ int last = length - 1;
+ int first = 0;
+ int highest = 0;
+
+ if (first == last) {
+ return last;
+ }
+ while (++first <= last) {
+ if (array[first] > array[highest]) {
+ highest = first;
+ }
+ }
+ return highest;
+}
+
+/**
+ * Finds the index of the maximum element in an arraylist
+ * of floats
+ *
+ * Example use:
+ * @code
+ * index_t index;
+ * ArrayList array;
+ * ...
+ * index = max_element_index(array);
+ * @endcode
+ */
+int max_element_index(ArrayList& array){
+
+ int last = array.size() - 1;
+ int first = 0;
+ int highest = 0;
+
+ if (first == last) {
+ return last;
+ }
+ while (++first <= last) {
+ if (array[first] > array[highest]) {
+ highest = first;
+ }
+ }
+ return highest;
+}
+
+/**
+ * Returns the index of the maximum element in
+ * an arraylist of doubles
+ *
+ * Example use:
+ * @code
+ * index_t index;
+ * ArrayList array;
+ * ...
+ * index = max_element_index(array);
+ * @endcode
+ */
+int max_element_index(ArrayList& array) {
+
+ int last = array.size() - 1;
+ int first = 0;
+ int highest = 0;
+
+ if (first == last) {
+ return last;
+ }
+ while (++first <= last) {
+ if(array[first] > array[highest]) {
+ highest = first;
+ }
+ }
+ return highest;
+}
+
diff --git a/fastlib/u/pram/mog_em/mog.cc b/fastlib/u/pram/mog_em/mog.cc
index ca6a1720a6..7738e5dfc0 100644
--- a/fastlib/u/pram/mog_em/mog.cc
+++ b/fastlib/u/pram/mog_em/mog.cc
@@ -144,7 +144,7 @@ void MoGEM::ExpectationMaximization(Matrix& data_points, ArrayList *resu
}
set_omega(omega);
- printf("loglikelihood value of the model: %Lf\n", best_l);
+ NOTIFY("loglikelihood value of the estimated model: %Lf\n", best_l);
Display();
OutputResults(results);
return;
diff --git a/fastlib/u/pram/mog_em/mog.h b/fastlib/u/pram/mog_em/mog.h
index 8d47113433..0dc2184e0f 100644
--- a/fastlib/u/pram/mog_em/mog.h
+++ b/fastlib/u/pram/mog_em/mog.h
@@ -61,6 +61,12 @@ class MoGEM {
sigma_.Resize(number_of_gaussians_);
}
+ void Init(datanode *mog_em_module) {
+
+ index_t num_gauss = fx_param_int_req(mog_em_module, "K");
+ index_t dim = fx_param_int_req(mog_em_module, "D");
+ Init(num_gauss, dim);
+ }
// The get functions
ArrayList& mu() {
diff --git a/fastlib/u/pram/mog_em/mog_em_main.cc b/fastlib/u/pram/mog_em/mog_em_main.cc
index 212a5aedd8..b6ff02a227 100644
--- a/fastlib/u/pram/mog_em/mog_em_main.cc
+++ b/fastlib/u/pram/mog_em/mog_em_main.cc
@@ -11,11 +11,11 @@
* This is the file that contains the data on which
* the model is to be fit
*
- * --number_of_gaussians
+ * --mog_em/K
* This is the number of gaussians we want to fit
* on the data, defaults to '1'
*
- * --output_filename
+ * --output
* This file will contain the parameters estimated,
* defaults to 'ouotput.csv'
*
@@ -39,29 +39,29 @@ int main(int argc, char* argv[]) {
MoGEM mog;
- struct datanode* mog_em_module = fx_submodule(NULL, "mog_em", "mog_em_module");
- const int number_of_gaussians = fx_param_int(NULL, "number_of_gaussians", 1);
- const int dimensions = data_points.n_rows();
+ struct datanode* mog_em_module = fx_submodule(NULL, "mog_em", "mog_em");
+ fx_param_int(mog_em_module, "K", 1);
+ fx_param_int(mog_em_module, "D", data_points.n_rows());
////// Timing the initialization of the mixture model //////
- fx_timer_start(mog_em_module, "model_initializing");
+ fx_timer_start(mog_em_module, "model_init");
- mog.Init(number_of_gaussians, dimensions);
+ mog.Init(mog_em_module);
- fx_timer_stop(mog_em_module, "model_initializing");
+ fx_timer_stop(mog_em_module, "model_init");
////// Computing the parameters of the model using the EM algorithm //////
ArrayList results;
- fx_timer_start(mog_em_module, "optimization_via_EM");
+ fx_timer_start(mog_em_module, "EM");
mog.ExpectationMaximization(data_points, &results);
- fx_timer_stop(mog_em_module, "optimization_via_EM");
+ fx_timer_stop(mog_em_module, "EM");
////// OUTPUT RESULTS //////
- const char *output_filename = fx_param_str(NULL, "output_filename", "output.csv");
+ const char *output_filename = fx_param_str(NULL, "output", "output.csv");
FILE *output_file = fopen(output_filename, "w");