Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added FeatureBlockLogisticRegression
- Loading branch information
Showing
4 changed files
with
214 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
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
134 changes: 134 additions & 0 deletions
134
src/shogun/classifier/FeatureBlockLogisticRegression.cpp
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,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; | ||
} | ||
|
||
} |
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,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 |