Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Updated IndicesTree
  • Loading branch information
lisitsyn committed Jun 8, 2012
1 parent f209569 commit 92cd89a
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 23 deletions.
22 changes: 20 additions & 2 deletions src/shogun/lib/IndicesTree.cpp
Expand Up @@ -12,14 +12,32 @@

using namespace shogun;

float64_t* CIndicesTree::get_ind() const
SGVector<float64_t> CIndicesTree::get_ind() const
{
SG_WARNING("Not implemented, uses one supernode\n");
float64_t* ind = SG_MALLOC(float64_t, 3);
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);
}
82 changes: 66 additions & 16 deletions src/shogun/lib/IndicesTree.h
Expand Up @@ -19,6 +19,7 @@
namespace shogun
{

#ifndef DOXYGEN_SHOULD_SKIP_THIS
/** @brief indices tree node */
class CIndicesTreeNode
{
Expand All @@ -27,20 +28,21 @@ class CIndicesTreeNode
{
node_indices = SGVector<index_t>();
child_nodes = v_array<CIndicesTreeNode*>();
weight = 0.0;
}

CIndicesTreeNode(SGVector<index_t> indices)
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)
Expand All @@ -55,56 +57,104 @@ class CIndicesTreeNode

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()
{
root_node = new CIndicesTreeNode();
current_node = root_node;
last_node = current_node;
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;
}

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

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

void add_child(SGVector<index_t> indices)
/** add child
*
* @param indices indices to add to the tree as child
*/
void add_child(SGVector<index_t> indices, float64_t weight)
{
current_node->add_child(new CIndicesTreeNode(indices));
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)
{
last_node = current_node;
current_node = current_node->child_nodes[child_index];
m_last_node = m_current_node;
m_current_node = m_current_node->child_nodes[child_index];
}

/** move back
*
*/
void go_back()
{
current_node = last_node;
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;
}

/** 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:

CIndicesTreeNode* root_node;
/** root node */
CIndicesTreeNode* m_root_node;

CIndicesTreeNode* current_node;
/** current node */
CIndicesTreeNode* m_current_node;

CIndicesTreeNode* last_node;
/** last node */
CIndicesTreeNode* m_last_node;

};
}
Expand Down
3 changes: 2 additions & 1 deletion src/shogun/regression/FeatureTreeLeastSquaresRegression.cpp
Expand Up @@ -73,7 +73,8 @@ bool CFeatureTreeLeastSquaresRegression::train_machine(CFeatures* data)
options.restart_num = 10000;
options.n_nodes = 1;
options.regularization = 0;
options.ind = m_feature_tree->get_ind();
SGVector<float64_t> ind = m_feature_tree->get_ind();
options.ind = ind.vector;
options.G = NULL;
options.initial_w = NULL;

Expand Down
24 changes: 21 additions & 3 deletions src/shogun/transfer/multitask/MultitaskLeastSquaresRegression.cpp
Expand Up @@ -4,7 +4,6 @@
* 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
*/

Expand All @@ -18,6 +17,7 @@ namespace shogun
CMultitaskLeastSquaresRegression::CMultitaskLeastSquaresRegression() :
CSLEPMachine(), m_task_tree(NULL)
{
register_parameters();
}

CMultitaskLeastSquaresRegression::CMultitaskLeastSquaresRegression(
Expand All @@ -26,6 +26,7 @@ CMultitaskLeastSquaresRegression::CMultitaskLeastSquaresRegression(
CSLEPMachine(z,train_features,(CLabels*)train_labels), m_task_tree(NULL)
{
set_task_tree(task_tree);
register_parameters();
}

CMultitaskLeastSquaresRegression::~CMultitaskLeastSquaresRegression()
Expand All @@ -38,6 +39,22 @@ void CMultitaskLeastSquaresRegression::register_parameters()
SG_ADD((CSGObject**)&m_task_tree, "feature_tree", "feature tree", MS_NOT_AVAILABLE);
}

int32_t CMultitaskLeastSquaresRegression::get_current_task() const
{
return m_current_task;
}

void CMultitaskLeastSquaresRegression::set_current_task(int32_t task)
{
ASSERT(task>0);
ASSERT(task<m_tasks_w.num_cols);
m_current_task = task;
int32_t n_feats = ((CDotFeatures*)features)->get_dim_feature_space();
w = SGVector<float64_t>(n_feats);
for (int32_t i=0; i<n_feats; i++)
w[i] = m_tasks_w(i,task);
}

CIndicesTree* CMultitaskLeastSquaresRegression::get_task_tree() const
{
SG_REF(m_task_tree);
Expand Down Expand Up @@ -73,11 +90,12 @@ bool CMultitaskLeastSquaresRegression::train_machine(CFeatures* data)
options.restart_num = 10000;
options.n_nodes = 1;
options.regularization = 0;
options.ind = m_task_tree->get_ind();
SGVector<float64_t> ind = m_task_tree->get_ind();
options.ind = ind.vector;
options.G = NULL;
options.initial_w = NULL;

//w = slep_tree_mt_lsr(features,y,m_z,options);
m_tasks_w = slep_tree_mt_lsr(features,y,m_z,options);

SG_FREE(y);

Expand Down
24 changes: 23 additions & 1 deletion src/shogun/transfer/multitask/MultitaskLeastSquaresRegression.h
Expand Up @@ -4,7 +4,6 @@
* 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
*/

Expand Down Expand Up @@ -47,7 +46,24 @@ class CMultitaskLeastSquaresRegression : public CSLEPMachine
return "MultitaskLeastSquaresRegression";
}

/** getter for current task
* @return current task index
*/
int32_t get_current_task() const;

/** setter for current task
* @param task task index
*/
void set_current_task(int32_t task);

/** getter for task tree
* @return task tree
*/
CIndicesTree* get_task_tree() const;

/** setter for task tree
* @param task_tree task tree
*/
void set_task_tree(CIndicesTree* task_tree);

protected:
Expand All @@ -62,9 +78,15 @@ class CMultitaskLeastSquaresRegression : public CSLEPMachine

protected:

/** current task index */
int32_t m_current_task;

/** feature tree */
CIndicesTree* m_task_tree;

/** tasks w's */
SGMatrix<float64_t> m_tasks_w;

};
}
#endif /* ----- #ifndef MULTITASKLEASTSQUARESREGRESSION_H_ ----- */

0 comments on commit 92cd89a

Please sign in to comment.