11 #ifndef _MULTITASKKERNELTREENORMALIZER_H___ 12 #define _MULTITASKKERNELTREENORMALIZER_H___ 14 #include <shogun/lib/config.h> 46 for (
size_t i = 0; i <
children.size(); i++)
55 std::set<CNode*> nodes_on_path = std::set<CNode*>();
57 while (node != NULL) {
58 nodes_on_path.insert(node);
70 std::vector<int32_t> task_ids;
71 std::deque<CNode*> grey_nodes;
72 grey_nodes.push_back(
this);
74 while(grey_nodes.size() > 0)
77 CNode *current_node = grey_nodes.front();
78 grey_nodes.pop_front();
80 for(int32_t i = 0; i!=int32_t(current_node->
children.size()); i++){
81 grey_nodes.push_back(current_node->
children[i]);
85 task_ids.push_back(current_node->
getNode_id());
157 nodes.push_back(root);
159 name2id = std::map<std::string, int32_t>();
165 for (
size_t i = 0; i < nodes.size(); i++)
169 task_histogram.clear();
177 return nodes[task_id];
185 nodes[0]->beta =
beta;
195 if (child_name==
"")
SG_SERROR(
"child_name empty")
196 if (parent_name==
"")
SG_SERROR(
"parent_name empty")
203 nodes.push_back(child_node);
204 int32_t
id = nodes.size()-1;
206 name2id[child_name] = id;
224 return name2id[name];
238 std::set<CNode*> intersection;
240 std::set_intersection(root_path_lhs.begin(), root_path_lhs.end(),
241 root_path_rhs.begin(), root_path_rhs.end(),
242 std::inserter(intersection, intersection.end()));
256 CNode* node_lhs = get_node(task_lhs);
257 CNode* node_rhs = get_node(task_rhs);
260 std::set<CNode*> intersection = intersect_root_path(node_lhs, node_rhs);
264 for (std::set<CNode*>::const_iterator p = intersection.begin(); p != intersection.end(); ++p) {
279 task_histogram.clear();
283 for (std::vector<int32_t>::const_iterator it=task_vector_lhs.begin(); it!=task_vector_lhs.end(); it++)
285 task_histogram[*it] = 0.0;
289 for (std::vector<int32_t>::const_iterator it=task_vector_lhs.begin(); it!=task_vector_lhs.end(); it++)
291 task_histogram[*it] += 1.0;
295 for (std::map<int32_t, float64_t>::const_iterator it=task_histogram.begin(); it!=task_histogram.end(); it++)
297 task_histogram[it->first] = task_histogram[it->first] /
float64_t(task_vector_lhs.size());
305 return (int32_t)(nodes.size());
311 int32_t num_leaves = 0;
313 for (int32_t i=0; i!=get_num_nodes(); i++)
315 if (get_node(i)->is_leaf()==
true)
359 return name2id[name];
397 std::vector<std::string> task_rhs,
402 set_task_vector_lhs(task_lhs);
403 set_task_vector_rhs(task_rhs);
405 num_nodes = taxonomy.get_num_nodes();
407 dependency_matrix = std::vector<float64_t>(num_nodes * num_nodes);
424 for (int32_t i=0; i!=num_nodes; i++)
426 for (int32_t j=0; j!=num_nodes; j++)
429 float64_t similarity = taxonomy.compute_node_similarity(i, j);
430 set_node_similarity(i,j,similarity);
447 int32_t task_idx_lhs = task_vector_lhs[idx_lhs];
448 int32_t task_idx_rhs = task_vector_rhs[idx_rhs];
451 float64_t task_similarity = get_node_similarity(task_idx_lhs, task_idx_rhs);
467 SG_ERROR(
"normalize_lhs not implemented")
477 SG_ERROR(
"normalize_rhs not implemented")
486 task_vector_lhs.clear();
488 for (int32_t i = 0; i != (int32_t)(vec.size()); ++i)
490 task_vector_lhs.push_back(taxonomy.get_id(vec[i]));
494 taxonomy.update_task_histogram(task_vector_lhs);
502 task_vector_rhs.clear();
504 for (int32_t i = 0; i != (int32_t)(vec.size()); ++i)
506 task_vector_rhs.push_back(taxonomy.get_id(vec[i]));
514 set_task_vector_lhs(vec);
515 set_task_vector_rhs(vec);
522 return taxonomy.get_num_nodes();
532 return taxonomy.get_node_weight(idx);
542 taxonomy.set_node_weight(idx, weight);
557 ASSERT(node_lhs < num_nodes && node_lhs >= 0)
558 ASSERT(node_rhs < num_nodes && node_rhs >= 0)
560 return dependency_matrix[node_lhs * num_nodes + node_rhs];
573 ASSERT(node_lhs < num_nodes && node_lhs >= 0)
574 ASSERT(node_rhs < num_nodes && node_rhs >= 0)
576 dependency_matrix[node_lhs * num_nodes + node_rhs] = similarity;
583 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)
void scale(SGVector< T > &a, SGVector< T > &result, T alpha=1)
std::map< std::string, int32_t > name2id
CMultitaskKernelTreeNormalizer(std::vector< std::string > task_lhs, std::vector< std::string > task_rhs, CTaxonomy tax)
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 const char * get_name() const
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)
virtual const char * get_name() const
all of classes and functions are contained in the shogun namespace
int32_t getNode_id() const
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
std::vector< int32_t > get_task_ids_below()
virtual const char * get_name() const
std::vector< CNode * > nodes
The MultitaskKernel allows Multitask Learning via a modified kernel function based on taxonomy...