Skip to content

Commit

Permalink
Several minor improvements in the SO framework and the HMSVM
Browse files Browse the repository at this point in the history
  • Loading branch information
iglesias committed Jun 3, 2013
1 parent f4f4e2c commit 64e789a
Show file tree
Hide file tree
Showing 22 changed files with 200 additions and 59 deletions.
10 changes: 5 additions & 5 deletions examples/undocumented/libshogun/so_hmsvm_mosek_simple.cpp
Expand Up @@ -24,11 +24,11 @@ int main(int argc, char ** argv)

// No need for ref_counting in SGVector since the data is allocated
// during compilation time
labels->add_label(SGVector< int32_t >(lab1, 4, false));
labels->add_label(SGVector< int32_t >(lab2, 4, false));
labels->add_label(SGVector< int32_t >(lab3, 4, false));
labels->add_label(SGVector< int32_t >(lab4, 4, false));
labels->add_label(SGVector< int32_t >(lab5, 4, false));
labels->add_vector_label(SGVector< int32_t >(lab1, 4, false));
labels->add_vector_label(SGVector< int32_t >(lab2, 4, false));
labels->add_vector_label(SGVector< int32_t >(lab3, 4, false));
labels->add_vector_label(SGVector< int32_t >(lab4, 4, false));
labels->add_vector_label(SGVector< int32_t >(lab5, 4, false));

// Create features
CMatrixFeatures< float64_t >* features = new CMatrixFeatures< float64_t >(5, 3);
Expand Down
2 changes: 1 addition & 1 deletion src/shogun/features/MatrixFeatures.cpp
Expand Up @@ -117,7 +117,7 @@ template< class ST > void CMatrixFeatures< ST >::get_feature_vector_col(
}

template< class ST > void CMatrixFeatures< ST >::set_feature_vector(
SGMatrix< ST > const & vec,
SGMatrix< ST > const vec,
int32_t num)
{
if ( num < 0 || num >= get_num_vectors() )
Expand Down
2 changes: 1 addition & 1 deletion src/shogun/features/MatrixFeatures.h
Expand Up @@ -109,7 +109,7 @@ template< class ST > class CMatrixFeatures : public CFeatures
* @param vec feature vector
* @param num index of vector to set
*/
void set_feature_vector(SGMatrix< ST > const & vec, int32_t num);
void set_feature_vector(SGMatrix< ST > const vec, int32_t num);

/** get features
*
Expand Down
4 changes: 4 additions & 0 deletions src/shogun/machine/LinearStructuredOutputMachine.cpp
Expand Up @@ -84,3 +84,7 @@ void CLinearStructuredOutputMachine::register_parameters()
{
SG_ADD(&m_w, "m_w", "Weight vector", MS_NOT_AVAILABLE);
}

void CLinearStructuredOutputMachine::store_model_features()
{
}
6 changes: 6 additions & 0 deletions src/shogun/machine/LinearStructuredOutputMachine.h
Expand Up @@ -69,6 +69,12 @@ class CLinearStructuredOutputMachine : public CStructuredOutputMachine
*/
virtual CStructuredLabels* apply_structured(CFeatures* data = NULL);

/** Stores feature data of underlying model. Does nothing because
* Linear machines store the normal vector of the separating hyperplane
* and therefore the model anyway
*/
virtual void store_model_features();

/** @return object name */
virtual const char* get_name() const
{
Expand Down
6 changes: 6 additions & 0 deletions src/shogun/machine/StructuredOutputMachine.cpp
Expand Up @@ -67,3 +67,9 @@ void CStructuredOutputMachine::register_parameters()
SG_ADD((CSGObject**)&m_model, "m_model", "Structured model", MS_NOT_AVAILABLE);
SG_ADD((CSGObject**)&m_loss, "m_loss", "Structured loss", MS_NOT_AVAILABLE);
}

void CStructuredOutputMachine::set_labels(CLabels* lab)
{
CMachine::set_labels(lab);
m_model->set_labels(CLabelsFactory::to_structured(lab));
}
6 changes: 6 additions & 0 deletions src/shogun/machine/StructuredOutputMachine.h
Expand Up @@ -73,6 +73,12 @@ class CStructuredOutputMachine : public CMachine
return "StructuredOutputMachine";
}

