Skip to content

Commit

Permalink
Added FeatureBlockLogisticRegression
Browse files Browse the repository at this point in the history
  • Loading branch information
lisitsyn committed Jul 5, 2012
1 parent eea1b33 commit 9c4e1d8
Show file tree
Hide file tree
Showing 4 changed files with 214 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/interfaces/modular/Classifier.i
Expand Up @@ -63,6 +63,7 @@
%rename(SVMLight) CSVMLight;
%rename(SVMLightOneClass) CSVMLightOneClass;
#endif //USE_SVMLIGHT
%rename(FeatureBlockLogisticRegression) CFeatureBlockLogisticRegression;
%rename(DirectorLinearMachine) CDirectorLinearMachine;
%rename(DirectorKernelMachine) CDirectorKernelMachine;

Expand Down Expand Up @@ -107,6 +108,8 @@
%include <shogun/classifier/vw/VowpalWabbit.h>
%include <shogun/classifier/svm/NewtonSVM.h>
%include <shogun/classifier/svm/PegasosSVM.h>
%include <shogun/machine/SLEPMachine.h>
%include <shogun/classifier/FeatureBlockLogisticRegression.h>
%include <shogun/machine/DirectorLinearMachine.h>
%include <shogun/machine/DirectorKernelMachine.h>

Expand Down
1 change: 1 addition & 0 deletions src/interfaces/modular/Classifier_includes.i
Expand Up @@ -38,6 +38,7 @@
#include <shogun/classifier/svm/SVMLightOneClass.h>
#endif //USE_SVMLIGHT

#include <shogun/classifier/FeatureBlockLogisticRegression.h>
#include <shogun/machine/DirectorLinearMachine.h>
#include <shogun/machine/DirectorKernelMachine.h>
%}
134 changes: 134 additions & 0 deletions src/shogun/classifier/FeatureBlockLogisticRegression.cpp
@@ -0,0 +1,134 @@
/*
* 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.
*
* Copyright (C) 2012 Sergey Lisitsyn
*/

#include <shogun/classifier/FeatureBlockLogisticRegression.h>
#include <shogun/lib/slep/slep_logistic.h>
#include <shogun/lib/slep/slep_options.h>

#include <shogun/lib/IndexBlockGroup.h>
#include <shogun/lib/IndexBlockTree.h>

