Skip to content

Commit

Permalink
Merge pull request #1803 from iglesias/develop
Browse files Browse the repository at this point in the history
Small refactoring and gitignore update
  • Loading branch information
iglesias committed Dec 30, 2013
2 parents 75cf24e + 733f1b3 commit 7279a7a
Show file tree
Hide file tree
Showing 11 changed files with 32 additions and 51 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Expand Up @@ -130,3 +130,6 @@ examples/undocumented/python_modular/*
CMakeCache.txt
/build
/third_party

# protobuf
src/shogun/io/protobuf/*.pb.*
Expand Up @@ -9,10 +9,8 @@
parameter_list=[[data_dict]]

def structure_discrete_hmsvm_bmrm (m_data_dict=data_dict):
from modshogun import RealMatrixFeatures
from modshogun import SequenceLabels, HMSVMModel, Sequence, TwoStateModel, SMT_TWO_STATE
from modshogun import StructuredAccuracy
from modshogun import DualLibQPBMSOSVM
from modshogun import RealMatrixFeatures, SequenceLabels, HMSVMModel, Sequence, TwoStateModel
from modshogun import StructuredAccuracy, DualLibQPBMSOSVM, SMT_TWO_STATE

labels_array = m_data_dict['label'][0]

Expand Down
Expand Up @@ -9,9 +9,8 @@
parameter_list=[[data_dict]]

def structure_discrete_hmsvm_mosek (m_data_dict=data_dict):
from modshogun import RealMatrixFeatures
from modshogun import SequenceLabels, HMSVMModel, Sequence, TwoStateModel, SMT_TWO_STATE
from modshogun import StructuredAccuracy
from modshogun import RealMatrixFeatures, SequenceLabels, HMSVMModel, Sequence, TwoStateModel
from modshogun import StructuredAccuracy, SMT_TWO_STATE

try:
from modshogun import PrimalMosekSOSVM
Expand Down
Expand Up @@ -3,9 +3,7 @@
parameter_list=[[100, 250, 10, 2]]

def structure_plif_hmsvm_bmrm (num_examples, example_length, num_features, num_noise_features):
from modshogun import RealMatrixFeatures
from modshogun import TwoStateModel, DualLibQPBMSOSVM
from modshogun import StructuredAccuracy
from modshogun import RealMatrixFeatures, TwoStateModel, DualLibQPBMSOSVM, StructuredAccuracy

model = TwoStateModel.simulate_data(num_examples, example_length, num_features, num_noise_features)
sosvm = DualLibQPBMSOSVM(model, model.get_labels(), 5000.0)
Expand Down
Expand Up @@ -3,9 +3,7 @@
parameter_list=[[100, 250, 10, 2]]

def structure_plif_hmsvm_mosek (num_examples, example_length, num_features, num_noise_features):
from modshogun import RealMatrixFeatures
from modshogun import TwoStateModel
from modshogun import StructuredAccuracy
from modshogun import RealMatrixFeatures, TwoStateModel, StructuredAccuracy

try:
from modshogun import PrimalMosekSOSVM
Expand Down
4 changes: 3 additions & 1 deletion src/shogun/distributions/Distribution.h
Expand Up @@ -43,6 +43,8 @@ class CDistribution : public CSGObject
public:
/** default constructor */
CDistribution();

/** destructor */
virtual ~CDistribution();

