11 #ifndef _MULTITASKKERNELTREENORMALIZER_H___ 12 #define _MULTITASKKERNELTREENORMALIZER_H___ 45 for (
size_t i = 0; i <
children.size(); i++)
54 std::set<CNode*> nodes_on_path = std::set<CNode*>();
56 while (node != NULL) {
57 nodes_on_path.insert(node);
69 std::vector<int32_t> task_ids;
70 std::deque<CNode*> grey_nodes;
71 grey_nodes.push_back(
this);
73 while(grey_nodes.size() > 0)
76 CNode *current_node = grey_nodes.front();
77 grey_nodes.pop_front();
79 for(int32_t i = 0; i!=int32_t(current_node->
children.size()); i++){
80 grey_nodes.push_back(current_node->
children[i]);
84 task_ids.push_back(current_node->
getNode_id());
156 nodes.push_back(root);
158 name2id = std::map<std::string, int32_t>();
164 for (
size_t i = 0; i < nodes.size(); i++)
168 task_histogram.clear();
176 return nodes[task_id];
184 nodes[0]->beta =
beta;
194 if (child_name==
"")
SG_ERROR(
"child_name empty")
195 if (parent_name==
"")
SG_ERROR(
"parent_name empty")
202 nodes.push_back(child_node);
203 int32_t
id = nodes.size()-1;
205 name2id[child_name] = id;
223 return name2id[name];
237 std::set<CNode*> intersection;
239 std::set_intersection(root_path_lhs.begin(), root_path_lhs.end(),
240 root_path_rhs.begin(), root_path_rhs.end(),
241 std::inserter(intersection, intersection.end()));
255 CNode* node_lhs = get_node(task_lhs);
256 CNode* node_rhs = get_node(task_rhs);
259 std::set<CNode*> intersection = intersect_root_path(node_lhs, node_rhs);
263 for (std::set<CNode*>::const_iterator p = intersection.begin(); p != intersection.end(); ++p) {
278 task_histogram.clear();
282 for (std::vector<int32_t>::const_iterator it=task_vector_lhs.begin(); it!=task_vector_lhs.end(); it++)
284 task_histogram[*it] = 0.0;
288 for (std::vector<int32_t>::const_iterator it=task_vector_lhs.begin(); it!=task_vector_lhs.end(); it++)
290 task_histogram[*it] += 1.0;
294 for (std::map<int32_t, float64_t>::const_iterator it=task_histogram.begin(); it!=task_histogram.end(); it++)
296 task_histogram[it->first] = task_histogram[it->first] /
float64_t(task_vector_lhs.size());
304 return (int32_t)(nodes.size());
310 int32_t num_leaves = 0;
312 for (int32_t i=0; i!=get_num_nodes(); i++)
314 if (get_node(i)->is_leaf()==
true)
358 return name2id[name];
396 std::vector<std::string> task_rhs,
401 set_task_vector_lhs(task_lhs);
402 set_task_vector_rhs(task_rhs);
404 num_nodes = taxonomy.get_num_nodes();
406 dependency_matrix = std::vector<float64_t>(num_nodes * num_nodes);
423 for (int32_t i=0; i!=num_nodes; i++)
425 for (int32_t j=0; j!=num_nodes; j++)
428 float64_t similarity = taxonomy.compute_node_similarity(i, j);
429 set_node_similarity(i,j,similarity);
446 int32_t task_idx_lhs = task_vector_lhs[idx_lhs];
447 int32_t task_idx_rhs = task_vector_rhs[idx_rhs];
450 float64_t task_similarity = get_node_similarity(task_idx_lhs, task_idx_rhs);
466 SG_ERROR(
"normalize_lhs not implemented")
476 SG_ERROR(
"normalize_rhs not implemented")
485 task_vector_lhs.clear();
487 for (int32_t i = 0; i != (int32_t)(vec.size()); ++i)
489 task_vector_lhs.push_back(taxonomy.get_id(vec[i]));
493 taxonomy.update_task_histogram(task_vector_lhs);
501 task_vector_rhs.clear();
503 for (int32_t i = 0; i != (int32_t)(vec.size()); ++i)
505 task_vector_rhs.push_back(taxonomy.get_id(vec[i]));
513 set_task_vector_lhs(vec);
514 set_task_vector_rhs(vec);
521 return taxonomy.get_num_nodes();
531 return taxonomy.get_node_weight(idx);
541 taxonomy.set_node_weight(idx, weight);
556 ASSERT(node_lhs < num_nodes && node_lhs >= 0)
557 ASSERT(node_rhs < num_nodes && node_rhs >= 0)
559 return dependency_matrix[node_lhs * num_nodes + node_rhs];
572 ASSERT(node_lhs < num_nodes && node_lhs >= 0)
573 ASSERT(node_rhs < num_nodes && node_rhs >= 0)
575 dependency_matrix[node_lhs * num_nodes + node_rhs] = similarity;
582 return "MultitaskKernelTreeNormalizer";
void add_child(CNode *node)
std::vector< int32_t > task_vector_lhs
void set_task_vector_rhs(std::vector< std::string > vec)
CTaxonomy is used to describe hierarchical structure between tasks.
std::map< std::string, int32_t > get_name2id()
int32_t get_id(std::string name)
virtual float64_t normalize_rhs(float64_t value, int32_t idx_rhs)
void set_root_beta(float64_t beta)
virtual const char * get_name() const
std::map< std::string, int32_t > name2id
int32_t getNode_id() const
CMultitaskKernelTreeNormalizer(std::vector< std::string > task_lhs, std::vector< std::string > task_rhs, CTaxonomy tax)
virtual const char * get_name() const
std::map< int32_t, float64_t > task_histogram
A CNode is an element of a CTaxonomy, which is used to describe hierarchical structure between tasks...
void set_node_similarity(int32_t node_lhs, int32_t node_rhs, float64_t similarity)
void setNode_id(int32_t node_idx)
std::set< CNode * > get_path_root()
std::set< CNode * > intersect_root_path(CNode *node_lhs, CNode *node_rhs)
CMultitaskKernelTreeNormalizer()
float64_t get_beta(int32_t idx)
Class SGObject is the base class of all shogun objects.
void set_task_vector_lhs(std::vector< std::string > vec)
std::vector< CNode * > children
virtual float64_t normalize_lhs(float64_t value, int32_t idx_lhs)
int32_t get_id_by_name(std::string name)
float64_t get_node_weight(int32_t idx)
CNode * add_node(std::string parent_name, std::string child_name, float64_t beta)
The class Kernel Normalizer defines a function to post-process kernel values.
CMultitaskKernelTreeNormalizer * KernelNormalizerToMultitaskKernelTreeNormalizer(CKernelNormalizer *n)
virtual ~CMultitaskKernelTreeNormalizer()
float64_t compute_node_similarity(int32_t task_lhs, int32_t task_rhs)
void set_beta(int32_t idx, float64_t weight)
Base-class for parameterized Kernel Normalizers.
CNode * get_node(int32_t task_id)
void update_task_histogram(std::vector< int32_t > task_vector_lhs)
all of classes and functions are contained in the shogun namespace
void scale(Matrix A, Matrix B, typename Matrix::Scalar alpha)
void set_node_weight(int32_t idx, float64_t weight)
void set_task_vector(std::vector< std::string > vec)
std::vector< float64_t > dependency_matrix
virtual float64_t normalize(float64_t value, int32_t idx_lhs, int32_t idx_rhs)
float64_t get_node_similarity(int32_t node_lhs, int32_t node_rhs)
std::vector< int32_t > task_vector_rhs
virtual const char * get_name() const
std::vector< int32_t > get_task_ids_below()
std::vector< CNode * > nodes
The MultitaskKernel allows Multitask Learning via a modified kernel function based on taxonomy...