Advertisement
Guest User

Untitled

a guest
Aug 23rd, 2012
21
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.24 KB | None | 0 0
  1. #include <shogun/latent/LatentSOModel.h>
  2.  
  3. using namespace shogun;
  4.  
  5. CLatentSOModel::CLatentSOModel()
  6.     : CLatentModel(),
  7.     m_sum_argmax_h(0.0)
  8. {
  9.     register_parameters();
  10. }
  11.  
  12. CLatentSOModel::CLatentSOModel(CLatentFeatures* feats, CLatentLabels* labels)
  13.     : CLatentModel(feats, labels),
  14.     m_sum_argmax_h(0.0)
  15. {
  16.     register_parameters();
  17. }
  18.  
  19. CLatentSOModel::~CLatentSOModel()
  20. {
  21.  
  22. }
  23.  
  24. void CLatentSOModel::register_parameters()
  25. {
  26.  
  27. }
  28.  
  29. float64_t CLatentSOModel::get_sum_argmax_h() const
  30. {
  31.     return m_sum_argmax_h;
  32. }
  33.  
  34. void CLatentSOModel::argmax_h(const SGVector<float64_t>& w)
  35. {
  36.     int32_t num = get_num_vectors();
  37.     ASSERT(num > 0);
  38.  
  39.     for (int32_t i = 0; i < num; ++i)
  40.     {
  41.         // infer h and set it for the argmax_h <w,psi(x,h)>
  42.         CData* latent_data = infer_latent_variable(w, i);
  43.         m_labels->set_latent_label(i, latent_data);
  44.     }
  45. }
  46.  
  47. CLSOModel::CLSOModel()
  48.     : CStructuredModel()
  49. {
  50.  
  51. }
  52.  
  53. CLSOModel::CLSOModel(CLatentSOModel* latent_model)
  54.     : CStructuredModel(),
  55.     m_latent_model(latent_model)
  56. {
  57.     SG_REF(m_latent_model);
  58. }
  59.  
  60. CLSOModel::~CLSOModel()
  61. {
  62.     SG_UNREF(m_latent_model);
  63. }
  64.  
  65. int32_t CLSOModel::get_dim() const
  66. {
  67.     m_latent_model->get_dim();
  68. }
  69.  
  70. CResultSet* CLSOModel::argmax(SGVector< float64_t > w, int32_t feat_idx, bool const training)
  71. {
  72.     CStructuredData* ybar, y;
  73.     CData* hbar;
  74.  
  75.     m_latent_model->get_most_violated_constraint(w, feat_idx, ybar, hbar);
  76.  
  77.     CResultSet* ret = new CResultSet();
  78.     SG_REF(ret);
  79.     ret->score = ;
  80.     ret->delta = m_latent_model->delta(y, ybar, hbar);
  81.  
  82.     return ret;
  83. }
  84.  
  85. float64_t CLSOModel::risk(float64_t* subgrad, float64_t* W, TMultipleCPinfo* info)
  86. {
  87.     int32_t w_dim = get_dim();
  88.     float64_t R = 0.0;
  89.     for (int32_t i=0; i < w_dim; i++)
  90.         subgrad[i] = 0;
  91.  
  92.     for (index_t i = 0; i < m_latent_model->get_num_vectors(); ++i)
  93.     {
  94.         CResultSet* ret = argmax(SGVector<float64_t>(W, w_dim, false), i);
  95.         R += (ret->score - ret->delta);
  96.         SG_UNREF(ret);
  97.     }
  98.  
  99.     /* calculate \f$ \sum_{i=0}^N w\cdot\Psi(x,y,h*) \f$ */
  100.     CDotFeatures* feats = m_latent_model->get_psi_feature_vectors();
  101.     SGVector<float64_t> v(m_latent_model->get_num_vectors());
  102.     feats->dense_dot_range(v.vector, 0, m_latent_model->get_num_vectors(), NULL, W, get_dim(), 0.0);
  103.     R -= SGVector<float64_t>::sum(v.vector, v.vlen);
  104.  
  105.     return R;
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement