Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #582 from uricamic/BM_SOL
Bundle method solver for structured output learning [Work in progress]
- Loading branch information
Showing
6 changed files
with
502 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* | ||
* This program is free software; you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation; either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* Written (W) 2012 Michal Uricar | ||
* Copyright (C) 2012 Michal Uricar | ||
*/ | ||
|
||
#include <shogun/so/DualLibQPBMSOSVM.h> | ||
#include <shogun/so/libbmrm.h> | ||
|
||
using namespace shogun; | ||
|
||
CDualLibQPBMSOSVM::CDualLibQPBMSOSVM() | ||
:CLinearStructuredOutputMachine() | ||
{ | ||
} | ||
|
||
CDualLibQPBMSOSVM::CDualLibQPBMSOSVM( | ||
CStructuredModel* model, | ||
CLossFunction* loss, | ||
CStructuredLabels* labs, | ||
CFeatures* features, | ||
float64_t lambda) | ||
:CLinearStructuredOutputMachine(model, loss, labs, features) | ||
{ | ||
set_TolRel(0.001); | ||
set_TolAbs(0.0); | ||
set_BufSize(100); | ||
set_lambda(lambda); | ||
} | ||
|
||
CDualLibQPBMSOSVM::~CDualLibQPBMSOSVM() | ||
{ | ||
} | ||
|
||
bool CDualLibQPBMSOSVM::train_machine(CFeatures* data) | ||
{ | ||
// get dimension of w | ||
uint32_t nDim=0; | ||
//uint32_t nDim=m_risk_function->get_dim(data); //TODO: get_dim function accessible trough StructuredModel | ||
|
||
// call the BMRM solver | ||
bmrm_return_value_T result = svm_bmrm_solver(data, m_w.vector, m_TolRel, m_TolAbs, m_lambda, | ||
m_BufSize, nDim, m_risk_function); | ||
|
||
if (result.exitflag==1) | ||
{ | ||
return true; | ||
} else { | ||
return false; | ||
} | ||
} | ||
|
||
void CDualLibQPBMSOSVM::register_parameters() | ||
{ | ||
SG_ADD(&m_w, "m_w", "Weight vector", MS_NOT_AVAILABLE); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
/* | ||
* This program is free software; you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation; either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* Written (W) 2012 Michal Uricar | ||
* Copyright (C) 2012 Michal Uricar | ||
*/ | ||
|
||
#ifndef _DUALLIBQPBMSOSVM__H__ | ||
#define _DUALLIBQPBMSOSVM__H__ | ||
|
||
#include <shogun/machine/LinearStructuredOutputMachine.h> | ||
#include <shogun/so/RiskFunction.h> | ||
|
||
namespace shogun | ||
{ | ||
|
||
class CDualLibQPBMSOSVM : public CLinearStructuredOutputMachine | ||
{ | ||
public: | ||
/** default constructor */ | ||
CDualLibQPBMSOSVM(); | ||
|
||
/** standard constructor | ||
* | ||
*/ | ||
CDualLibQPBMSOSVM(CStructuredModel* model, CLossFunction* loss, CStructuredLabels* labs, CFeatures* features, float64_t lambda); | ||
|
||
/** destructor */ | ||
~CDualLibQPBMSOSVM(); | ||
|
||
/** set lambda */ | ||
inline void set_lambda(float64_t lambda) { m_lambda=lambda; } | ||
|
||
/** get lambda */ | ||
inline float64_t get_lambda() { return m_lambda; } | ||
|
||
/** set relative tolerance */ | ||
inline void set_TolRel(float64_t TolRel) { m_TolRel=TolRel; } | ||
|
||
/** get relative tolerance */ | ||
inline float64_t get_TolRel() { return m_TolRel; } | ||
|
||
/** set absolute tolerance */ | ||
inline void set_TolAbs(float64_t TolAbs) { m_TolAbs=TolAbs; } | ||
|
||
/** get absolute tolerance */ | ||
inline float64_t get_TolAbs() { return m_TolAbs; } | ||
|
||
/** set size of cutting plane buffer */ | ||
inline void set_BufSize(uint32_t BufSize) { m_BufSize=BufSize; } | ||
|
||
/** get size of cutting plane buffer */ | ||
inline uint32_t get_BufSize() { return m_BufSize; } | ||
|
||
protected: | ||
/** train dual SO-SVM | ||
* | ||
*/ | ||
bool train_machine(CFeatures* data=NULL); | ||
|
||
private: | ||
/** register class parameters */ | ||
void register_parameters(); | ||
|
||
private: | ||
/** weight vector */ | ||
SGVector< float64_t > m_w; | ||
|
||
/** lambda */ | ||
float64_t m_lambda; | ||
|
||
/** TolRel */ | ||
float64_t m_TolRel; | ||
|
||
/** TolAbs */ | ||
float64_t m_TolAbs; | ||
|
||
/** BufSize */ | ||
uint32_t m_BufSize; | ||
|
||
/** Risk function */ | ||
CRiskFunction* m_risk_function; | ||
|
||
}; /* class CDualLibQPBMSOSVM */ | ||
|
||
} /* namespace shogun */ | ||
|
||
#endif /* _DUALLIBQPBMSOSVM__H__ */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/* | ||
* This program is free software; you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation; either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* Written (W) 2012 Michal Uricar | ||
* Copyright (C) 2012 Michal Uricar | ||
*/ | ||
|
||
#include <shogun/so/RiskFunction.h> | ||
|
||
using namespace shogun; | ||
|
||
CRiskFunction::CRiskFunction() | ||
: CSGObject() | ||
{ | ||
} | ||
|
||
CRiskFunction::~CRiskFunction() | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* | ||
* This program is free software; you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation; either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* Written (W) 2012 Michal Uricar | ||
* Copyright (C) 2012 Michal Uricar | ||
*/ | ||
|
||
#ifndef _RISK_FUNCTION__H__ | ||
#define _RISK_FUNCTION__H__ | ||
|
||
#include <shogun/base/SGObject.h> | ||
//#include <shogun/features/Features.h> | ||
//#include <shogun/labels/StructuredLabels.h> | ||
//#include <shogun/lib/SGVector.h> | ||
|
||
namespace shogun | ||
{ | ||
|
||
/** @brief Class CRiskFunction TODO | ||
* | ||
*/ | ||
class CRiskFunction : public CSGObject | ||
{ | ||
public: | ||
/** default constructor */ | ||
CRiskFunction(); | ||
|
||
/** destructor */ | ||
virtual ~CRiskFunction(); | ||
|
||
/** computes the value of the risk function and sub-gradient at given point | ||
* | ||
*/ | ||
//virtual void risk(void* data, float64_t* R, SGVector< float64_t > subgrad, SGVector< float64_t > w) = 0; | ||
virtual void risk(void* data, float64_t* R, float64_t* subgrad, float64_t* W) = 0; | ||
|
||
/** get the dimension of joint feature vector w | ||
* | ||
*/ | ||
//virtual uint32_t get_dim(void* data) = 0; //TODO: Delete, will be accessible from StructuredModel | ||
|
||
/** @return name of SGSerializable */ | ||
virtual const char* get_name() const { return "RiskFunction"; } | ||
|
||
}; /* CRiskFunction */ | ||
|
||
} | ||
|
||
#endif /* _RISK_FUNCTION__H__ */ |
Oops, something went wrong.