Skip to content

Commit

Permalink
Fixes for Alesis code
Browse files Browse the repository at this point in the history
  • Loading branch information
lisitsyn committed Jun 20, 2011
1 parent 136be95 commit 176a1b2
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 60 deletions.
18 changes: 10 additions & 8 deletions src/libshogun/clustering/GMM.cpp
Expand Up @@ -69,10 +69,12 @@ bool CGMM::train(CFeatures* data)
CEuclidianDistance* dist = new CEuclidianDistance();
CKMeans* init_k_means = new CKMeans(m_n, dist);
init_k_means->train(dotdata);
float64_t* init_means;
int32_t init_mean_dim;
int32_t init_mean_size;
init_k_means->get_cluster_centers(&init_means, &init_mean_dim, &init_mean_size);
// sorry ;)
SGMatrix<float64_t> cluster_centers = init_k_means->get_cluster_centers();
float64_t* init_means = cluster_centers.matrix;
int32_t init_mean_dim = cluster_centers.num_rows;
int32_t init_mean_size = cluster_centers.num_cols;


float64_t* init_cov;
int32_t init_cov_rows;
Expand All @@ -85,8 +87,8 @@ bool CGMM::train(CFeatures* data)
for (int i=0; i<m_n; i++)
{
m_coefficients[i] = 1.0/m_coef_size;
m_components[i] = new CGaussian(&(init_means[i*init_mean_dim]), init_mean_dim,
init_cov, init_cov_rows, init_cov_cols);
m_components[i] = new CGaussian(SGVector<float64_t>(&(init_means[i*init_mean_dim]), init_mean_dim),
SGMatrix<float64_t>(init_cov, init_cov_rows, init_cov_cols));
}

/** question of faster vs. less memory using */
Expand Down Expand Up @@ -155,7 +157,7 @@ bool CGMM::train(CFeatures* data)
for (int j=0; j<num_dim; j++)
mean_sum[j] /= T_sum;

m_components[i]->set_mean(mean_sum, num_dim);
m_components[i]->set_mean(SGVector<float64_t>(mean_sum, num_dim));

cov_sum = new float64_t[num_dim*num_dim];
memset(cov_sum, 0, num_dim*num_dim*sizeof(float64_t));
Expand All @@ -172,7 +174,7 @@ bool CGMM::train(CFeatures* data)
for (int j=0; j<num_dim*num_dim; j++)
cov_sum[j] /= T_sum;

m_components[i]->set_cov(cov_sum, num_dim, num_dim);
m_components[i]->set_cov(SGMatrix<float64_t>(cov_sum, num_dim, num_dim));

delete[] mean_sum;
delete[] cov_sum;
Expand Down
21 changes: 6 additions & 15 deletions src/libshogun/clustering/GMM.h
Expand Up @@ -94,39 +94,30 @@ class CGMM : public CDistribution

/** get nth mean
*
* @param mean copy of the mean
* @param mean_length
* @param num which mean to retrieve
*/
virtual inline void get_nth_mean(float64_t** mean, int32_t* mean_length, int32_t num)
virtual SGVector<float64_t> get_nth_mean(int32_t num)
{
ASSERT(num<m_n);
return m_components[num]->get_mean(mean, mean_length);
return m_components[num]->get_mean();
}

/** get nth cov
*
* @param cov copy of the cov
* @param cov_rows
* @param cov_cols
* @param num which covariance to retrieve
*/
virtual inline void get_nth_cov(float64_t** cov, int32_t* cov_rows, int32_t* cov_cols, int32_t num)
virtual SGMatrix<float64_t> get_nth_cov(int32_t num)
{
ASSERT(num<m_n);
return m_components[num]->get_cov(cov, cov_rows, cov_cols);
return m_components[num]->get_cov();
}

/** get coefficients
*
* @param coef copy of coeffiecients
* @param coef_length coef vector length
*/
virtual inline void get_coef(float64_t** coef, int32_t* coef_length)
virtual SGVector<float64_t> get_coef()
{
*coef = new float64_t[m_coef_size];
memcpy(*coef, m_coefficients, sizeof(float64_t)*m_coef_size);
*coef_length = m_coef_size;
return SGVector<float64_t>(m_coefficients,m_coef_size);
}

/** @return object name */
Expand Down
19 changes: 8 additions & 11 deletions src/libshogun/distributions/Gaussian.cpp
Expand Up @@ -24,19 +24,16 @@ m_mean_length(0)
{
}

