MultitaskKernelMaskPairNormalizer.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 _MULTITASKKERNELMASKPAIRNORMALIZER_H___
00012 #define _MULTITASKKERNELMASKPAIRNORMALIZER_H___
00013 
00014 #include <shogun/kernel/KernelNormalizer.h>
00015 #include <shogun/kernel/Kernel.h>
00016 
00017 
00018 namespace shogun
00019 {
00020 
00021 
00026 class CMultitaskKernelMaskPairNormalizer: public CKernelNormalizer
00027 {
00028 
00029 public:
00030 
00033     CMultitaskKernelMaskPairNormalizer() :
00034         CKernelNormalizer(), scale(1.0), normalization_constant(1.0)
00035     {
00036     }
00037 
00042     CMultitaskKernelMaskPairNormalizer(std::vector<int32_t> task_vector_,
00043                                        std::vector<std::pair<int32_t, int32_t> > active_pairs_) :
00044                                        scale(1.0), normalization_constant(1.0)
00045     {
00046 
00047         set_task_vector(task_vector_);
00048         active_pairs = active_pairs_;
00049 
00050     }
00051 
00052 
00054     virtual ~CMultitaskKernelMaskPairNormalizer()
00055     {
00056     }
00057 
00060     virtual bool init(CKernel* k)
00061     {
00062         ASSERT(k);
00063         int32_t num_lhs = k->get_num_vec_lhs();
00064         int32_t num_rhs = k->get_num_vec_rhs();
00065         ASSERT(num_lhs>0);
00066         ASSERT(num_rhs>0);
00067 
00068 
00069         //same as first-element normalizer
00070         CFeatures* old_lhs=k->lhs;
00071         CFeatures* old_rhs=k->rhs;
00072         k->lhs=old_lhs;
00073         k->rhs=old_lhs;
00074 
00075 
00076         if (std::string(k->get_name()) == "WeightedDegree") {
00077             SG_INFO("using first-element normalization\n");
00078             scale=k->compute(0, 0);
00079         } else {
00080             SG_INFO("no inner normalization for non-WDK kernel\n");
00081             scale=1.0;
00082         }
00083 
00084         k->lhs=old_lhs;
00085         k->rhs=old_rhs;
00086 
00087 
00088         return true;
00089     }
00090 
00091 
00092 
00098     inline virtual float64_t normalize(float64_t value, int32_t idx_lhs, int32_t idx_rhs)
00099     {
00100 
00101         //lookup tasks
00102         int32_t task_idx_lhs = task_vector_lhs[idx_lhs];
00103         int32_t task_idx_rhs = task_vector_rhs[idx_rhs];
00104 
00105         //lookup similarity
00106         float64_t task_similarity = get_similarity(task_idx_lhs, task_idx_rhs);
00107 
00108         //take task similarity into account
00109         float64_t similarity = (value/scale) * task_similarity;
00110 
00111 
00112         return similarity;
00113 
00114     }
00115 
00120     inline virtual float64_t normalize_lhs(float64_t value, int32_t idx_lhs)
00121     {
00122         SG_ERROR("normalize_lhs not implemented");
00123         return 0;
00124     }
00125 
00130     inline virtual float64_t normalize_rhs(float64_t value, int32_t idx_rhs)
00131     {
00132         SG_ERROR("normalize_rhs not implemented");
00133         return 0;
00134     }
00135 
00137     std::vector<int32_t> get_task_vector_lhs() const
00138     {
00139         return task_vector_lhs;
00140     }
00141 
00142 
00144     void set_task_vector_lhs(std::vector<int32_t> vec)
00145     {
00146 
00147         task_vector_lhs.clear();
00148 
00149         for (int32_t i = 0; i != (int32_t)(vec.size()); ++i)
00150         {
00151             task_vector_lhs.push_back(vec[i]);
00152         }
00153 
00154     }
00155 
00158     std::vector<int32_t> get_task_vector_rhs() const
00159     {
00160         return task_vector_rhs;
00161     }
00162 
00163 
00165     void set_task_vector_rhs(std::vector<int32_t> vec)
00166     {
00167 
00168         task_vector_rhs.clear();
00169 
00170         for (int32_t i = 0; i != (int32_t)(vec.size()); ++i)
00171         {
00172             task_vector_rhs.push_back(vec[i]);
00173         }
00174 
00175     }
00176 
00178     void set_task_vector(std::vector<int32_t> vec)
00179     {
00180         set_task_vector_lhs(vec);
00181         set_task_vector_rhs(vec);
00182     }
00183 
00184 
00190     float64_t get_similarity(int32_t task_lhs, int32_t task_rhs)
00191     {
00192 
00193         float64_t similarity = 0.0;
00194 
00195         for (int32_t i=0; i!=static_cast<int>(active_pairs.size()); i++)
00196         {
00197             std::pair<int32_t, int32_t> block = active_pairs[i];
00198 
00199             // ignore order of pair
00200             if ((block.first==task_lhs && block.second==task_rhs) ||
00201                 (block.first==task_rhs && block.second==task_lhs))
00202             {
00203                 similarity = 1.0 / normalization_constant;
00204                 break;
00205             }
00206         }
00207 
00208 
00209         return similarity;
00210 
00211     }
00212 
00214     std::vector<std::pair<int32_t, int32_t> > get_active_pairs()
00215     {
00216         return active_pairs;
00217     }
00218 
00220     float64_t get_normalization_constant () const
00221     {
00222         return normalization_constant;
00223     }
00224 
00226     float64_t set_normalization_constant(float64_t constant)
00227     {
00228         normalization_constant = constant;
00229 
00230         SG_NOTIMPLEMENTED;
00231         return 0.0;
00232     }
00233 
00234 
00236     inline virtual const char* get_name() const
00237     {
00238         return "MultitaskKernelMaskPairNormalizer";
00239     }
00240 
00241 
00242 
00243 protected:
00244 
00246     std::vector<std::pair<int32_t, int32_t> > active_pairs;
00247 
00249     std::vector<int32_t> task_vector_lhs;
00250 
00252     std::vector<int32_t> task_vector_rhs;
00253 
00255     float64_t scale;
00256 
00258     float64_t normalization_constant;
00259 
00260 };
00261 }
00262 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

SHOGUN Machine Learning Toolbox - Documentation