Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Removed SLEP machine and redundant L1LogisticRegression, rebased Feat…
…ureBlockLogisticRegression and fixed missed weight initialization
  • Loading branch information
lisitsyn committed Aug 13, 2012
1 parent 97b8217 commit df80e5c
Show file tree
Hide file tree
Showing 15 changed files with 234 additions and 378 deletions.
1 change: 1 addition & 0 deletions examples/undocumented/libshogun/Makefile
Expand Up @@ -92,6 +92,7 @@ TARGETS = basic_minimal \
statistics_linear_time_mmd \
statistics_linear_time_mmd_kernel_choice \
statistics_hsic \
classifier_featureblocklogisticregression \

all: $(TARGETS)

Expand Down
@@ -0,0 +1,60 @@
#include <shogun/labels/RegressionLabels.h>
#include <shogun/features/DenseFeatures.h>
#include <shogun/classifier/FeatureBlockLogisticRegression.h>
#include <shogun/lib/IndexBlock.h>
#include <shogun/lib/IndexBlockTree.h>
#include <shogun/lib/IndexBlockGroup.h>
#include <shogun/base/init.h>
#include <shogun/lib/common.h>
#include <shogun/io/SGIO.h>

using namespace shogun;

void print_message(FILE* target, const char* str)
{
fprintf(target, "%s", str);
}

int main(int argc, char** argv)
{
init_shogun_with_defaults();

// create some data
SGMatrix<float64_t> matrix(4,4);
for (int32_t i=0; i<4*4; i++)
matrix.matrix[i]=i;

CDenseFeatures<float64_t>* features= new CDenseFeatures<float64_t>(matrix);

// create three labels
CBinaryLabels* labels=new CBinaryLabels(4);
labels->set_label(0, -1);
labels->set_label(1, +1);
labels->set_label(2, -1);
labels->set_label(3, +1);

CIndexBlock* first_block = new CIndexBlock(0,2);
CIndexBlock* second_block = new CIndexBlock(2,4);
CIndexBlockGroup* block_group = new CIndexBlockGroup();
block_group->add_block(first_block);
block_group->add_block(second_block);

CFeatureBlockLogisticRegression* regressor = new CFeatureBlockLogisticRegression(0.5,features,labels,block_group);
regressor->train();

regressor->get_w().display_vector();

CIndexBlock* root_block = new CIndexBlock(0,4);
root_block->add_sub_block(first_block);
root_block->add_sub_block(second_block);
CIndexBlockTree* block_tree = new CIndexBlockTree(root_block);

regressor->set_feature_relation(block_tree);
regressor->train();

regressor->get_w().display_vector();

SG_UNREF(regressor);
exit_shogun();
return 0;
}
@@ -0,0 +1,36 @@
from numpy import array,hstack
from numpy.random import seed, rand
from tools.load import LoadMatrix
lm=LoadMatrix()

traindat = lm.load_numbers('../data/fm_train_real.dat')
testdat = lm.load_numbers('../data/fm_test_real.dat')
label_traindat = lm.load_labels('../data/label_train_twoclass.dat')

parameter_list = [[traindat,testdat,label_traindat]]

def classifier_featureblock_logistic_regression(fm_train=traindat,fm_test=testdat,label_train=label_traindat):

from modshogun import BinaryLabels, RealFeatures, IndexBlock, IndexBlockGroup, FeatureBlockLogisticRegression

features = RealFeatures(hstack((traindat,traindat)))
labels = BinaryLabels(hstack((label_train,label_train)))

n_features = features.get_num_features()
block_one = IndexBlock(0,n_features/2)
block_two = IndexBlock(n_features/2,n_features)
block_group = IndexBlockGroup()
block_group.add_block(block_one)
block_group.add_block(block_two)

mtlr = FeatureBlockLogisticRegression(0.1,features,labels,block_group)
mtlr.set_regularization(1) # use regularization ratio
mtlr.set_tolerance(1e-2) # use 1e-2 tolerance
mtlr.train()
out = mtlr.apply().get_labels()

return out

if __name__=='__main__':
print('FeatureBlockLogisticRegression')
classifier_featureblock_logistic_regression(*parameter_list[0])
3 changes: 0 additions & 3 deletions src/interfaces/modular/Classifier.i
Expand Up @@ -63,7 +63,6 @@
%rename(SVMLightOneClass) CSVMLightOneClass;
#endif //USE_SVMLIGHT
%rename(FeatureBlockLogisticRegression) CFeatureBlockLogisticRegression;
%rename(L1LogisticRegression) CL1LogisticRegression;
%rename(DirectorLinearMachine) CDirectorLinearMachine;
%rename(DirectorKernelMachine) CDirectorKernelMachine;

