Skip to content

Commit

Permalink
fix parameters hashing framework
Browse files Browse the repository at this point in the history
  • Loading branch information
votjakovr committed Mar 4, 2014
1 parent db13b04 commit 0a9bba3
Show file tree
Hide file tree
Showing 14 changed files with 253 additions and 78 deletions.
Expand Up @@ -63,6 +63,7 @@ int main(int argc, char** argv)
CDenseFeatures<float64_t>* feat_train=new CDenseFeatures<float64_t>(X_train);
CBinaryLabels* lab_train=new CBinaryLabels(y_train);
CDenseFeatures<float64_t>* feat_test=new CDenseFeatures<float64_t>(X_test);
SG_REF(feat_test);

// create Gaussian kernel with width = 2.0
CGaussianKernel* kernel=new CGaussianKernel(10, 2.0);
Expand Down Expand Up @@ -114,6 +115,7 @@ int main(int argc, char** argv)
// free up memory
SG_UNREF(gpc);
SG_UNREF(predictions);
SG_UNREF(feat_test);

exit_shogun();
return 0;
Expand Down
Expand Up @@ -19,7 +19,7 @@ int main(int argc, char** argv)
// shogun will now own the matrix created
CDenseFeatures<float64_t>* features= new CDenseFeatures<float64_t>(matrix);

ASSERT(features->update_parameter_hash());
ASSERT(features->parameter_hash_changed());

SG_UNREF(features);
exit_shogun();
Expand Down
86 changes: 52 additions & 34 deletions src/shogun/base/SGObject.cpp
Expand Up @@ -184,26 +184,32 @@ void CSGObject::set_global_parallel(Parallel* new_parallel)
sg_parallel=new_parallel;
}

bool CSGObject::update_parameter_hash()
void CSGObject::update_parameter_hash()
{
uint32_t new_hash = 0;
uint32_t carry = 0;
uint32_t length = 0;
SG_DEBUG("entering\n")

get_parameter_incremental_hash(m_parameters, new_hash,
carry, length);
uint32_t carry=0;
uint32_t length=0;

new_hash = CHash::FinalizeIncrementalMurmurHash3(new_hash,
carry, length);
get_parameter_incremental_hash(m_hash, carry, length);
m_hash=CHash::FinalizeIncrementalMurmurHash3(m_hash, carry, length);

if(new_hash != m_hash)
{
m_hash = new_hash;
return true;
}
SG_DEBUG("leaving\n")
}

else
return false;
bool CSGObject::parameter_hash_changed()
{
SG_DEBUG("entering\n")

uint32_t hash=0;
uint32_t carry=0;
uint32_t length=0;

get_parameter_incremental_hash(hash, carry, length);
hash=CHash::FinalizeIncrementalMurmurHash3(hash, carry, length);

SG_DEBUG("leaving\n")
return (m_hash!=hash);
}

Parallel* CSGObject::get_global_parallel()
Expand Down Expand Up @@ -1124,32 +1130,44 @@ bool CSGObject::is_param_new(const SGParamInfo param_info) const
return result;
}

void CSGObject::get_parameter_incremental_hash(Parameter* param,
uint32_t& hash, uint32_t& carry, uint32_t& total_length)
void CSGObject::get_parameter_incremental_hash(uint32_t& hash, uint32_t& carry,
uint32_t& total_length)
{
if (!param)
return;

for (index_t i=0; i<param->get_num_parameters(); i++)
for (index_t i=0; i<m_parameters->get_num_parameters(); i++)
{
TParameter* p = param->get_parameter(i);
SG_DEBUG("Updating hash for parameter \"%s\"\n", p->m_name ? p->m_name : "(nil)");
TParameter* p=m_parameters->get_parameter(i);

if (!p || !p->is_valid())
continue;
SG_DEBUG("Updating hash for parameter %s.%s\n", get_name(), p->m_name);

if (p->m_datatype.m_ptype != PT_SGOBJECT)
if (p->m_datatype.m_ptype == PT_SGOBJECT)
{
p->get_incremental_hash(hash, carry, total_length);
continue;
}
if (p->m_datatype.m_ctype == CT_SCALAR)
{
CSGObject* child = *((CSGObject**)(p->m_parameter));

CSGObject* child = *((CSGObject**)(p->m_parameter));
if (child)
{
child->get_parameter_incremental_hash(hash, carry,
total_length);
}
}
else if (p->m_datatype.m_ctype==CT_VECTOR ||
p->m_datatype.m_ctype==CT_SGVECTOR)
{
CSGObject** child=(*(CSGObject***)(p->m_parameter));

if (child)
get_parameter_incremental_hash(
child->m_parameters, hash,
carry, total_length);
for (index_t j=0; j<*(p->m_datatype.m_length_y); j++)
{
if (child[j])
{
child[j]->get_parameter_incremental_hash(hash, carry,
total_length);
}
}
}
}
else
p->get_incremental_hash(hash, carry, total_length);
}
}

