Skip to content

Commit

Permalink
Added SLEP machines
Browse files Browse the repository at this point in the history
  • Loading branch information
lisitsyn committed Jun 8, 2012
1 parent 25d7549 commit f209569
Show file tree
Hide file tree
Showing 21 changed files with 987 additions and 39 deletions.
6 changes: 5 additions & 1 deletion src/interfaces/modular/Regression.i
Expand Up @@ -19,6 +19,8 @@
%rename(LibLinearRegression) CLibLinearRegression;
%rename(MKL) CMKL;
%rename(MKLRegression) CMKLRegression;
%rename(IndicesTree) CIndicesTree;
%rename(FeatureTreeLeastSquaresRegression) CFeatureTreeLeastSquaresRegression;
#ifdef USE_SVMLIGHT
%rename(SVRLight) CSVRLight;
#endif //USE_SVMLIGHT
Expand All @@ -34,7 +36,9 @@
%include <shogun/regression/svr/LibLinearRegression.h>
%include <shogun/classifier/mkl/MKL.h>
%include <shogun/regression/svr/MKLRegression.h>

%include <shogun/lib/IndicesTree.h>
%include <shogun/machine/SLEPMachine.h>
%include <shogun/regression/FeatureTreeLeastSquaresRegression.h>

#ifdef USE_SVMLIGHT
%include <shogun/regression/svr/SVRLight.h>
Expand Down
3 changes: 3 additions & 0 deletions src/interfaces/modular/Regression_includes.i
Expand Up @@ -13,6 +13,9 @@
#include <shogun/regression/svr/LibLinearRegression.h>
#include <shogun/classifier/mkl/MKL.h>
#include <shogun/regression/svr/MKLRegression.h>
#include <shogun/lib/IndicesTree.h>
#include <shogun/machine/SLEPMachine.h>
#include <shogun/regression/FeatureTreeLeastSquaresRegression.h>
#ifdef USE_SVMLIGHT
#include <shogun/classifier/svm/SVMLight.h>
#include <shogun/regression/svr/SVRLight.h>
Expand Down
6 changes: 4 additions & 2 deletions src/interfaces/modular/Transfer.i
Expand Up @@ -21,7 +21,10 @@
%rename(DomainAdaptationSVM) CDomainAdaptationSVM;
#endif //USE_SVMLIGHT
%rename(DomainAdaptationSVMLinear) CDomainAdaptationSVMLinear;
%rename(DomainAdaptationMulticlassLibLinear) CDomainAdaptationMulticlassLibLinear;


/* Includes */
%include <shogun/lib/IndicesTree.h>

/* Multitask includes */
%include <shogun/transfer/multitask/MultitaskKernelNormalizer.h>
Expand All @@ -36,4 +39,3 @@
%include <shogun/transfer/domain_adaptation/DomainAdaptationSVM.h>
#endif // USE_SVMLIGHT
%include <shogun/transfer/domain_adaptation/DomainAdaptationSVMLinear.h>
%include <shogun/transfer/domain_adaptation/DomainAdaptationMulticlassLibLinear.h>
3 changes: 2 additions & 1 deletion src/interfaces/modular/Transfer_includes.i
@@ -1,4 +1,6 @@
%{
#include <shogun/lib/IndicesTree.h>

#include <shogun/transfer/multitask/MultitaskKernelNormalizer.h>
#include <shogun/transfer/multitask/MultitaskKernelMklNormalizer.h>
#include <shogun/transfer/multitask/MultitaskKernelTreeNormalizer.h>
Expand All @@ -10,5 +12,4 @@
#include <shogun/transfer/domain_adaptation/DomainAdaptationSVM.h>
#endif /* USE_SVMLIGHT */
#include <shogun/transfer/domain_adaptation/DomainAdaptationSVMLinear.h>
#include <shogun/transfer/domain_adaptation/DomainAdaptationMulticlassLibLinear.h>
%}
25 changes: 25 additions & 0 deletions src/shogun/lib/IndicesTree.cpp
@@ -0,0 +1,25 @@
/*
* 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 Sergey Lisitsyn
* Copyright (C) 2012 Sergey Lisitsyn
*/

#include <shogun/lib/IndicesTree.h>

using namespace shogun;

float64_t* CIndicesTree::get_ind() const
{
SG_WARNING("Not implemented, uses one supernode\n");
float64_t* ind = SG_MALLOC(float64_t, 3);
ind[0] = -1;
ind[1] = -1;
ind[2] = 1.0;

return ind;
}

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

#ifndef INDICES_TREE_H_
#define INDICES_TREE_H_

#include <shogun/base/SGObject.h>

#include <shogun/lib/SGVector.h>
#include <shogun/lib/v_array.h>

