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

SHOGUN Machine Learning Toolbox - Documentation