CGaussian::CGaussian(float64_t* mean, int32_t mean_length,
float64_t* cov, int32_t cov_rows, int32_t cov_cols) : CDistribution(),
CGaussian::CGaussian(SGVector<float64_t> mean_vector, SGMatrix<float64_t> cov_matrix) : CDistribution(),
m_cov_inverse(NULL)
{
ASSERT(mean_length == cov_rows);
ASSERT(cov_rows == cov_cols);
m_mean = new float64_t[mean_length];
memcpy(m_mean, mean, sizeof(float64_t)*mean_length);
m_cov = new float64_t[cov_rows*cov_cols];
memcpy(m_cov, cov, sizeof(float64_t)*cov_rows*cov_cols);
m_mean_length = mean_length;
m_cov_rows = cov_rows;
m_cov_cols = cov_cols;
ASSERT(mean_vector.vlen == cov_matrix.num_rows);
ASSERT(cov_matrix.num_rows == cov_matrix.num_cols);
m_mean = mean_vector.vector;
m_cov = cov_matrix.matrix;
m_mean_length = mean_vector.vlen;
m_cov_rows = cov_matrix.num_rows;
m_cov_cols = cov_matrix.num_cols;
init();
register_params();
}
Expand Down
41 changes: 15 additions & 26 deletions src/libshogun/distributions/Gaussian.h
Expand Up @@ -33,8 +33,10 @@ class CDotFeatures;
class CGaussian : public CDistribution
{
public:

/** default constructor */
CGaussian();

/** constructor
*
* @param mean mean of the Gaussian
Expand All @@ -43,8 +45,8 @@ class CGaussian : public CDistribution
* @param cov_rows
* @param cov_cols
*/
CGaussian(float64_t* mean, int32_t mean_length,
float64_t* cov, int32_t cov_rows, int32_t cov_cols);
CGaussian(SGVector<float64_t> mean_vector, SGMatrix<float64_t> cov_matrix);

virtual ~CGaussian();

/** Compute the inverse covariance and constant part */
Expand Down Expand Up @@ -110,52 +112,39 @@ class CGaussian : public CDistribution

/** get mean
*
* @param mean copy of the mean
* @param mean_length
*/
virtual inline void get_mean(float64_t** mean, int32_t* mean_length)
virtual SGVector<float64_t> get_mean()
{
*mean = new float64_t[m_mean_length];
memcpy(*mean, m_mean, sizeof(float64_t)*m_mean_length);
*mean_length = m_mean_length;
return SGVector<float64_t>(m_mean,m_mean_length);
}

/** set mean
*
* @param mean new mean
* @param mean_length has to match current mean length
*/
virtual inline void set_mean(float64_t* mean, int32_t mean_length)
virtual void set_mean(SGVector<float64_t> mean_vector)
{
ASSERT(mean_length == m_mean_length);
memcpy(m_mean, mean, sizeof(float64_t)*m_mean_length);
ASSERT(mean_vector.vlen == m_mean_length);
memcpy(m_mean, mean_vector.vector, sizeof(float64_t)*m_mean_length);
}

/** get cov
*
* @param cov copy of the cov
* @param cov_rows
* @param cov_cols
*/
virtual inline void get_cov(float64_t** cov, int32_t* cov_rows, int32_t* cov_cols)
virtual SGMatrix<float64_t> get_cov()
{
*cov = new float64_t[m_cov_rows*m_cov_cols];
memcpy(*cov, m_cov, sizeof(float64_t)*m_cov_rows*m_cov_cols);
*cov_rows = m_cov_rows;
*cov_cols = m_cov_cols;
return SGMatrix<float64_t>(m_cov,m_cov_rows,m_cov_cols);
}

/** set cov
*
* @param cov new cov
* @param cov_rows has to match current cov rows
* @param cov_cols has to be equal to cov_rows
*/
virtual inline void set_cov(float64_t* cov, int32_t cov_rows, int32_t cov_cols)
virtual inline void set_cov(SGMatrix<float64_t> cov_matrix)
{
ASSERT(cov_rows = cov_cols);
ASSERT(cov_rows = m_cov_rows);
memcpy(m_cov, cov, sizeof(float64_t)*m_cov_rows*m_cov_cols);
ASSERT(cov_matrix.num_rows = cov_matrix.num_cols);
ASSERT(cov_matrix.num_rows = m_cov_rows);
memcpy(m_cov, cov_matrix.matrix, sizeof(float64_t)*m_cov_rows*m_cov_cols);
init();
}

Expand Down

0 comments on commit 176a1b2

Please sign in to comment.