Skip to content

Commit

Permalink
minor RiskFunction change + MulticlassRiskFunction implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
uricamic committed Jun 21, 2012
1 parent 2cbb216 commit 6e12559
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 9 deletions.
79 changes: 79 additions & 0 deletions src/shogun/structure/MulticlassRiskFunction.cpp
@@ -0,0 +1,79 @@
/*
* 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/features/DotFeatures.h>
#include <shogun/structure/MulticlassRiskFunction.h>
#include <shogun/structure/libbmrm.h>
#include <shogun/structure/MulticlassSOLabels.h>
#include <shogun/structure/MulticlassModel.h>

using namespace shogun;

CMulticlassRiskFunction::CMulticlassRiskFunction()
:CRiskFunction()
{
}

CMulticlassRiskFunction::~CMulticlassRiskFunction()
{
}

void CMulticlassRiskFunction::risk(void* data, float64_t* R, float64_t* subgrad, float64_t* W)
{
bmrm_data_T* data_struct=(bmrm_data_T*)data;
CDotFeatures* X=(CDotFeatures*)data_struct->X;
CMulticlassSOLabels* y=(CMulticlassSOLabels*)data_struct->y;

uint32_t N=X->get_num_vectors();
uint32_t num_classes=y->get_num_classes();
uint32_t feats_dim=X->get_dim_feature_space();
uint32_t w_dim=(uint32_t)data_struct->w_dim;

*R=0;
SGVector< float64_t > subgradient(w_dim);
subgradient.zero();

SGVector< float64_t > xi;

float64_t Rtmp=0.0;
float64_t Rmax=0.0;
float64_t loss=0.0;
uint32_t yhat=0;
uint32_t GT=0;

/* loop through examples */
for(uint32_t i = 0; i < N; ++i)
{
Rmax=-CMath::INFTY;
xi=X->get_computed_dot_feature_vector(i);
GT=(uint32_t)((CRealNumber*)y->get_label(i))->value;

for (uint32_t c = 0; c < num_classes; ++c)
{
loss=(c == GT) ? 0.0 : 1.0;
Rtmp = loss + SGVector< float64_t >::dot(W+c*feats_dim, xi.vector, feats_dim) - SGVector< float64_t >::dot(W+GT*feats_dim, xi.vector, feats_dim);

if (Rtmp > Rmax)
{
Rmax=Rtmp;
yhat=c;
}
}
*R += Rmax;

for(uint32_t j = 0; j < feats_dim; ++j)
{
subgradient[yhat*feats_dim+j]+=xi[j];
subgradient[GT*feats_dim+j]-=xi[j];
}

}
memcpy(subgrad, subgradient.vector, w_dim*sizeof(float64_t));
}
39 changes: 39 additions & 0 deletions src/shogun/structure/MulticlassRiskFunction.h
@@ -0,0 +1,39 @@
/*
* 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 _MULTICLASSRISK_FUNCTION__H__
#define _MULTICLASSRISK_FUNCITON__H__

#include <shogun/structure/RiskFunction.h>

namespace shogun
{

/** @brief Class CMulticlassRiskFunction
*
*/
class CMulticlassRiskFunction : public CRiskFunction
{
public:
/** default constructor */
CMulticlassRiskFunction();

/** destructor */
~CMulticlassRiskFunction();

virtual void risk(void* data, float64_t* R, float64_t* subgrad, float64_t* W);

virtual const char* get_name() const { return "MulticlassRiskFunction"; }

}; /* CMulticlassRiskFunction */

}

#endif /* _MULTICLASSTISK_FUNCTION__H__ */
9 changes: 0 additions & 9 deletions src/shogun/structure/RiskFunction.h
Expand Up @@ -12,9 +12,6 @@
#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
{
Expand All @@ -34,14 +31,8 @@ class CRiskFunction : public CSGObject
/** 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"; }

Expand Down

0 comments on commit 6e12559

Please sign in to comment.