/usr/include/shogun/transfer/multitask/LibLinearMTL.h is in libshogun-dev 3.2.0-7.3build4.
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 | /*
* 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 3 of the License, or
* (at your option) any later version.
*
* Written (W) 2011-2012 Christian Widmer
* Written (W) 2007-2010 Soeren Sonnenburg
* Copyright (c) 2007-2009 The LIBLINEAR Project.
* Copyright (C) 2007-2012 Fraunhofer Institute FIRST and Max-Planck-Society
*/
#ifndef _LIBLINEARMTL_H___
#define _LIBLINEARMTL_H___
#include <shogun/lib/config.h>
#include <shogun/lib/common.h>
#include <shogun/base/Parameter.h>
#include <shogun/machine/LinearMachine.h>
#include <shogun/optimization/liblinear/shogun_liblinear.h>
#include <shogun/lib/SGSparseMatrix.h>
#include <map>
namespace shogun
{
#ifdef HAVE_LAPACK
/** @brief mapped sparse matrix for
* representing graph relations of tasks
*/
class MappedSparseMatrix
{
public:
/** operator overload for matrix read only access
* @param i_row
* @param i_col
*/
inline const float64_t operator()(index_t i_row, index_t i_col) const
{
// lookup complexity is O(log n)
std::map<index_t, float64_t>::const_iterator it = data[i_row].find(i_col);
if (it != data[i_row].end())
{
// use mapping for lookup
return it->second;
} else {
return 0.0;
}
}
/** set matrix from SGSparseMatrix
* @param sgm
*/
void set_from_sparse(const SGSparseMatrix<float64_t> &sgm)
{
data.clear();
// deep copy sparse matrix
for (int32_t i=0; i!=sgm.num_vectors; i++)
{
SGSparseVector<float64_t> ts_row = sgm.sparse_matrix[i];
data.push_back(std::map<index_t, float64_t>());
for (int32_t k=0; k!=ts_row.num_feat_entries; k++)
{
// get data from sparse matrix
SGSparseVectorEntry<float64_t> e = ts_row.features[k];
data[i][e.feat_index] = e.entry;
}
}
}
/** under-the-hood data structure */
std::vector< std::map<index_t, float64_t> > data;
};
/** @brief class to implement LibLinear */
class CLibLinearMTL : public CLinearMachine
{
public:
/** default constructor */
CLibLinearMTL();
/** constructor (using L2R_L1LOSS_SVC_DUAL as default)
*
* @param C constant C
* @param traindat training features
* @param trainlab training labels
*/
CLibLinearMTL(
float64_t C, CDotFeatures* traindat,
CLabels* trainlab);
/** destructor */
virtual ~CLibLinearMTL();
/** get classifier type
*
* @return the classifier type
*/
virtual EMachineType get_classifier_type() { return CT_LIBLINEAR; }
/** set C
*
* @param c_neg C1
* @param c_pos C2
*/
inline void set_C(float64_t c_neg, float64_t c_pos) { C1=c_neg; C2=c_pos; }
/** get C1
*
* @return C1
*/
inline float64_t get_C1() { return C1; }
/** get C2
*
* @return C2
*/
inline float64_t get_C2() { return C2; }
/** set epsilon
*
* @param eps new epsilon
*/
inline void set_epsilon(float64_t eps) { epsilon=eps; }
/** get epsilon
*
* @return epsilon
*/
inline float64_t get_epsilon() { return epsilon; }
/** set if bias shall be enabled
*
* @param enable_bias if bias shall be enabled
*/
inline void set_bias_enabled(bool enable_bias) { use_bias=enable_bias; }
/** check if bias is enabled
*
* @return if bias is enabled
*/
inline bool get_bias_enabled() { return use_bias; }
/** @return object name */
virtual const char* get_name() const { return "LibLinearMTL"; }
/** get the maximum number of iterations liblinear is allowed to do */
inline int32_t get_max_iterations()
{
return max_iterations;
}
/** set the maximum number of iterations liblinear is allowed to do */
inline void set_max_iterations(int32_t max_iter=1000)
{
max_iterations=max_iter;
}
/** set number of tasks */
inline void set_num_tasks(int32_t nt)
{
num_tasks = nt;
}
/** set the linear term for qp */
inline void set_linear_term(SGVector<float64_t> linear_term)
{
if (!m_labels)
SG_ERROR("Please assign labels first!\n")
int32_t num_labels=m_labels->get_num_labels();
if (num_labels!=linear_term.vlen)
{
SG_ERROR("Number of labels (%d) does not match number"
" of entries (%d) in linear term \n", num_labels,
linear_term.vlen);
}
m_linear_term = linear_term;
}
/** set task indicator for lhs */
inline void set_task_indicator_lhs(SGVector<int32_t> ti)
{
task_indicator_lhs = ti;
}
/** set task indicator for rhs */
inline void set_task_indicator_rhs(SGVector<int32_t> ti)
{
task_indicator_rhs = ti;
}
/** set task similarity matrix */
inline void set_task_similarity_matrix(SGSparseMatrix<float64_t> tsm)
{
task_similarity_matrix.set_from_sparse(tsm);
}
/** set graph laplacian */
inline void set_graph_laplacian(SGMatrix<float64_t> lap)
{
graph_laplacian = lap;
}
/** get V
*
* @return matrix of weight vectors
*/
inline SGMatrix<float64_t> get_V()
{
return V;
}
/** get W
*
* @return matrix of weight vectors
*/
inline SGMatrix<float64_t> get_W()
{
int32_t w_size = V.num_rows;
SGMatrix<float64_t> W = SGMatrix<float64_t>(w_size, num_tasks);
for(int32_t k=0; k<w_size*num_tasks; k++)
{
W.matrix[k] = 0;
}
for (int32_t s=0; s<num_tasks; s++)
{
float64_t* v_s = V.get_column_vector(s);
for (int32_t t=0; t<num_tasks; t++)
{
float64_t sim_ts = task_similarity_matrix(s,t);
for(int32_t i=0; i<w_size; i++)
{
W.matrix[t*w_size + i] += sim_ts * v_s[i];
}
}
}
return W;
}
/** get alphas
*
* @return matrix of example weights alphas
*/
inline SGVector<float64_t> get_alphas()
{
return alphas;
}
/** compute primal objective
*
* @return primal objective
*/
virtual float64_t compute_primal_obj();
/** compute dual objective
*
* @return dual objective
*/
virtual float64_t compute_dual_obj();
/** compute duality gap
*
* @return duality gap
*/
virtual float64_t compute_duality_gap();
protected:
/** train linear SVM classifier
*
* @param data training data (parameter can be avoided if distance or
* kernel-based classifiers are used and distance/kernels are
* initialized with train data)
*
* @return whether training was successful
*/
virtual bool train_machine(CFeatures* data=NULL);
private:
/** set up parameters */
void init();
void solve_l2r_l1l2_svc(
const liblinear_problem *prob, double eps, double Cp, double Cn);
protected:
/** C1 */
float64_t C1;
/** C2 */
float64_t C2;
/** if bias shall be used */
bool use_bias;
/** epsilon */
float64_t epsilon;
/** maximum number of iterations */
int32_t max_iterations;
/** precomputed linear term */
SGVector<float64_t> m_linear_term;
/** keep track of alphas */
SGVector<float64_t> alphas;
/** set number of tasks */
int32_t num_tasks;
/** task indicator left hand side */
SGVector<int32_t> task_indicator_lhs;
/** task indicator right hand side */
SGVector<int32_t> task_indicator_rhs;
/** task similarity matrix */
//SGMatrix<float64_t> task_similarity_matrix;
//SGSparseMatrix<float64_t> task_similarity_matrix;
MappedSparseMatrix task_similarity_matrix;
/** task similarity matrix */
SGMatrix<float64_t> graph_laplacian;
/** parameter matrix n * d */
SGMatrix<float64_t> V;
/** duality gap */
float64_t duality_gap;
};
#endif //HAVE_LAPACK
} /* namespace shogun */
#endif //_LIBLINEARMTL_H___
|