Skip to content

Commit

Permalink
Merge branch 'slep' of git://github.com/lisitsyn/shogun
Browse files Browse the repository at this point in the history
  • Loading branch information
lisitsyn committed Jun 9, 2012
2 parents 06aee69 + cc95465 commit bac8cc0
Show file tree
Hide file tree
Showing 21 changed files with 1,101 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 @@ -23,7 +23,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 @@ -40,4 +43,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 @@ -12,5 +14,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>
%}
43 changes: 43 additions & 0 deletions src/shogun/lib/IndicesTree.cpp
@@ -0,0 +1,43 @@
/*
* 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;

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

return ind;
}

void CIndicesTree::print_tree() const
{
print_tree_recursive(m_root_node,0);
}

void CIndicesTree::print_tree_recursive(CIndicesTreeNode* node, int32_t level) const
{
for (int32_t i=0; i<level; i++)
SG_PRINT("\t");

SG_PRINT("[ ");
for (int32_t i=0; i<node->node_indices.vlen; i++)
SG_PRINT(" %d ",node->node_indices[i]);
SG_PRINT("] %f \n", node->weight);

for (int32_t i=0; i<node->child_nodes.index(); i++)
print_tree_recursive(node->child_nodes[i],level+1);
}
174 changes: 174 additions & 0 deletions src/shogun/lib/IndicesTree.h
@@ -0,0 +1,174 @@
/*
* 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
{

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

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

~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;

float64_t weight;

v_array<CIndicesTreeNode*> child_nodes;

};
#endif

/** @brief indices tree
*
*/
class CIndicesTree : public CSGObject
{
public:

/** constructor */
CIndicesTree() : CSGObject()
{
SGVector<int32_t> supernode(2);
supernode[0] = -1;
supernode[1] = -1;
m_root_node = new CIndicesTreeNode(supernode,1.0);
m_current_node = m_root_node;
m_last_node = m_root_node;
m_num_nodes = 1;
}

/** destructor */
virtual ~CIndicesTree()
{
delete m_root_node;
}

/** get indices in SLEP format
* @return indices
*/
SGVector<float64_t> get_ind() const;

/** add child
*
* @param indices indices to add to the tree as child
*/
void add_child(SGVector<index_t> indices, float64_t weight)
{
m_num_nodes++;
m_current_node->add_child(new CIndicesTreeNode(indices,weight));
}

/** move to specific child of current node
*
* @param child_index index of child
*/
void go_child(int32_t child_index)
{
m_last_node = m_current_node;
m_current_node = m_current_node->child_nodes[child_index];
}

/** move back
*
*/
void go_back()
{
CIndicesTreeNode* current_node = m_current_node;
m_current_node = m_last_node;
m_last_node = current_node;
}

/** move to root
*
*/
void go_root()
{
m_last_node = m_current_node;
m_current_node = m_root_node;
}

/** get number of nodes
*
*/
inline int32_t get_num_nodes() const
{
return m_num_nodes;
}

/** print tree */
void print_tree() const;

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

protected:

/** print tree recursive */
void print_tree_recursive(CIndicesTreeNode* node, int32_t level) const;

private:

/** root node */
CIndicesTreeNode* m_root_node;

/** current node */
CIndicesTreeNode* m_current_node;

/** last node */
CIndicesTreeNode* m_last_node;

/** number of nodes */
int32_t m_num_nodes;

};
}
#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 bac8cc0

Please sign in to comment.