/** learn distribution
Expand Down Expand Up @@ -72,7 +74,7 @@ class CDistribution : public CSGObject

/** get model parameter (logarithmic)
*
* abstrac base method
* abstract base method
*
* @return model parameter (logarithmic)
*/
Expand Down
12 changes: 4 additions & 8 deletions src/shogun/evaluation/StructuredAccuracy.cpp
Expand Up @@ -69,10 +69,8 @@ float64_t CStructuredAccuracy::evaluate_real(CStructuredLabels* predicted,

for ( int32_t i = 0 ; i < length ; ++i )
{
CRealNumber* truth =
CRealNumber::obtain_from_generic(ground_truth->get_label(i));
CRealNumber* pred =
CRealNumber::obtain_from_generic(predicted->get_label(i));
CRealNumber* truth = CRealNumber::obtain_from_generic(ground_truth->get_label(i));
CRealNumber* pred = CRealNumber::obtain_from_generic(predicted->get_label(i));

num_equal += truth->value == pred->value;

Expand All @@ -93,10 +91,8 @@ float64_t CStructuredAccuracy::evaluate_sequence(CStructuredLabels* predicted,

for ( int32_t i = 0 ; i < length ; ++i )
{
CSequence* true_seq =
CSequence::obtain_from_generic(ground_truth->get_label(i));
CSequence* pred_seq =
CSequence::obtain_from_generic(predicted->get_label(i));
CSequence* true_seq = CSequence::obtain_from_generic(ground_truth->get_label(i));
CSequence* pred_seq = CSequence::obtain_from_generic(predicted->get_label(i));

SGVector<int32_t> true_seq_data = true_seq->get_data();
SGVector<int32_t> pred_seq_data = pred_seq->get_data();
Expand Down
5 changes: 5 additions & 0 deletions src/shogun/evaluation/StructuredAccuracy.h
Expand Up @@ -48,11 +48,16 @@ class CStructuredAccuracy : public CEvaluation
*/
static SGMatrix<int32_t> get_confusion_matrix(CLabels* predicted, CLabels* ground_truth);

/** whether the evaluation criterion has to be maximimed or minimized
*
* @return maximize evaluation criterion
*/
inline EEvaluationDirection get_evaluation_direction() const
{
return ED_MAXIMIZE;
}

/** @return name of SGSerializable */
virtual const char* get_name() const { return "StructuredAccuracy"; }

private:
Expand Down
6 changes: 2 additions & 4 deletions src/shogun/structure/HMSVMModel.cpp
Expand Up @@ -335,10 +335,8 @@ CResultSet* CHMSVMModel::argmax(
if ( training )
{
ret->delta = CStructuredModel::delta_loss(feat_idx, ypred);
ret->psi_truth = CStructuredModel::get_joint_feature_vector(
feat_idx, feat_idx);
ret->score -= SGVector< float64_t >::dot(w.vector,
ret->psi_truth.vector, dim);
ret->psi_truth = CStructuredModel::get_joint_feature_vector(feat_idx, feat_idx);
ret->score -= SGVector< float64_t >::dot(w.vector, ret->psi_truth.vector, dim);
}

return ret;
Expand Down
33 changes: 9 additions & 24 deletions src/shogun/structure/PrimalMosekSOSVM.cpp
Expand Up @@ -76,13 +76,7 @@ bool CPrimalMosekSOSVM::train_machine(CFeatures* data)
// Interface with MOSEK
CMosek* mosek = new CMosek(0, M+num_aux+N);
SG_REF(mosek);
if ( mosek->get_rescode() != MSK_RES_OK )
{
SG_PRINT("Mosek object could not be properly created..."
"aborting training of PrimalMosekSOSVM\n");

return false;
}
REQUIRE(mosek->get_rescode() == MSK_RES_OK, "Mosek object could not be properly created in PrimalMosekSOSVM training.\n");

// Initialize the terms of the optimization problem
SGMatrix< float64_t > A, B, C;
Expand All @@ -92,11 +86,8 @@ bool CPrimalMosekSOSVM::train_machine(CFeatures* data)
SG_DEBUG("Regularization used in PrimalMosekSOSVM equal to %.2f.\n", m_regularization);

// Input terms of the problem that do not change between iterations
if ( mosek->init_sosvm(M, N, num_aux, num_aux_con, C, lb, ub, A, b) != MSK_RES_OK )
{
// MOSEK error took place
return false;
}
REQUIRE(mosek->init_sosvm(M, N, num_aux, num_aux_con, C, lb, ub, A, b) == MSK_RES_OK,
"Mosek error in PrimalMosekSOSVM initializing SO-SVM.\n")

// Initialize the weight vector
m_w = SGVector< float64_t >(M);
Expand All @@ -119,11 +110,6 @@ bool CPrimalMosekSOSVM::train_machine(CFeatures* data)
// Initialize variables used in the loop
int32_t num_con = num_aux_con; // number of constraints
int32_t old_num_con = num_con;
float64_t slack = 0.0;
float64_t max_slack = 0.0;
CResultSet* result = NULL;
CResultSet* cur_res = NULL;
CList* cur_list = NULL;
bool exception = false;
index_t iteration = 0;

Expand All @@ -142,24 +128,23 @@ bool CPrimalMosekSOSVM::train_machine(CFeatures* data)
for ( int32_t i = 0 ; i < N ; ++i )
{
// Predict the result of the ith training example (loss-aug)
result = m_model->argmax(m_w, i);
CResultSet* result = m_model->argmax(m_w, i);

// Compute the loss associated with the prediction (surrogate loss, max(0, \tilde{H}))
slack = CHingeLoss().loss( compute_loss_arg(result) );
cur_list = (CList*) results->get_element(i);
float64_t slack = CHingeLoss().loss( compute_loss_arg(result) );
CList* cur_list = (CList*) results->get_element(i);

// Update the list of constraints
if ( cur_list->get_num_elements() > 0 )
{
// Find the maximum loss within the elements of
// the list of constraints
cur_res = (CResultSet*) cur_list->get_first_element();
max_slack = -CMath::INFTY;
CResultSet* cur_res = (CResultSet*) cur_list->get_first_element();
float64_t max_slack = -CMath::INFTY;

while ( cur_res != NULL )
{
max_slack = CMath::max(max_slack,
CHingeLoss().loss( compute_loss_arg(cur_res) ));
max_slack = CMath::max(max_slack, CHingeLoss().loss( compute_loss_arg(cur_res) ));

SG_UNREF(cur_res);
cur_res = (CResultSet*) cur_list->get_next_element();
Expand Down
1 change: 0 additions & 1 deletion src/shogun/structure/SequenceLabels.h
Expand Up @@ -59,7 +59,6 @@ class CSequence : public CStructuredData
SGVector<int32_t> get_data() const { return data; }

protected:

/** data sequence */
SGVector< int32_t > data;

Expand Down

0 comments on commit 7279a7a

Please sign in to comment.