Expand Down
25 changes: 12 additions & 13 deletions src/shogun/base/SGObject.h
Expand Up @@ -396,12 +396,13 @@ class CSGObject : public SGRefObject
virtual void save_serializable_post() throw (ShogunException);

public:
/** Updates the hash of current parameter combination.
*
* @return bool if parameter combination has changed since last
* update.
/** Updates the hash of current parameter combination */
virtual void update_parameter_hash();

/**
* @return whether parameter combination has changed since last update
*/
virtual bool update_parameter_hash();
virtual bool parameter_hash_changed();

/** Recursively compares the current SGObject to another one. Compares all
* registered numerical parameters, recursion upon complex (SGObject)
Expand Down Expand Up @@ -455,18 +456,16 @@ class CSGObject : public SGRefObject
int32_t load_parameter_version(CSerializableFile* file,
const char* prefix="");

/*Gets an incremental hash of all parameters as well as the parameters
* of CSGObject children of the current object's parameters.
/** Gets an incremental hash of all parameters as well as the parameters of
* CSGObject children of the current object's parameters.
*
* @param param Parameter to hash
* @param current hash
* @param carry value for Murmur3 incremental hash
* @param total_length total byte length of all hashed
* parameters so far. Byte length of parameters will be added
* to the total length
* @param total_length total byte length of all hashed parameters so
* far. Byte length of parameters will be added to the total length
*/
void get_parameter_incremental_hash(Parameter* param,
uint32_t& hash, uint32_t& carry, uint32_t& total_length);
void get_parameter_incremental_hash(uint32_t& hash, uint32_t& carry,
uint32_t& total_length);

public:
/** io */
Expand Down
Expand Up @@ -69,8 +69,7 @@ bool CGaussianProcessBinaryClassification::train_machine(CFeatures* data)
m_method->set_features(data);

// perform inference
if (m_method->update_parameter_hash())
m_method->update();
m_method->update();

return true;
}
Expand Down
4 changes: 3 additions & 1 deletion src/shogun/evaluation/GradientEvaluation.cpp
Expand Up @@ -51,7 +51,7 @@ void CGradientEvaluation::update_parameter_dictionary()

