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 Jul 7, 2012
2 parents 6af57ce + 25dc5ad commit 3717f65
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 10 deletions.
7 changes: 7 additions & 0 deletions src/shogun/classifier/FeatureBlockLogisticRegression.cpp
Expand Up @@ -104,7 +104,14 @@ bool CFeatureBlockLogisticRegression::train_machine(CFeatures* data)
CIndexBlockTree* feature_tree = (CIndexBlockTree*)m_feature_relation;

SGVector<float64_t> ind_t = feature_tree->get_SLEP_ind_t();
SGVector<float64_t> G;
if (feature_tree->is_general())
{
G = feature_tree->get_SLEP_G();
options.general = true;
}
options.ind_t = ind_t.vector;
options.G = G.vector;
options.n_nodes = ind_t.vlen/3;
options.n_feature_blocks = ind_t.vlen/3;
options.mode = FEATURE_TREE;
Expand Down
77 changes: 72 additions & 5 deletions src/shogun/lib/IndexBlockTree.cpp
Expand Up @@ -61,6 +61,15 @@ void print_tree(tree_node_t* node, int tabs)
print_tree(node->desc[i],tabs+1);
}

int32_t fill_G_recursive(tree_node_t* node, vector<int32_t>* G)
{
int32_t c=1;
G->push_back(node->idx);
for (int32_t i=0; i<node->n_desc; i++)
c+= fill_G_recursive(node->desc[i], G);
return c;
}

void fill_ind_recursive(tree_node_t* node, vector<block_tree_node_t>* tree_nodes, int32_t lower)
{
int32_t l = lower;
Expand Down Expand Up @@ -97,20 +106,22 @@ void collect_tree_nodes_recursive(CIndexBlock* subtree_root_block, vector<block_
SG_UNREF(sub_blocks);
}

CIndexBlockTree::CIndexBlockTree() : CIndexBlockRelation(), m_root_block(NULL)
CIndexBlockTree::CIndexBlockTree() :
CIndexBlockRelation(), m_root_block(NULL),
m_general(false)
{

}

CIndexBlockTree::CIndexBlockTree(CIndexBlock* root_block) : CIndexBlockRelation(),
m_root_block(NULL)
m_root_block(NULL), m_general(false)
{
set_root_block(root_block);
}

CIndexBlockTree::CIndexBlockTree(SGMatrix<float64_t> adjacency_matrix) :
CIndexBlockRelation(),
m_root_block(NULL)
m_root_block(NULL), m_general(true)
{
ASSERT(adjacency_matrix.num_rows == adjacency_matrix.num_cols);
int32_t n_features = adjacency_matrix.num_rows;
Expand Down Expand Up @@ -141,6 +152,42 @@ CIndexBlockTree::CIndexBlockTree(SGMatrix<float64_t> adjacency_matrix) :
}
SG_FREE(nz_row);

vector<int32_t> G;
vector<int32_t> ind_t;
int current_l_idx = 1;
for (int32_t i=0; i<n_features; i++)
{
if (nodes[i].n_desc > 0)
{
int sub_count = fill_G_recursive(&nodes[i],&G);
ind_t.push_back(current_l_idx);
ind_t.push_back(current_l_idx+sub_count-1);
ind_t.push_back(1.0);
current_l_idx += sub_count;
}
}
/*
SG_SPRINT("[");
for (int32_t i=0; i<G.size(); i++)
SG_SPRINT(" %d ",G[i]);
SG_SPRINT("]\n");
SG_SPRINT("[");
for (int32_t i=0; i<ind_t.size(); i++)
SG_SPRINT(" %d ",ind_t[i]);
SG_SPRINT("]\n");
*/

m_precomputed_ind_t = SGVector<float64_t>((int32_t)ind_t.size()+3);
m_precomputed_ind_t[0] = -1;
m_precomputed_ind_t[1] = -1;
m_precomputed_ind_t[2] = 1.0;
for (int32_t i=0; i<(int32_t)ind_t.size(); i++)
m_precomputed_ind_t[i+3] = ind_t[i];
m_precomputed_G = SGVector<float64_t>((int32_t)G.size());
for (int32_t i=0; i<(int32_t)G.size(); i++)
m_precomputed_G[i] = G[i] + 1;
m_general = true;
/*
count_sub_nodes_recursive(nodes,1);
print_tree(nodes,0);
int32_t n_leaves = count_sub_nodes_recursive(nodes,0);
Expand All @@ -158,11 +205,20 @@ CIndexBlockTree::CIndexBlockTree(SGMatrix<float64_t> adjacency_matrix) :
m_precomputed_ind_t[3+3*i+1] = blocks[i].t_max_index;
m_precomputed_ind_t[3+3*i+2] = blocks[i].weight;
}
*/
for (int32_t i=0; i<n_features; i++)
SG_FREE(nodes[i].desc);
SG_FREE(nodes);
}