Expand Down Expand Up @@ -107,9 +106,7 @@
%include <shogun/classifier/mkl/MKLOneClass.h>
%include <shogun/classifier/vw/VowpalWabbit.h>
%include <shogun/classifier/svm/NewtonSVM.h>
%include <shogun/machine/SLEPMachine.h>
%include <shogun/classifier/FeatureBlockLogisticRegression.h>
%include <shogun/classifier/L1LogisticRegression.h>
%include <shogun/machine/DirectorLinearMachine.h>
%include <shogun/machine/DirectorKernelMachine.h>

Expand Down
1 change: 0 additions & 1 deletion src/interfaces/modular/Classifier_includes.i
Expand Up @@ -38,7 +38,6 @@
#endif //USE_SVMLIGHT

#include <shogun/classifier/FeatureBlockLogisticRegression.h>
#include <shogun/classifier/L1LogisticRegression.h>
#include <shogun/machine/DirectorLinearMachine.h>
#include <shogun/machine/DirectorKernelMachine.h>
%}
1 change: 0 additions & 1 deletion src/interfaces/modular/Regression.i
Expand Up @@ -43,7 +43,6 @@
%include <shogun/regression/svr/LibLinearRegression.h>
%include <shogun/classifier/mkl/MKL.h>
%include <shogun/regression/svr/MKLRegression.h>
%include <shogun/machine/SLEPMachine.h>

