SHOGUN  v1.1.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
ScatterKernelNormalizer.h
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License as published by
4  * the Free Software Foundation; either version 3 of the License, or
5  * (at your option) any later version.
6  *
7  * Written (W) 2010 Soeren Sonnenburg
8  * Copyright (C) 2010 Berlin Institute of Technology
9  */
10 
11 #ifndef _SCATTERKERNELNORMALIZER_H___
12 #define _SCATTERKERNELNORMALIZER_H___
13 
16 #include <shogun/kernel/Kernel.h>
17 #include <shogun/features/Labels.h>
18 #include <shogun/io/SGIO.h>
19 
20 namespace shogun
21 {
24 {
25 
26 public:
29  {
30  init();
31  }
32 
35  CScatterKernelNormalizer(float64_t const_diag, float64_t const_offdiag,
36  CLabels* labels,CKernelNormalizer* normalizer=NULL)
38  {
39  init();
40 
41  m_testing_class=-1;
42  m_const_diag=const_diag;
43  m_const_offdiag=const_offdiag;
44 
45  ASSERT(labels)
46  SG_REF(labels);
47  m_labels=labels;
48 
49  if (normalizer==NULL)
50  normalizer=new CIdentityKernelNormalizer();
51  SG_REF(normalizer);
52  m_normalizer=normalizer;
53 
54  SG_DEBUG("Constructing ScatterKernelNormalizer with const_diag=%g"
55  " const_offdiag=%g num_labels=%d and normalizer='%s'\n",
56  const_diag, const_offdiag, labels->get_num_labels(),
57  normalizer->get_name());
58  }
59 
62  {
65  }
66 
69  virtual bool init(CKernel* k)
70  {
71  m_normalizer->init(k);
72  return true;
73  }
74 
80  {
81  return m_testing_class;
82  }
83 
88  void set_testing_class(int32_t c)
89  {
91  }
92 
98  inline virtual float64_t normalize(float64_t value, int32_t idx_lhs,
99  int32_t idx_rhs)
100  {
101  value=m_normalizer->normalize(value, idx_lhs, idx_rhs);
103 
104  if (m_testing_class>=0)
105  {
106  if (m_labels->get_label(idx_lhs) == m_testing_class)
107  c=m_const_diag;
108  }
109  else
110  {
111  if (m_labels->get_label(idx_lhs) == m_labels->get_label(idx_rhs))
112  c=m_const_diag;
113 
114  }
115  return value*c;
116  }
117 
122  inline virtual float64_t normalize_lhs(float64_t value, int32_t idx_lhs)
123  {
124  SG_ERROR("normalize_lhs not implemented");
125  return 0;
126  }
127 
132  inline virtual float64_t normalize_rhs(float64_t value, int32_t idx_rhs)
133  {
134  SG_ERROR("normalize_rhs not implemented");
135  return 0;
136  }
137 
139  inline virtual const char* get_name() const
140  {
141  return "ScatterKernelNormalizer";
142  }
143 
144 private:
145  void init()
146  {
147  m_const_diag = 1.0;
148  m_const_offdiag = 1.0;
149 
150  m_labels = NULL;
151  m_normalizer = NULL;
152 
153  m_testing_class = -1;
154 
155 
156  m_parameters->add(&m_testing_class, "m_testing_class"
157  "Testing Class.");
158  m_parameters->add(&m_const_diag, "m_const_diag"
159  "Factor to multiply to diagonal elements.");
160  m_parameters->add(&m_const_offdiag, "m_const_offdiag"
161  "Factor to multiply to off-diagonal elements.");
162 
163  m_parameters->add((CSGObject**) &m_labels, "m_labels", "Labels");
164  m_parameters->add((CSGObject**) &m_normalizer, "m_normalizer", "Kernel normalizer.");
165  }
166 
167 protected:
168 
173 
176 
179 
182 };
183 }
184 #endif
185 

SHOGUN Machine Learning Toolbox - Documentation