CIndexBlockTree::CIndexBlockTree(SGVector<float64_t> G, SGVector<float64_t> ind_t) :
CIndexBlockRelation(),
m_root_block(NULL), m_general(true)
{
m_precomputed_G = G;
m_precomputed_ind_t = ind_t;
}

CIndexBlockTree::~CIndexBlockTree()
{
SG_UNREF(m_root_block);
Expand All @@ -181,19 +237,30 @@ void CIndexBlockTree::set_root_block(CIndexBlock* root_block)
m_root_block = root_block;
}

SGVector<index_t> CIndexBlockTree::get_SLEP_ind()
SGVector<index_t> CIndexBlockTree::get_SLEP_ind() const
{
SG_SNOTIMPLEMENTED;
return SGVector<index_t>();
}

SGVector<float64_t> CIndexBlockTree::get_SLEP_ind_t()
SGVector<float64_t> CIndexBlockTree::get_SLEP_G() const
{
return m_precomputed_G;
}

bool CIndexBlockTree::is_general() const
{
return m_general;
}

SGVector<float64_t> CIndexBlockTree::get_SLEP_ind_t() const
{
if (m_precomputed_ind_t.vlen)
return m_precomputed_ind_t;

else
{
ASSERT(m_root_block);
CList* blocks = new CList(true);

vector<block_tree_node_t> tree_nodes = vector<block_tree_node_t>();
Expand Down
20 changes: 18 additions & 2 deletions src/shogun/lib/IndexBlockTree.h
Expand Up @@ -32,6 +32,12 @@ class CIndexBlockTree : public CIndexBlockRelation
* @param adjacency_matrix adjacency matrix
*/
CIndexBlockTree(SGMatrix<float64_t> adjacency_matrix);

/** constructor
* @param G custom G
* @param ind_t custom ind_t
*/
CIndexBlockTree(SGVector<float64_t> G, SGVector<float64_t> ind_t);

/** destructor */
virtual ~CIndexBlockTree();
Expand All @@ -45,15 +51,19 @@ class CIndexBlockTree : public CIndexBlockRelation
/** returns information about blocks in
* SLEP "ind" format
*/
virtual SGVector<index_t> get_SLEP_ind();
virtual SGVector<index_t> get_SLEP_ind() const;

virtual SGVector<float64_t> get_SLEP_G() const;

/** returns information about blocks relations
* in SLEP "ind_t" format
*/
virtual SGVector<float64_t> get_SLEP_ind_t();
virtual SGVector<float64_t> get_SLEP_ind_t() const;

virtual EIndexBlockRelationType get_relation_type() const { return TREE; }

bool is_general() const;

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

Expand All @@ -62,8 +72,14 @@ class CIndexBlockTree : public CIndexBlockRelation
/** root block */
CIndexBlock* m_root_block;

/** general */
bool m_general;

/** precomputed ind_t */
SGVector<float64_t> m_precomputed_ind_t;

/** precomputed G */
SGVector<float64_t> m_precomputed_G;
};

}
Expand Down
4 changes: 3 additions & 1 deletion src/shogun/lib/slep/slep_logistic.cpp
Expand Up @@ -252,6 +252,8 @@ double compute_lambda_logistic(
lambda_max = general_findLambdaMax(ATb, n_feats, options.G, options.ind_t, options.n_nodes);
else
lambda_max = findLambdaMax(ATb, n_feats, options.ind_t, options.n_nodes);

SG_FREE(ATb);
}
break;
default:
Expand Down Expand Up @@ -456,7 +458,7 @@ slep_result_t slep_logistic(
{
for (i=0; i<n_feats*n_tasks; i++)
v[i] = s[i] - g[i]*(1.0/L);

for (t=0; t<n_tasks; t++)
c[t] = sc[t] - gc[t]*(1.0/L);

Expand Down
2 changes: 1 addition & 1 deletion src/shogun/lib/slep/tree/altra.cpp
Expand Up @@ -160,7 +160,7 @@ double treeNorm(double *x, int ldx, int n, double *ind, int nodes){

lambda=ind[2];

for(j=0;j<n;j+=ldx){
for(j=0;j<n*ldx;j+=ldx){
tree_norm+=fabs(x[j]);
}

Expand Down
2 changes: 1 addition & 1 deletion src/shogun/lib/slep/tree/general_altra.cpp
Expand Up @@ -35,7 +35,7 @@ void general_altra(double *x, double *v, int n, double *G, double *ind, int node
exit(1);
}

lambda=ind[2];
lambda=mult*ind[2];

for(j=0;j<n;j++){
if (v[j]>lambda)
Expand Down

0 comments on commit 3717f65

Please sign in to comment.