namespace shogun
{

/** @brief indices tree node */
class CIndicesTreeNode
{
public:
CIndicesTreeNode()
{
node_indices = SGVector<index_t>();
child_nodes = v_array<CIndicesTreeNode*>();
}

CIndicesTreeNode(SGVector<index_t> indices)
{
node_indices = indices;
child_nodes = v_array<CIndicesTreeNode*>();
}

~CIndicesTreeNode()
{
int32_t len_child_nodes = child_nodes.index();
for (int32_t i; i<len_child_nodes; i++)
delete child_nodes[i];

}

void add_child(CIndicesTreeNode* child)
{
child_nodes.push(child);
}

void clear_childs()
{
child_nodes.erase();
}

SGVector<index_t> node_indices;

v_array<CIndicesTreeNode*> child_nodes;

};


class CIndicesTree : public CSGObject
{
public:
CIndicesTree() : CSGObject()
{
root_node = new CIndicesTreeNode();
current_node = root_node;
last_node = current_node;
}

virtual ~CIndicesTree()
{
delete root_node;
}

float64_t* get_ind() const;

void add_child(SGVector<index_t> indices)
{
current_node->add_child(new CIndicesTreeNode(indices));
}

void go_child(int32_t child_index)
{
last_node = current_node;
current_node = current_node->child_nodes[child_index];
}

void go_back()
{
current_node = last_node;
}

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

private:

CIndicesTreeNode* root_node;

CIndicesTreeNode* current_node;

CIndicesTreeNode* last_node;

};
}
#endif /* ----- #ifndef INDICES_TREE_H_ ----- */
4 changes: 2 additions & 2 deletions src/shogun/lib/slep/slep_options.h
Expand Up @@ -31,9 +31,9 @@ IGNORE_IN_CLASSLIST struct slep_options
double* initial_w;

static bool get_default_general() { return false; }
static int get_default_termination() { return 1; }
static int get_default_termination() { return 2; }
static double get_default_tolerance() { return 1e-3; }
static int get_default_max_iter() { return 100; }
static int get_default_max_iter() { return 1000; }
static int get_default_restart_num() { return 100; }
static int get_default_regularization() { return 0; }
};
Expand Down
23 changes: 8 additions & 15 deletions src/shogun/lib/slep/slep_tree_lsr.cpp
Expand Up @@ -16,7 +16,7 @@
namespace shogun
{

double* slep_tree_lsr(
SGVector<double> slep_tree_lsr(
CDotFeatures* features,
double* y,
double z,
Expand Down Expand Up @@ -48,7 +48,7 @@ double* slep_tree_lsr(
else
lambda = z;

double* w = SG_CALLOC(double, n_feats);
SGVector<double> w(n_feats);
if (options.initial_w)
{
for (i=0; i<n_feats; i++)
Expand All @@ -65,7 +65,7 @@ double* slep_tree_lsr(
double* v = SG_CALLOC(double, n_feats);

double* Aw = SG_CALLOC(double, n_vecs);
features->dense_dot_range(Aw,0,n_vecs,NULL,w,n_feats,0.0);
features->dense_dot_range(Aw,0,n_vecs,NULL,w.vector,n_feats,0.0);
double* Av = SG_MALLOC(double, n_vecs);
double* As = SG_MALLOC(double, n_vecs);
double* ATAs = SG_MALLOC(double, n_feats);
Expand All @@ -86,14 +86,11 @@ double* slep_tree_lsr(

while (!done && iter < options.max_iter)
{
//CMath::display_vector(w,n_feats,"w");

beta = (alphap-1.0)/alpha;

for (i=0; i<n_feats; i++)
s[i] = w[i] + beta*wwp[i];


for (i=0; i<n_vecs; i++)
As[i] = Aw[i] + beta*(Aw[i]-Awp[i]);

Expand All @@ -120,18 +117,15 @@ double* slep_tree_lsr(
v[i] = s[i] - g[i]*(1.0/L);

if (options.general)
general_altra(w, v, n_feats, options.G, options.ind, options.n_nodes, lambda/L);
general_altra(w.vector, v, n_feats, options.G, options.ind, options.n_nodes, lambda/L);
else
altra(w, v, n_feats, options.ind, options.n_nodes, lambda/L);

//CMath::display_vector(w,n_feats,"w_inner");
//SG_SPRINT("\n");
altra(w.vector, v, n_feats, options.ind, options.n_nodes, lambda/L);

// v = x - s
for (i=0; i<n_feats; i++)
v[i] = w[i] - s[i];

features->dense_dot_range(Aw,0,n_vecs,NULL,w,n_feats,0.0);
features->dense_dot_range(Aw,0,n_vecs,NULL,w.vector,n_feats,0.0);

for (i=0; i<n_vecs; i++)
Av[i] = Aw[i] - As[i];
Expand All @@ -150,7 +144,6 @@ double* slep_tree_lsr(
else
L = CMath::max(2*L, l_sum/r_sum);

SG_SPRINT("L=%.3f\n",L);
}

alphap = alpha;
Expand All @@ -165,10 +158,10 @@ double* slep_tree_lsr(

double tree_norm;
if (options.general)
tree_norm = general_treeNorm(w,n_feats,options.G,
tree_norm = general_treeNorm(w.vector,n_feats,options.G,
options.ind,options.n_nodes);
else
tree_norm = treeNorm(w,n_feats,options.ind,options.n_nodes);
tree_norm = treeNorm(w.vector,n_feats,options.ind,options.n_nodes);

funcp = func;
func = 0.5*SGVector<float64_t>::dot(resid,resid,n_vecs) + lambda*tree_norm;
Expand Down
2 changes: 1 addition & 1 deletion src/shogun/lib/slep/slep_tree_lsr.h
Expand Up @@ -17,7 +17,7 @@
namespace shogun
{

double* slep_tree_lsr(
SGVector<double> slep_tree_lsr(
CDotFeatures* features,
double* y,
double z,
Expand Down

0 comments on commit f209569

Please sign in to comment.