Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Added num of nodes counting in indices tree
  • Loading branch information
lisitsyn committed Jun 9, 2012
1 parent 92cd89a commit cc95465
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 16 deletions.
13 changes: 13 additions & 0 deletions src/shogun/lib/IndicesTree.h
Expand Up @@ -80,6 +80,7 @@ class CIndicesTree : public CSGObject
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 */
Expand All @@ -99,6 +100,7 @@ class CIndicesTree : public CSGObject
*/
void add_child(SGVector<index_t> indices, float64_t weight)
{
m_num_nodes++;
m_current_node->add_child(new CIndicesTreeNode(indices,weight));
}

Expand Down Expand Up @@ -131,6 +133,14 @@ class CIndicesTree : public CSGObject
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;

Expand All @@ -156,6 +166,9 @@ class CIndicesTree : public CSGObject
/** last node */
CIndicesTreeNode* m_last_node;

/** number of nodes */
int32_t m_num_nodes;

};
}
#endif /* ----- #ifndef INDICES_TREE_H_ ----- */
22 changes: 7 additions & 15 deletions src/shogun/lib/slep/slep_tree_mt_lsr.cpp
Expand Up @@ -159,12 +159,10 @@ SGMatrix<double> slep_tree_mt_lsr(
for (i=0; i<n_vecs; i++)
Av[i] = Aw[i] - As[i];

double r_sum = 0.0;
// frobenius norm of r
for (i=0; i<n_feats*n_tasks; i++)
r_sum += v[i]*v[i];
// squared frobenius norm of r
double r_sum = SGVector<float64_t>::dot(v,v,n_feats*n_tasks);

double l_sum = CMath::dot(Av,Av,n_vecs);
double l_sum = SGVector<float64_t>::dot(Av,Av,n_vecs);

if (r_sum <= 1e-20)
{
Expand All @@ -176,8 +174,6 @@ SGMatrix<double> slep_tree_mt_lsr(
break;
else
L = CMath::max(2*L, l_sum/r_sum);

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

alphap = alpha;
Expand All @@ -197,18 +193,14 @@ SGMatrix<double> slep_tree_mt_lsr(
w_row[j] = w(i,j);

if (options.general)
{
tree_norm += general_treeNorm(w_row,n_tasks,options.G,
options.ind,options.n_nodes);
}
else
{
tree_norm += treeNorm(w_row,n_tasks,options.ind,options.n_nodes);
}
}

funcp = func;
func = 0.5*CMath::dot(resid,resid,n_vecs) + lambda*tree_norm;
func = 0.5*SGVector<float64_t>::dot(resid,resid,n_vecs) + lambda*tree_norm;

if (gradient_break)
break;
Expand Down Expand Up @@ -239,13 +231,13 @@ SGMatrix<double> slep_tree_mt_lsr(
done = true;
break;
case 3:
norm_wwp = CMath::sqrt(CMath::dot(wwp,wwp,n_feats));
norm_wwp = CMath::sqrt(SGVector<float64_t>::dot(wwp,wwp,n_feats));
if (norm_wwp <= options.tolerance)
done = true;
break;
case 4:
norm_wp = CMath::sqrt(CMath::dot(wp,wp,n_feats));
norm_wwp = CMath::sqrt(CMath::dot(wwp,wwp,n_feats));
norm_wp = CMath::sqrt(SGVector<float64_t>::dot(wp,wp,n_feats));
norm_wwp = CMath::sqrt(SGVector<float64_t>::dot(wwp,wwp,n_feats));
if (norm_wwp <= options.tolerance*CMath::max(norm_wp,1.0))
done = true;
break;
Expand Down
Expand Up @@ -88,7 +88,7 @@ bool CMultitaskLeastSquaresRegression::train_machine(CFeatures* data)
options.tolerance = m_tolerance;
options.max_iter = m_max_iter;
options.restart_num = 10000;
options.n_nodes = 1;
options.n_nodes = m_task_tree->get_num_nodes();
options.regularization = 0;
SGVector<float64_t> ind = m_task_tree->get_ind();
options.ind = ind.vector;
Expand Down

0 comments on commit cc95465

Please sign in to comment.