/usr/include/shogun/kernel/MultitaskKernelPlifNormalizer.h is in libshogun-dev 1.1.0-4ubuntu2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 | /*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* Written (W) 2010 Christian Widmer
* Copyright (C) 2010 Max-Planck-Society
*/
#ifndef _MULTITASKKERNELPLIFNORMALIZER_H___
#define _MULTITASKKERNELPLIFNORMALIZER_H___
#include <shogun/kernel/KernelNormalizer.h>
#include <shogun/kernel/MultitaskKernelMklNormalizer.h>
#include <shogun/kernel/Kernel.h>
#include <algorithm>
namespace shogun
{
/** @brief The MultitaskKernel allows learning a piece-wise linear function (PLIF) via MKL
*
*/
class CMultitaskKernelPlifNormalizer: public CMultitaskKernelMklNormalizer
{
public:
/** default constructor */
CMultitaskKernelPlifNormalizer() : CMultitaskKernelMklNormalizer()
{
num_tasks = 0;
num_tasksqr = 0;
num_betas = 0;
}
/** constructor
*/
CMultitaskKernelPlifNormalizer(std::vector<float64_t> support_, std::vector<int32_t> task_vector)
: CMultitaskKernelMklNormalizer()
{
num_betas = static_cast<int>(support_.size());
support = support_;
// init support points values with constant function
betas = std::vector<float64_t>(num_betas);
for (int i=0; i!=num_betas; i++)
{
betas[i] = 1;
}
num_tasks = get_num_unique_tasks(task_vector);
num_tasksqr = num_tasks * num_tasks;
// set both sides equally
set_task_vector(task_vector);
// init distance matrix
distance_matrix = std::vector<float64_t>(num_tasksqr);
// init similarity matrix
similarity_matrix = std::vector<float64_t>(num_tasksqr);
}
/** normalize the kernel value
* @param value kernel value
* @param idx_lhs index of left hand side vector
* @param idx_rhs index of right hand side vector
*/
inline virtual float64_t normalize(float64_t value, int32_t idx_lhs,
int32_t idx_rhs)
{
//lookup tasks
int32_t task_idx_lhs = task_vector_lhs[idx_lhs];
int32_t task_idx_rhs = task_vector_rhs[idx_rhs];
//lookup similarity
float64_t task_similarity = get_task_similarity(task_idx_lhs,
task_idx_rhs);
//take task similarity into account
float64_t similarity = (value/scale) * task_similarity;
return similarity;
}
/** helper routine
*
* @param vec vector with containing task_id for each example
* @return number of unique task ids
*/
int32_t get_num_unique_tasks(std::vector<int32_t> vec) {
//sort
std::sort(vec.begin(), vec.end());
//reorder tasks with unique prefix
std::vector<int32_t>::iterator endLocation = std::unique(vec.begin(), vec.end());
//count unique tasks
int32_t num_vec = std::distance(vec.begin(), endLocation);
return num_vec;
}
/** default destructor */
virtual ~CMultitaskKernelPlifNormalizer()
{
}
/** update cache */
void update_cache()
{
for (int32_t i=0; i!=num_tasks; i++)
{
for (int32_t j=0; j!=num_tasks; j++)
{
float64_t similarity = compute_task_similarity(i, j);
set_task_similarity(i,j,similarity);
}
}
}
/** derive similarity from distance with plif */
float64_t compute_task_similarity(int32_t task_a, int32_t task_b)
{
float64_t distance = get_task_distance(task_a, task_b);
float64_t similarity = -1;
int32_t upper_bound_idx = -1;
// determine interval
for (int i=1; i!=num_betas; i++)
{
if (distance <= support[i])
{
upper_bound_idx = i;
break;
}
}
// perform interpolation (constant for beyond upper bound)
if (upper_bound_idx == -1)
{
similarity = betas[num_betas-1];
} else {
int32_t lower_bound_idx = upper_bound_idx - 1;
float64_t interval_size = support[upper_bound_idx] - support[lower_bound_idx];
float64_t factor_lower = 1 - (distance - support[lower_bound_idx]) / interval_size;
float64_t factor_upper = 1 - factor_lower;
similarity = factor_lower*betas[lower_bound_idx] + factor_upper*betas[upper_bound_idx];
}
return similarity;
}
public:
/** @return vec task vector with containing task_id for each example on left hand side */
virtual std::vector<int32_t> get_task_vector_lhs() const
{
return task_vector_lhs;
}
/** @param vec task vector with containing task_id for each example */
virtual void set_task_vector_lhs(std::vector<int32_t> vec)
{
task_vector_lhs = vec;
}
/** @return vec task vector with containing task_id for each example on right hand side */
virtual std::vector<int32_t> get_task_vector_rhs() const
{
return task_vector_rhs;
}
/** @param vec task vector with containing task_id for each example */
virtual void set_task_vector_rhs(std::vector<int32_t> vec)
{
task_vector_rhs = vec;
}
/** @param vec task vector with containing task_id for each example */
virtual void set_task_vector(std::vector<int32_t> vec)
{
task_vector_lhs = vec;
task_vector_rhs = vec;
}
/**
* @param task_lhs task_id on left hand side
* @param task_rhs task_id on right hand side
* @return distance between tasks
*/
float64_t get_task_distance(int32_t task_lhs, int32_t task_rhs)
{
ASSERT(task_lhs < num_tasks && task_lhs >= 0);
ASSERT(task_rhs < num_tasks && task_rhs >= 0);
return distance_matrix[task_lhs * num_tasks + task_rhs];
}
/**
* @param task_lhs task_id on left hand side
* @param task_rhs task_id on right hand side
* @param distance distance between tasks
*/
void set_task_distance(int32_t task_lhs, int32_t task_rhs,
float64_t distance)
{
ASSERT(task_lhs < num_tasks && task_lhs >= 0);
ASSERT(task_rhs < num_tasks && task_rhs >= 0);
distance_matrix[task_lhs * num_tasks + task_rhs] = distance;
}
/**
* @param task_lhs task_id on left hand side
* @param task_rhs task_id on right hand side
* @return similarity between tasks
*/
float64_t get_task_similarity(int32_t task_lhs, int32_t task_rhs)
{
ASSERT(task_lhs < num_tasks && task_lhs >= 0);
ASSERT(task_rhs < num_tasks && task_rhs >= 0);
return similarity_matrix[task_lhs * num_tasks + task_rhs];
}
/**
* @param task_lhs task_id on left hand side
* @param task_rhs task_id on right hand side
* @param similarity similarity between tasks
*/
void set_task_similarity(int32_t task_lhs, int32_t task_rhs,
float64_t similarity)
{
ASSERT(task_lhs < num_tasks && task_lhs >= 0);
ASSERT(task_rhs < num_tasks && task_rhs >= 0);
similarity_matrix[task_lhs * num_tasks + task_rhs] = similarity;
}
/**
* @param idx index of MKL weight to get
*/
float64_t get_beta(int32_t idx)
{
return betas[idx];
}
/**
* @param idx index of MKL weight to set
* @param weight MKL weight to set
*/
void set_beta(int32_t idx, float64_t weight)
{
betas[idx] = weight;
update_cache();
}
/**
* @return number of kernel weights (support points)
*/
int32_t get_num_betas()
{
return num_betas;
}
/** @return object name */
inline virtual const char* get_name() const
{
return "MultitaskKernelPlifNormalizer";
}
/** casts kernel normalizer to multitask kernel plif normalizer
* @param n kernel normalizer to cast
*/
CMultitaskKernelPlifNormalizer* KernelNormalizerToMultitaskKernelPlifNormalizer(CKernelNormalizer* n)
{
return dynamic_cast<shogun::CMultitaskKernelPlifNormalizer*>(n);
}
protected:
/** register the parameters
*/
virtual void register_params()
{
m_parameters->add(&num_tasks, "num_tasks", "the number of tasks");
m_parameters->add(&num_betas, "num_betas", "the number of weights");
m_parameters->add_vector((SGString<float64_t>**)&distance_matrix, &num_tasksqr, "distance_matrix", "distance between tasks");
m_parameters->add_vector((SGString<float64_t>**)&similarity_matrix, &num_tasksqr, "similarity_matrix", "similarity between tasks");
m_parameters->add_vector((SGString<float64_t>**)&betas, &num_betas, "num_betas", "weights");
m_parameters->add_vector((SGString<float64_t>**)&support, &num_betas, "support", "support points");
}
/** number of tasks **/
int32_t num_tasks;
/** square of num_tasks -- for registration purpose**/
int32_t num_tasksqr;
/** task vector indicating to which task each example on the left hand side belongs **/
std::vector<int32_t> task_vector_lhs;
/** task vector indicating to which task each example on the right hand side belongs **/
std::vector<int32_t> task_vector_rhs;
/** MxM matrix encoding distance between tasks **/
std::vector<float64_t> distance_matrix;
/** MxM matrix encoding similarity between tasks **/
std::vector<float64_t> similarity_matrix;
/** number of weights **/
int32_t num_betas;
/** weights **/
std::vector<float64_t> betas;
/** support points **/
std::vector<float64_t> support;
};
}
#endif
|