SHOGUN  4.1.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules 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 
14 #include <shogun/lib/config.h>
15 
18 #include <shogun/kernel/Kernel.h>
19 #include <shogun/labels/Labels.h>
21 #include <shogun/io/SGIO.h>
22 
23 namespace shogun
24 {
27 {
28 
29 public:
32  {
33  init();
34  }
35 
38  CScatterKernelNormalizer(float64_t const_diag, float64_t const_offdiag,
39  CLabels* labels,CKernelNormalizer* normalizer=NULL)
41  {
42  init();
43 
44  m_testing_class=-1;
45  m_const_diag=const_diag;
46  m_const_offdiag=const_offdiag;
47 
48  ASSERT(labels)
49  SG_REF(labels);
50  m_labels=labels;
52  labels->ensure_valid();
53 
54  if (normalizer==NULL)
55  normalizer=new CIdentityKernelNormalizer();
56  SG_REF(normalizer);
57  m_normalizer=normalizer;
58 
59  SG_DEBUG("Constructing ScatterKernelNormalizer with const_diag=%g"
60  " const_offdiag=%g num_labels=%d and normalizer='%s'\n",
61  const_diag, const_offdiag, labels->get_num_labels(),
62  normalizer->get_name());
63  }
64 
67  {
70  }
71 
74  virtual bool init(CKernel* k)
75  {
76  m_normalizer->init(k);
77  return true;
78  }
79 
85  {
86  return m_testing_class;
87  }
88 
93  void set_testing_class(int32_t c)
94  {
96  }
97 
103  virtual float64_t normalize(float64_t value, int32_t idx_lhs,
104  int32_t idx_rhs)
105  {
106  value=m_normalizer->normalize(value, idx_lhs, idx_rhs);
108 
109  if (m_testing_class>=0)
110  {
111  if (((CMulticlassLabels*) m_labels)->get_label(idx_lhs) == m_testing_class)
112  c=m_const_diag;
113  }
114  else
115  {
116  if (((CMulticlassLabels*) m_labels)->get_label(idx_lhs) == ((CMulticlassLabels*) m_labels)->get_label(idx_rhs))
117  c=m_const_diag;
118 
119  }
120  return value*c;
121  }
122 
127  virtual float64_t normalize_lhs(float64_t value, int32_t idx_lhs)
128  {
129  SG_ERROR("normalize_lhs not implemented")
130  return 0;
131  }
132 
137  virtual float64_t normalize_rhs(float64_t value, int32_t idx_rhs)
138  {
139  SG_ERROR("normalize_rhs not implemented")
140  return 0;
141  }
142 
144  virtual const char* get_name() const
145  {
146  return "ScatterKernelNormalizer";
147  }
148 
149 private:
150  void init()
151  {
152  m_const_diag = 1.0;
153  m_const_offdiag = 1.0;
154 
155  m_labels = NULL;
156  m_normalizer = NULL;
157 
158  m_testing_class = -1;
159 
160  SG_ADD(&m_testing_class, "m_testing_class",
161  "Testing Class.", MS_NOT_AVAILABLE);
162  SG_ADD(&m_const_diag, "m_const_diag",
163  "Factor to multiply to diagonal elements.", MS_AVAILABLE);
164  SG_ADD(&m_const_offdiag, "m_const_offdiag",
165  "Factor to multiply to off-diagonal elements.", MS_AVAILABLE);
166 
167  SG_ADD((CSGObject**) &m_labels, "m_labels", "Labels", MS_NOT_AVAILABLE);
168  SG_ADD((CSGObject**) &m_normalizer, "m_normalizer", "Kernel normalizer.",
169  MS_AVAILABLE);
170  }
171 
172 protected:
173 
178 
181 
184 
187 };
188 }
189 #endif
190 
virtual ELabelType get_label_type() const =0
virtual float64_t normalize_rhs(float64_t value, int32_t idx_rhs)
The class Labels models labels, i.e. class assignments of objects.
Definition: Labels.h:43
virtual int32_t get_num_labels() const =0
multi-class labels 0,1,...
Definition: LabelTypes.h:20
#define SG_ERROR(...)
Definition: SGIO.h:129
CScatterKernelNormalizer(float64_t const_diag, float64_t const_offdiag, CLabels *labels, CKernelNormalizer *normalizer=NULL)
#define SG_REF(x)
Definition: SGObject.h:51
Multiclass Labels for multi-class classification.
#define ASSERT(x)
Definition: SGIO.h:201
Class SGObject is the base class of all shogun objects.
Definition: SGObject.h:112
double float64_t
Definition: common.h:50
the scatter kernel normalizer
Identity Kernel Normalization, i.e. no normalization is applied.
virtual float64_t normalize_lhs(float64_t value, int32_t idx_lhs)
The class Kernel Normalizer defines a function to post-process kernel values.
#define SG_UNREF(x)
Definition: SGObject.h:52
#define SG_DEBUG(...)
Definition: SGIO.h:107
all of classes and functions are contained in the shogun namespace
Definition: class_list.h:18
virtual bool init(CKernel *k)=0
The Kernel base class.
Definition: Kernel.h:158
virtual float64_t normalize(float64_t value, int32_t idx_lhs, int32_t idx_rhs)
#define SG_ADD(...)
Definition: SGObject.h:81
virtual float64_t normalize(float64_t value, int32_t idx_lhs, int32_t idx_rhs)=0
virtual void ensure_valid(const char *context=NULL)=0
virtual const char * get_name() const

SHOGUN Machine Learning Toolbox - Documentation