Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #756 from iglesias/master
Fix memory leaks in BMRM and fix the generic risk function
  • Loading branch information
lisitsyn committed Aug 24, 2012
2 parents 10341ca + 0a2700a commit 093b502
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 12 deletions.
3 changes: 3 additions & 0 deletions src/shogun/structure/HMSVMModel.cpp
Expand Up @@ -111,6 +111,7 @@ CResultSet* CHMSVMModel::argmax(
int32_t feat_idx,
bool const training)
{
int32_t dim = get_dim();
ASSERT( w.vlen == get_dim() );

// Shorthand for the number of features of the feature vector
Expand Down Expand Up @@ -254,6 +255,8 @@ CResultSet* CHMSVMModel::argmax(
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);
}

return ret;
Expand Down
4 changes: 3 additions & 1 deletion src/shogun/structure/HMSVMModel.h
Expand Up @@ -64,7 +64,9 @@ class CHMSVMModel : public CStructuredModel
*/
virtual SGVector< float64_t > get_joint_feature_vector(int32_t feat_idx, CStructuredData* y);

/** obtains the argmax
/**
* obtains the argmax of \f$ \Delta(y_{pred}, y_{truth}) +
* \langle w, \Psi(x_{truth}, y_{pred}) \rangle \f$
*
* @param w weight vector
* @param feat_idx index of the feature to compute the argmax
Expand Down
14 changes: 11 additions & 3 deletions src/shogun/structure/MulticlassModel.cpp
Expand Up @@ -76,7 +76,8 @@ CResultSet* CMulticlassModel::argmax(
"using it for prediction\n");
}

ASSERT(feats_dim*m_num_classes == w.vlen);
int32_t dim = get_dim();
ASSERT(dim == w.vlen);

// Find the class that gives the maximum score

Expand Down Expand Up @@ -107,8 +108,11 @@ CResultSet* CMulticlassModel::argmax(
ret->argmax = y;
if ( training )
{
ret->psi_truth = CStructuredModel::get_joint_feature_vector(feat_idx, feat_idx);
ret->delta = CStructuredModel::delta_loss(feat_idx, y);
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 Expand Up @@ -190,12 +194,14 @@ float64_t CMulticlassModel::risk(float64_t* subgrad, float64_t* W, TMultipleCPin
float64_t loss=0.0;
uint32_t yhat=0;
uint32_t GT=0;
CRealNumber* GT_rn=NULL;

/* loop through examples */
for(uint32_t i=from; i<to; ++i)
{
Rmax=-CMath::INFTY;
GT=(uint32_t)((CRealNumber*)y->get_label(i))->value;
GT_rn=CRealNumber::obtain_from_generic(y->get_label(i));
GT=(uint32_t)GT_rn->value;

for (uint32_t c = 0; c < num_classes; ++c)
{
Expand All @@ -213,6 +219,8 @@ float64_t CMulticlassModel::risk(float64_t* subgrad, float64_t* W, TMultipleCPin

X->add_to_dense_vec(1.0, i, subgrad+yhat*feats_dim, feats_dim);
X->add_to_dense_vec(-1.0, i, subgrad+GT*feats_dim, feats_dim);

SG_UNREF(GT_rn);
}

return R;
Expand Down
4 changes: 3 additions & 1 deletion src/shogun/structure/MulticlassModel.h
Expand Up @@ -58,7 +58,9 @@ class CMulticlassModel : public CStructuredModel
*/
virtual SGVector< float64_t > get_joint_feature_vector(int32_t feat_idx, CStructuredData* y);

/** obtains the argmax
/**
* obtains the argmax of \f$ \Delta(y_{pred}, y_{truth}) +
* \langle w, \Psi(x_{truth}, y_{pred}) \rangle \f$
*
* @param w weight vector
* @param feat_idx index of the feature to compute the argmax
Expand Down
3 changes: 3 additions & 0 deletions src/shogun/structure/StructuredModel.cpp
Expand Up @@ -159,6 +159,9 @@ float64_t CStructuredModel::risk(float64_t* subgrad, float64_t* W, TMultipleCPin

int32_t dim = this->get_dim();
float64_t R = 0.0;
for (uint32_t i=0; i<dim; i++)
subgrad[i] = 0;

for (int32_t i=from; i<to; i++)
{
CResultSet* result = this->argmax(SGVector<float64_t>(W,dim,false), i, true);
Expand Down
7 changes: 5 additions & 2 deletions src/shogun/structure/StructuredModel.h
Expand Up @@ -50,7 +50,8 @@ struct CResultSet : public CSGObject
/** joint feature vector for the prediction */
SGVector< float64_t > psi_pred;

/** corresponding score */
/** \f$ \Delta(y_{pred}, y_{truth}) + \langle w,
* \Psi(x_{truth}, y_{pred}) - \Psi(x_{truth}, y_{truth}) \rangle \f$ */
float64_t score;

/** delta loss for the prediction vs. truth */
Expand Down Expand Up @@ -160,7 +161,9 @@ class CStructuredModel : public CSGObject
*/
virtual SGVector< float64_t > get_joint_feature_vector(int32_t feat_idx, CStructuredData* y);

/** obtains the argmax
/**
* obtains the argmax of \f$ \Delta(y_{pred}, y_{truth}) +
* \langle w, \Psi(x_{truth}, y_{pred}) \rangle \f$
*
* @param w weight vector
* @param feat_idx index of the feature to compute the argmax
Expand Down
8 changes: 3 additions & 5 deletions src/shogun/structure/libbmrm.cpp
Expand Up @@ -263,10 +263,9 @@ bmrm_return_value_T svm_bmrm_solver(
goto cleanup;
}


bmrm.hist_Fp.resize_vector(BufSize);
bmrm.hist_Fd.resize_vector(BufSize);
bmrm.hist_wdist.resize_vector(BufSize);
bmrm.hist_Fp = SGVector< float64_t >(BufSize);
bmrm.hist_Fd = SGVector< float64_t >(BufSize);
bmrm.hist_wdist = SGVector< float64_t >(BufSize);

/* Iinitial solution */
R=model->risk(subgrad, W);
Expand Down Expand Up @@ -524,7 +523,6 @@ bmrm_return_value_T svm_bmrm_solver(
}
} /* end of main loop */


bmrm.hist_Fp.resize_vector(bmrm.nIter);
bmrm.hist_Fd.resize_vector(bmrm.nIter);
bmrm.hist_wdist.resize_vector(bmrm.nIter);
Expand Down

0 comments on commit 093b502

Please sign in to comment.