changes made as per ravi's suggestion and added fastexec magic
This commit is contained in:
@@ -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<float> array;
|
||||
* ...
|
||||
* index = max_element_index(array);
|
||||
* @endcode
|
||||
*/
|
||||
int max_element_index(ArrayList<float>& 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<double> array;
|
||||
* ...
|
||||
* index = max_element_index(array);
|
||||
* @endcode
|
||||
*/
|
||||
int max_element_index(ArrayList<double>& 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;
|
||||
}
|
||||
|
||||
|
||||
@@ -144,7 +144,7 @@ void MoGEM::ExpectationMaximization(Matrix& data_points, ArrayList<double> *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;
|
||||
|
||||
@@ -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<Vector>& mu() {
|
||||
|
||||
@@ -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<double> 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");
|
||||
|
||||
|
||||
Reference in New Issue
Block a user