CEvaluationResult* CGradientEvaluation::evaluate()
{
if (update_parameter_hash())
if (parameter_hash_changed())
update_parameter_dictionary();

// create gradient result object
Expand All @@ -70,5 +70,7 @@ CEvaluationResult* CGradientEvaluation::evaluate()

SG_UNREF(gradient);

update_parameter_hash();

return result;
}
10 changes: 8 additions & 2 deletions src/shogun/machine/GaussianProcessMachine.cpp
Expand Up @@ -63,7 +63,10 @@ SGVector<float64_t> CGaussianProcessMachine::get_posterior_means(CFeatures* data
feat=m_method->get_features();

// get kernel and compute kernel matrix: K(feat, data)*scale^2
CKernel* kernel=m_method->get_kernel();
CKernel* training_kernel=m_method->get_kernel();
CKernel* kernel=CKernel::obtain_from_generic(training_kernel->clone());
SG_UNREF(training_kernel);

kernel->init(feat, data);

// get kernel matrix and create eigen representation of it
Expand Down Expand Up @@ -116,7 +119,10 @@ SGVector<float64_t> CGaussianProcessMachine::get_posterior_variances(
SG_REF(data);

// get kernel and compute kernel matrix: K(data, data)*scale^2
CKernel* kernel=m_method->get_kernel();
CKernel* training_kernel=m_method->get_kernel();
CKernel* kernel=CKernel::obtain_from_generic(training_kernel->clone());
SG_UNREF(training_kernel);

kernel->init(data, data);

// get kernel matrix and create eigen representation of it
Expand Down
19 changes: 13 additions & 6 deletions src/shogun/machine/gp/EPInferenceMethod.cpp
Expand Up @@ -66,54 +66,56 @@ void CEPInferenceMethod::init()

float64_t CEPInferenceMethod::get_negative_log_marginal_likelihood()
{
if (update_parameter_hash())
if (parameter_hash_changed())
update();

return m_nlZ;
}

SGVector<float64_t> CEPInferenceMethod::get_alpha()
{
if (update_parameter_hash())
if (parameter_hash_changed())
update();

return SGVector<float64_t>(m_alpha);
}

SGMatrix<float64_t> CEPInferenceMethod::get_cholesky()
{
if (update_parameter_hash())
if (parameter_hash_changed())
update();

return SGMatrix<float64_t>(m_L);
}

SGVector<float64_t> CEPInferenceMethod::get_diagonal_vector()
{
if (update_parameter_hash())
if (parameter_hash_changed())
update();

return SGVector<float64_t>(m_sttau);
}

SGVector<float64_t> CEPInferenceMethod::get_posterior_mean()
{
if (update_parameter_hash())
if (parameter_hash_changed())
update();

return SGVector<float64_t>(m_mu);
}

SGMatrix<float64_t> CEPInferenceMethod::get_posterior_covariance()
{
if (update_parameter_hash())
if (parameter_hash_changed())
update();

return SGMatrix<float64_t>(m_Sigma);
}

void CEPInferenceMethod::update()
{
SG_DEBUG("entering\n");

// update kernel and feature matrix
CInferenceMethod::update();

Expand Down Expand Up @@ -255,6 +257,11 @@ void CEPInferenceMethod::update()

// update matrices to compute derivatives
update_deriv();

// update hash of the parameters
update_parameter_hash();

SG_DEBUG("leaving\n");
}

void CEPInferenceMethod::update_alpha()
Expand Down
17 changes: 11 additions & 6 deletions src/shogun/machine/gp/ExactInferenceMethod.cpp
Expand Up @@ -40,12 +40,17 @@ CExactInferenceMethod::~CExactInferenceMethod()

void CExactInferenceMethod::update()
{
SG_DEBUG("entering\n");

CInferenceMethod::update();
update_chol();
update_alpha();
update_deriv();
update_mean();
update_cov();
update_parameter_hash();

SG_DEBUG("leaving\n");
}

void CExactInferenceMethod::check_members() const
Expand All @@ -60,7 +65,7 @@ void CExactInferenceMethod::check_members() const

SGVector<float64_t> CExactInferenceMethod::get_diagonal_vector()
{
if (update_parameter_hash())
if (parameter_hash_changed())
update();

// get the sigma variable from the Gaussian likelihood model
Expand All @@ -77,7 +82,7 @@ SGVector<float64_t> CExactInferenceMethod::get_diagonal_vector()

float64_t CExactInferenceMethod::get_negative_log_marginal_likelihood()
{
if (update_parameter_hash())
if (parameter_hash_changed())
update();

// get the sigma variable from the Gaussian likelihood model
Expand Down Expand Up @@ -106,31 +111,31 @@ float64_t CExactInferenceMethod::get_negative_log_marginal_likelihood()

SGVector<float64_t> CExactInferenceMethod::get_alpha()
{
if (update_parameter_hash())
if (parameter_hash_changed())
update();

return SGVector<float64_t>(m_alpha);
}

SGMatrix<float64_t> CExactInferenceMethod::get_cholesky()
{
if (update_parameter_hash())
if (parameter_hash_changed())
update();

return SGMatrix<float64_t>(m_L);
}

SGVector<float64_t> CExactInferenceMethod::get_posterior_mean()
{
if (update_parameter_hash())
if (parameter_hash_changed())
update();

return SGVector<float64_t>(m_mu);
}

SGMatrix<float64_t> CExactInferenceMethod::get_posterior_covariance()
{
if (update_parameter_hash())
if (parameter_hash_changed())
update();

return SGMatrix<float64_t>(m_Sigma);
Expand Down

0 comments on commit 0a9bba3

Please sign in to comment.