/** set labels
*
* @param lab labels
*/
virtual void set_labels(CLabels* lab);

private:
/** register class members */
void register_parameters();
Expand Down
17 changes: 15 additions & 2 deletions src/shogun/structure/DirectorStructuredModel.cpp
Expand Up @@ -53,9 +53,22 @@ bool CDirectorStructuredModel::check_training_setup() const
return false;
}

void CDirectorStructuredModel::init_opt(SGMatrix< float64_t > & A, SGVector< float64_t > a, SGMatrix< float64_t > B, SGVector< float64_t > & b, SGVector< float64_t > lb, SGVector< float64_t > ub, SGMatrix < float64_t > & C)
void CDirectorStructuredModel::init_opt(
float64_t regularization,
SGMatrix< float64_t > & A,
SGVector< float64_t > a,
SGMatrix< float64_t > B,
SGVector< float64_t > & b,
SGVector< float64_t > lb,
SGVector< float64_t > ub,
SGMatrix< float64_t > & C)
{
SG_ERROR("Please implemement init_opt(A,a,B,b,lb,ub,C) in your target language before use\n")
SG_ERROR("Please implemement init_opt(regularization,A,a,B,b,lb,ub,C) in your target language before use\n")
}

void CDirectorStructuredModel::init_training()
{
SG_ERROR("Please implemement init_training() in your target language before use\n")
}

#endif /* USE_SWIG_DIRECTORS */
10 changes: 9 additions & 1 deletion src/shogun/structure/DirectorStructuredModel.h
Expand Up @@ -98,7 +98,12 @@ IGNORE_IN_CLASSLIST class CDirectorStructuredModel : public CStructuredModel
* @param ub
* @param C
*/
virtual void init_opt(SGMatrix< float64_t > & A, SGVector< float64_t > a, SGMatrix< float64_t > B, SGVector< float64_t > & b, SGVector< float64_t > lb, SGVector< float64_t > ub, SGMatrix < float64_t > & C);
virtual void init_opt(
float64_t regularization,
SGMatrix< float64_t > & A, SGVector< float64_t > a,
SGMatrix< float64_t > B, SGVector< float64_t > & b,
SGVector< float64_t > lb, SGVector< float64_t > ub,
SGMatrix < float64_t > & C);

using CStructuredModel::director_risk;

Expand All @@ -107,6 +112,9 @@ IGNORE_IN_CLASSLIST class CDirectorStructuredModel : public CStructuredModel
/** @return name of SGSerializable */
virtual const char* get_name() const { return "DirectorStructuredModel"; }

/** initializes the part of the model that needs to be used during training. */
virtual void init_training();

}; /* class CDirectorStructuredModel */
} /* namespace shogun */
#endif /* USE_SWIG_DIRECTORS */
Expand Down
69 changes: 41 additions & 28 deletions src/shogun/structure/HMSVMModel.cpp
Expand Up @@ -48,15 +48,15 @@ CHMSVMModel::~CHMSVMModel()

int32_t CHMSVMModel::get_dim() const
{
// Shorthands for the number of states, the matrix features and their dimension
int32_t S = m_state_model->get_num_states();
// Shorthand for the number of free states
int32_t free_states = ((CSequenceLabels*) m_labels)->get_num_states();
CMatrixFeatures< float64_t >* mf = (CMatrixFeatures< float64_t >*) m_features;
int32_t D = mf->get_num_features();

if ( m_use_plifs )
return S*(S + D*m_num_plif_nodes);
return free_states*(free_states + D*m_num_plif_nodes);
else
return S*(S + D*m_num_obs);
return free_states*(free_states + D*m_num_obs);
}