#ifdef USE_SVMLIGHT
%include <shogun/regression/svr/SVRLight.h>
Expand Down
1 change: 0 additions & 1 deletion src/interfaces/modular/Regression_includes.i
Expand Up @@ -19,7 +19,6 @@
#include <shogun/regression/svr/LibLinearRegression.h>
#include <shogun/classifier/mkl/MKL.h>
#include <shogun/regression/svr/MKLRegression.h>
#include <shogun/machine/SLEPMachine.h>
#ifdef USE_SVMLIGHT
#include <shogun/classifier/svm/SVMLight.h>
#include <shogun/regression/svr/SVRLight.h>
Expand Down
93 changes: 88 additions & 5 deletions src/shogun/classifier/FeatureBlockLogisticRegression.cpp
Expand Up @@ -18,19 +18,27 @@ namespace shogun
{

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

CFeatureBlockLogisticRegression::CFeatureBlockLogisticRegression(
float64_t z, CDotFeatures* train_features,
CBinaryLabels* train_labels, CIndexBlockRelation* feature_relation) :
CSLEPMachine(z,train_features,(CLabels*)train_labels),
CLinearMachine(),
m_feature_relation(NULL)
{
set_feature_relation(feature_relation);
set_z(z);
set_q(2.0);
set_features(train_features);
set_labels(train_labels);
set_termination(0);
set_regularization(0);
set_tolerance(1e-3);
set_max_iter(1000);
register_parameters();
}

Expand All @@ -42,6 +50,12 @@ CFeatureBlockLogisticRegression::~CFeatureBlockLogisticRegression()
void CFeatureBlockLogisticRegression::register_parameters()
{
SG_ADD((CSGObject**)&m_feature_relation, "feature_relation", "feature relation", MS_NOT_AVAILABLE);
SG_ADD(&m_z, "z", "regularization coefficient", MS_AVAILABLE);
SG_ADD(&m_q, "q", "q of L1/Lq", MS_AVAILABLE);
SG_ADD(&m_termination, "termination", "termination", MS_NOT_AVAILABLE);
SG_ADD(&m_regularization, "regularization", "regularization", MS_NOT_AVAILABLE);
SG_ADD(&m_tolerance, "tolerance", "tolerance", MS_NOT_AVAILABLE);
SG_ADD(&m_max_iter, "max_iter", "maximum number of iterations", MS_NOT_AVAILABLE);
}

CIndexBlockRelation* CFeatureBlockLogisticRegression::get_feature_relation() const
Expand All @@ -57,6 +71,70 @@ void CFeatureBlockLogisticRegression::set_feature_relation(CIndexBlockRelation*
m_feature_relation = feature_relation;
}

int32_t CFeatureBlockLogisticRegression::get_max_iter() const
{
return m_max_iter;
}

int32_t CFeatureBlockLogisticRegression::get_regularization() const
{
return m_regularization;
}

int32_t CFeatureBlockLogisticRegression::get_termination() const
{
return m_termination;
}

float64_t CFeatureBlockLogisticRegression::get_tolerance() const
{
return m_tolerance;
}

float64_t CFeatureBlockLogisticRegression::get_z() const
{
return m_z;
}

float64_t CFeatureBlockLogisticRegression::get_q() const
{
return m_q;
}

void CFeatureBlockLogisticRegression::set_max_iter(int32_t max_iter)
{
ASSERT(max_iter>=0);
m_max_iter = max_iter;
}

void CFeatureBlockLogisticRegression::set_regularization(int32_t regularization)
{
ASSERT(regularization==0 || regularization==1);
m_regularization = regularization;
}

void CFeatureBlockLogisticRegression::set_termination(int32_t termination)
{
ASSERT(termination>=0 && termination<=4);
m_termination = termination;
}

void CFeatureBlockLogisticRegression::set_tolerance(float64_t tolerance)
{
ASSERT(tolerance>0.0);
m_tolerance = tolerance;
}

void CFeatureBlockLogisticRegression::set_z(float64_t z)
{
m_z = z;
}

void CFeatureBlockLogisticRegression::set_q(float64_t q)
{
m_q = q;
}

bool CFeatureBlockLogisticRegression::train_machine(CFeatures* data)
{
if (data && (CDotFeatures*)data)
Expand Down Expand Up @@ -89,11 +167,16 @@ bool CFeatureBlockLogisticRegression::train_machine(CFeatures* data)
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.gWeight = SG_MALLOC(double, options.n_feature_blocks);
for (int32_t i=0; i<options.n_feature_blocks; i++)
options.gWeight[i] = 1.0;
options.mode = FEATURE_GROUP;
options.loss = LOGISTIC;
options.n_nodes = 0;
slep_result_t result = slep_solver(features, y.vector, m_z, options);

SG_FREE(options.gWeight);
int32_t n_feats = features->get_dim_feature_space();
SGVector<float64_t> new_w(n_feats);
for (int i=0; i<n_feats; i++)
Expand Down Expand Up @@ -166,7 +249,7 @@ SGVector<float64_t> CFeatureBlockLogisticRegression::apply_get_outputs(CFeatures
float64_t* out=SG_MALLOC(float64_t, num);
features->dense_dot_range(out, 0, num, NULL, w.vector, w.vlen, bias);
for (int32_t i=0; i<num; i++)
out[i] = 2.0/(1.0+CMath::exp(-out[i])) - 1.0;//*CMath::exp(-CMath::sign(out[i])*out[i]);
out[i] = 2.0/(1.0+CMath::exp(-out[i])) - 1.0;
return SGVector<float64_t>(out,num);
}

Expand Down
48 changes: 46 additions & 2 deletions src/shogun/classifier/FeatureBlockLogisticRegression.h
Expand Up @@ -12,12 +12,12 @@

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

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

public:
Expand Down Expand Up @@ -57,6 +57,32 @@ class CFeatureBlockLogisticRegression : public CSLEPMachine
void set_feature_relation(CIndexBlockRelation* feature_relation);

virtual float64_t apply_one(int32_t vec_idx);

/** get max iter */
int32_t get_max_iter() const;
/** get q */
float64_t get_q() const;
/** get regularization */
int32_t get_regularization() const;
/** get termination */
int32_t get_termination() const;
/** get tolerance */
float64_t get_tolerance() const;
/** get z */
float64_t get_z() const;

/** set max iter */
void set_max_iter(int32_t max_iter);
/** set q */
void set_q(float64_t q);
/** set regularization */
void set_regularization(int32_t regularization);
/** set termination */
void set_termination(int32_t termination);
/** set tolerance */
void set_tolerance(float64_t tolerance);
/** set z */
void set_z(float64_t z);

protected:

Expand All @@ -74,6 +100,24 @@ class CFeatureBlockLogisticRegression : public CSLEPMachine

/** feature tree */
CIndexBlockRelation* m_feature_relation;

/** regularization type */
int32_t m_regularization;

/** termination criteria */
int32_t m_termination;

/** max iteration */
int32_t m_max_iter;

/** tolerance */
float64_t m_tolerance;

/** q of L1/Lq */
float64_t m_q;

/** regularization coefficient */
float64_t m_z;

};
}
Expand Down

0 comments on commit df80e5c

Please sign in to comment.