namespace shogun
{

CFeatureBlockLogisticRegression::CFeatureBlockLogisticRegression() :
CSLEPMachine(),
m_feature_relation(NULL)
{
register_parameters();
}

CFeatureBlockLogisticRegression::CFeatureBlockLogisticRegression(
float64_t z, CDotFeatures* train_features,
CBinaryLabels* train_labels, CIndexBlockRelation* feature_relation) :
CSLEPMachine(z,train_features,(CLabels*)train_labels),
m_feature_relation(NULL)
{
set_feature_relation(feature_relation);
register_parameters();
}

CFeatureBlockLogisticRegression::~CFeatureBlockLogisticRegression()
{
SG_UNREF(m_feature_relation);
}

void CFeatureBlockLogisticRegression::register_parameters()
{
SG_ADD((CSGObject**)&m_feature_relation, "feature_relation", "feature relation", MS_NOT_AVAILABLE);
}

CIndexBlockRelation* CFeatureBlockLogisticRegression::get_feature_relation() const
{
SG_REF(m_feature_relation);
return m_feature_relation;
}

void CFeatureBlockLogisticRegression::set_feature_relation(CIndexBlockRelation* feature_relation)
{
SG_UNREF(m_feature_relation);
SG_REF(feature_relation);
m_feature_relation = feature_relation;
}

bool CFeatureBlockLogisticRegression::train_machine(CFeatures* data)
{
if (data && (CDotFeatures*)data)
set_features((CDotFeatures*)data);

ASSERT(features);
ASSERT(m_labels);

SGVector<float64_t> y = ((CBinaryLabels*)m_labels)->get_labels();

slep_options options = slep_options::default_options();
options.q = m_q;
options.regularization = m_regularization;
options.termination = m_termination;
options.tolerance = m_tolerance;
options.max_iter = m_max_iter;

EIndexBlockRelationType relation_type = m_feature_relation->get_relation_type();
switch (relation_type)
{
case GROUP:
{
CIndexBlockGroup* feature_group = (CIndexBlockGroup*)m_feature_relation;
SGVector<index_t> ind = feature_group->get_SLEP_ind();
options.ind = ind.vector;
options.n_feature_blocks = ind.vlen-1;
if (ind[ind.vlen-1] > features->get_num_vectors())
SG_ERROR("Group of features covers more vectors than available\n");

options.mode = FEATURE_GROUP;
slep_result_t result = slep_logistic(features, y.vector, m_z, options);

int32_t n_feats = features->get_dim_feature_space();
SGVector<float64_t> new_w(n_feats);
for (int i=0; i<n_feats; i++)
new_w[i] = result.w[i];

set_bias(result.c[0]);
}
break;
case TREE:
{
CIndexBlockTree* feature_tree = (CIndexBlockTree*)m_feature_relation;

CIndexBlock* root_block = feature_tree->get_root_block();
if (root_block->get_max_index() > features->get_num_vectors())
SG_ERROR("Root block covers more vectors than available\n");
SG_UNREF(root_block);

SGVector<index_t> ind = feature_tree->get_SLEP_ind();
SGVector<float64_t> ind_t = feature_tree->get_SLEP_ind_t();
options.ind = ind.vector;
options.ind_t = ind_t.vector;
options.n_feature_blocks = ind.vlen-1;
options.n_nodes = ind_t.vlen / 3;
options.mode = FEATURE_TREE;

slep_result_t result = slep_logistic(features, y.vector, m_z, options);

int32_t n_feats = features->get_dim_feature_space();
SGVector<float64_t> new_w(n_feats);
for (int i=0; i<n_feats; i++)
new_w[i] = result.w[i];

set_bias(result.c[0]);
}
break;
default:
SG_ERROR("Not supported feature relation type\n");
}

return true;
}

}
76 changes: 76 additions & 0 deletions src/shogun/classifier/FeatureBlockLogisticRegression.h
@@ -0,0 +1,76 @@
/*
* 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.
*
* Copyright (C) 2012 Sergey Lisitsyn
*/

#ifndef FEATUREBLOCKLOGISTICREGRESSION_H_
#define FEATUREBLOCKLOGISTICREGRESSION_H_

#include <shogun/lib/config.h>
#include <shogun/lib/IndexBlockRelation.h>
#include <shogun/machine/SLEPMachine.h>

namespace shogun
{
/** @brief */
class CFeatureBlockLogisticRegression : public CSLEPMachine
{

public:
MACHINE_PROBLEM_TYPE(PT_BINARY)

/** default constructor */
CFeatureBlockLogisticRegression();

/** constructor
*
* @param z regularization coefficient
* @param training_data training features
* @param training_labels training labels
* @param task_relation task relation
*/
CFeatureBlockLogisticRegression(
float64_t z, CDotFeatures* training_data,
CBinaryLabels* training_labels, CIndexBlockRelation* task_relation);

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

/** get name */
virtual const char* get_name() const
{
return "FeatureBlockLogisticRegression";
}

/** getter for feature tree
* @return feature tree
*/
CIndexBlockRelation* get_feature_relation() const;

/** setter for feature tree
* @param feature_tree feature tree
*/
void set_feature_relation(CIndexBlockRelation* feature_relation);

protected:

/** train machine */
virtual bool train_machine(CFeatures* data=NULL);

private:

/** register parameters */
void register_parameters();

protected:

/** feature tree */
CIndexBlockRelation* m_feature_relation;

};
}
#endif

0 comments on commit 9c4e1d8

Please sign in to comment.