MultitaskKernelMaskNormalizer.h

Go to the documentation of this file.
00001 /*
00002  * This program is free software; you can redistribute it and/or modify
00003  * it under the terms of the GNU General Public License as published by
00004  * the Free Software Foundation; either version 2 of the License, or
00005  * (at your option) any later version.
00006  *
00007  * Written (W) 2010 Christian Widmer
00008  * Copyright (C) 2010 Max-Planck-Society
00009  */
00010 
00011 #ifndef _MULTITASKKERNELMASKNORMALIZER_H___
00012 #define _MULTITASKKERNELMASKNORMALIZER_H___
00013 
00014 #include <shogun/kernel/KernelNormalizer.h>
00015 #include <shogun/kernel/Kernel.h>
00016 
00017 namespace shogun
00018 {
00019 
00020 
00031 class CMultitaskKernelMaskNormalizer: public CKernelNormalizer
00032 {
00033 
00034 public:
00035 
00038     CMultitaskKernelMaskNormalizer() : CKernelNormalizer(),
00039         scale(1.0), normalization_constant(1.0)
00040     {
00041     }
00042 
00049     CMultitaskKernelMaskNormalizer(std::vector<int32_t> task_lhs,
00050                                    std::vector<int32_t> task_rhs,
00051                                    std::vector<int32_t> active_tasks_vec)
00052         : CKernelNormalizer(), scale(1.0), normalization_constant(1.0)
00053     {
00054 
00055 
00056         set_task_vector_lhs(task_lhs);
00057         set_task_vector_rhs(task_rhs);
00058 
00059         // set active tasks
00060         for (int32_t i = 0; i != (int32_t)(active_tasks_vec.size()); ++i)
00061         {
00062             active_tasks.insert(active_tasks_vec[i]);
00063         }
00064 
00065     }
00066 
00067 
00069     virtual ~CMultitaskKernelMaskNormalizer()
00070     {
00071     }
00072 
00075     virtual bool init(CKernel* k)
00076     {
00077         ASSERT(k);
00078         int32_t num_lhs = k->get_num_vec_lhs();
00079         int32_t num_rhs = k->get_num_vec_rhs();
00080         ASSERT(num_lhs>0);
00081         ASSERT(num_rhs>0);
00082 
00083 
00084         //same as first-element normalizer
00085         CFeatures* old_lhs=k->lhs;
00086         CFeatures* old_rhs=k->rhs;
00087         k->lhs=old_lhs;
00088         k->rhs=old_lhs;
00089 
00090         if (std::string(k->get_name()) == "WeightedDegree") {
00091             SG_INFO("using first-element normalization\n");
00092             scale=k->compute(0, 0);
00093         } else {
00094             SG_INFO("no inner normalization for non-WDK kernel\n");
00095             scale=1.0;
00096         }
00097 
00098         k->lhs=old_lhs;
00099         k->rhs=old_rhs;
00100 
00101 
00102         return true;
00103     }
00104 
00105 
00106 
00112     inline virtual float64_t normalize(float64_t value, int32_t idx_lhs, int32_t idx_rhs)
00113     {
00114 
00115         //lookup tasks
00116         int32_t task_idx_lhs = task_vector_lhs[idx_lhs];
00117         int32_t task_idx_rhs = task_vector_rhs[idx_rhs];
00118 
00119         //lookup similarity
00120         float64_t task_similarity = get_similarity(task_idx_lhs, task_idx_rhs);
00121 
00122         //take task similarity into account
00123         float64_t similarity = (value/scale) * task_similarity;
00124 
00125 
00126         return similarity;
00127 
00128     }
00129 
00134     inline virtual float64_t normalize_lhs(float64_t value, int32_t idx_lhs)
00135     {
00136         SG_ERROR("normalize_lhs not implemented");
00137         return 0;
00138     }
00139 
00144     inline virtual float64_t normalize_rhs(float64_t value, int32_t idx_rhs)
00145     {
00146         SG_ERROR("normalize_rhs not implemented");
00147         return 0;
00148     }
00149 
00151     std::vector<int32_t> get_task_vector_lhs() const
00152     {
00153         return task_vector_lhs;
00154     }
00155 
00156 
00158     void set_task_vector_lhs(std::vector<int32_t> vec)
00159     {
00160 
00161         task_vector_lhs.clear();
00162 
00163         for (int32_t i = 0; i != (int32_t)(vec.size()); ++i)
00164         {
00165             task_vector_lhs.push_back(vec[i]);
00166         }
00167 
00168     }
00169 
00172     std::vector<int32_t> get_task_vector_rhs() const
00173     {
00174         return task_vector_rhs;
00175     }
00176 
00177 
00179     void set_task_vector_rhs(std::vector<int32_t> vec)
00180     {
00181 
00182         task_vector_rhs.clear();
00183 
00184         for (int32_t i = 0; i != (int32_t)(vec.size()); ++i)
00185         {
00186             task_vector_rhs.push_back(vec[i]);
00187         }
00188 
00189     }
00190 
00192     void set_task_vector(std::vector<int32_t> vec)
00193     {
00194         set_task_vector_lhs(vec);
00195         set_task_vector_rhs(vec);
00196     }
00197 
00198 
00204     float64_t get_similarity(int32_t task_lhs, int32_t task_rhs)
00205     {
00206 
00207         const bool lhs_is_in = active_tasks.find(task_lhs) != active_tasks.end();
00208         const bool rhs_is_in = active_tasks.find(task_rhs) != active_tasks.end();
00209 
00210         float64_t similarity = 0.0;
00211 
00212         if (lhs_is_in && rhs_is_in)
00213         {
00214             similarity = 1.0 / normalization_constant;
00215         }
00216 
00217         return similarity;
00218 
00219     }
00220 
00224     std::vector<int32_t> get_active_tasks()
00225     {
00226 
00227         std::vector<int32_t> active_tasks_vec;
00228 
00229         // set active tasks
00230         for (std::set<int32_t>::const_iterator it=active_tasks.begin(); it!=active_tasks.end(); it++)
00231         {
00232             active_tasks_vec.push_back(*it);
00233         }
00234 
00235         return active_tasks_vec;
00236     }
00237 
00241     float64_t get_normalization_constant () const
00242     {
00243         return normalization_constant;
00244     }
00245 
00249     float64_t set_normalization_constant(float64_t constant)
00250     {
00251         normalization_constant = constant;
00252 
00253         SG_NOTIMPLEMENTED;
00254         return 0.0;
00255     }
00256 
00258     inline virtual const char* get_name() const
00259     {
00260         return "MultitaskKernelMaskNormalizer";
00261     }
00262 
00263 
00264 
00265 protected:
00266 
00268     std::set<int32_t> active_tasks;
00269 
00271     std::vector<int32_t> task_vector_lhs;
00272 
00274     std::vector<int32_t> task_vector_rhs;
00275 
00277     float64_t scale;
00278 
00280     float64_t normalization_constant;
00281 
00282 };
00283 }
00284 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

SHOGUN Machine Learning Toolbox - Documentation