SGVector< float64_t > CHMSVMModel::get_joint_feature_vector(
Expand All @@ -82,7 +82,9 @@ SGVector< float64_t > CHMSVMModel::get_joint_feature_vector(
m_transmission_weights(state_seq[i],state_seq[i+1]) += 1;

SGMatrix< float64_t > obs = mf->get_feature_vector(feat_idx);
ASSERT(obs.num_rows == D && obs.num_cols == state_seq.vlen)
REQUIRE(obs.num_rows == D && obs.num_cols == state_seq.vlen,
"obs.num_rows (%d) != D (%d) OR obs.num_cols (%d) != state_seq.vlen (%d)\n",
obs.num_rows, D, obs.num_cols, state_seq.vlen)
m_emission_weights.zero();
index_t aux_idx, weight_idx;

Expand All @@ -105,23 +107,19 @@ SGVector< float64_t > CHMSVMModel::get_joint_feature_vector(
else // Use PLiFs
{
int32_t S = m_state_model->get_num_states();
CPlif* plif;
float64_t* limits;
// The observation value
float64_t value;
// The number of supporting points smaller or equal than value
int32_t count;

for ( int32_t f = 0 ; f < D ; ++f )
{
aux_idx = f*m_num_plif_nodes;

for ( int32_t j = 0 ; j < state_seq.vlen ; ++j )
{
plif = (CPlif*) m_plif_matrix->get_element(S*f + state_seq[j]);
limits = plif->get_plif_limits();
count = 0;
value = obs(f,j);
CPlif* plif = (CPlif*) m_plif_matrix->get_element(S*f + state_seq[j]);
float64_t* limits = plif->get_plif_limits();
// The number of supporting points smaller or equal than value
int32_t count = 0;
// The observation value
float64_t value = obs(f,j);

for ( int32_t i = 0 ; i < m_num_plif_nodes ; ++i )
{
Expand Down Expand Up @@ -214,17 +212,16 @@ CResultSet* CHMSVMModel::argmax(
{
m_state_model->reshape_emission_params(m_plif_matrix, w, D, m_num_plif_nodes);

CPlif* plif;
for ( int32_t f = 0 ; f < D ; ++f )
for ( int32_t i = 0 ; i < T ; ++i )
{
for ( int32_t s = 0 ; s < S ; ++s )
for ( int32_t f = 0 ; f < D ; ++f )
{
plif = (CPlif*) m_plif_matrix->get_element(S*f + s);

for ( int32_t i = 0 ; i < T ; ++i )
for ( int32_t s = 0 ; s < S ; ++s )
{
CPlif* plif = (CPlif*) m_plif_matrix->get_element(S*f + s);
E(s,i) += plif->lookup( x(f,i) );

SG_UNREF(plif);
SG_UNREF(plif);
}
}
}
}
Expand Down Expand Up @@ -353,6 +350,7 @@ float64_t CHMSVMModel::delta_loss(CStructuredData* y1, CStructuredData* y2)
}

void CHMSVMModel::init_opt(
float64_t regularization,
SGMatrix< float64_t > & A,
SGVector< float64_t > a,
SGMatrix< float64_t > B,
Expand All @@ -361,19 +359,18 @@ void CHMSVMModel::init_opt(
SGVector< float64_t > ub,
SGMatrix< float64_t > & C)
{
// Shorthand for the number of free states (i.e. states that have all their
// parameters learnt)
// Shorthand for the number of free states (i.e. states for which parameters are learnt)
int32_t S = ((CSequenceLabels*) m_labels)->get_num_states();
// Shorthand for the number of features of the feature vector
int32_t D = ((CMatrixFeatures< float64_t >*) m_features)->get_num_features();

// Monotonicity constraints for feature scoring functions
SGVector< int32_t > monotonicity = m_state_model->get_monotonicity(S,D);

// Quadratic regularizer
// Quadratic regularization

float64_t C_small = 5.0;
float64_t C_smooth = 10.0;
float64_t C_small = regularization;
float64_t C_smooth = 0.02*regularization;
// TODO change the representation of C to sparse matrix
C = SGMatrix< float64_t >(get_dim()+m_num_aux, get_dim()+m_num_aux);
C.zero();
Expand Down Expand Up @@ -593,3 +590,19 @@ void CHMSVMModel::init_training()
}
}
}

SGMatrix< float64_t > CHMSVMModel::get_transmission_weights() const
{
return m_transmission_weights;
}

SGVector< float64_t > CHMSVMModel::get_emission_weights() const
{
return m_emission_weights;
}

CStateModel* CHMSVMModel::get_state_model() const
{
SG_REF(m_state_model);
return m_state_model;
}
23 changes: 21 additions & 2 deletions src/shogun/structure/HMSVMModel.h
Expand Up @@ -100,6 +100,7 @@ class CHMSVMModel : public CStructuredModel
* @param C
*/
virtual void init_opt(
float64_t regularization,
SGMatrix< float64_t > & A, SGVector< float64_t > a,
SGMatrix< float64_t > B, SGVector< float64_t > & b,
SGVector< float64_t > lb, SGVector< float64_t > ub,
Expand All @@ -117,7 +118,7 @@ class CHMSVMModel : public CStructuredModel
* implement smoothness regularization between adjacent emission
* scores via constraints.
*
* return the number of auxiliary variables
* @return the number of auxiliary variables
*/
virtual int32_t get_num_aux() const;

Expand All @@ -126,7 +127,7 @@ class CHMSVMModel : public CStructuredModel
* optimization problem. These constraints are used to implement
* smoothness regularization between adjacent emission scores.
*
* return the number of auxiliary constraints
* @return the number of auxiliary constraints
*/
virtual int32_t get_num_aux_con() const;

Expand All @@ -143,6 +144,24 @@ class CHMSVMModel : public CStructuredModel
*/
virtual void init_training();

/** get transmission weights
*
* @return vector with the transmission weights
*/
SGMatrix< float64_t > get_transmission_weights() const;

/** get emission weights
*
* @return vector with the emission weights
*/
SGVector< float64_t > get_emission_weights() const;

/** get state model
*
* @return model with the description of the states
*/
CStateModel* get_state_model() const;

private:
/* internal initialization */
void init();
Expand Down
3 changes: 2 additions & 1 deletion src/shogun/structure/MulticlassModel.cpp
Expand Up @@ -146,6 +146,7 @@ float64_t CMulticlassModel::delta_loss(float64_t y1, float64_t y2)
}

void CMulticlassModel::init_opt(
float64_t regularization,
SGMatrix< float64_t > & A,
SGVector< float64_t > a,
SGMatrix< float64_t > B,
Expand All @@ -154,7 +155,7 @@ void CMulticlassModel::init_opt(
SGVector< float64_t > ub,
SGMatrix< float64_t > & C)
{
C = SGMatrix< float64_t >::create_identity_matrix(get_dim(), 1);
C = SGMatrix< float64_t >::create_identity_matrix(get_dim(), regularization);
}

void CMulticlassModel::init()
Expand Down
1 change: 1 addition & 0 deletions src/shogun/structure/MulticlassModel.h
Expand Up @@ -93,6 +93,7 @@ class CMulticlassModel : public CStructuredModel
* @param C
*/
virtual void init_opt(
float64_t regularization,
SGMatrix< float64_t > & A, SGVector< float64_t > a,
SGMatrix< float64_t > B, SGVector< float64_t > & b,
SGVector< float64_t > lb, SGVector< float64_t > ub,
Expand Down

0 comments on commit 64e789a

Please sign in to comment.