SHOGUN  4.1.0
 全部  命名空间 文件 函数 变量 类型定义 枚举 枚举值 友元 宏定义  
PNormMappingFunction.h
浏览该文件的文档.
1 /*
2  * Copyright (c) The Shogun Machine Learning Toolbox
3  * Written (w) 2015 Wu Lin
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright notice, this
10  * list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright notice,
12  * this list of conditions and the following disclaimer in the documentation
13  * and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
19  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  *
26  * The views and conclusions contained in the software and documentation are those
27  * of the authors and should not be interpreted as representing official policies,
28  * either expressed or implied, of the Shogun Development Team.
29  *
30  */
31 
32 #ifndef PNORMMAPPINGFUNCTION_H
33 #define PNORMMAPPINGFUNCTION_H
36 namespace shogun
37 {
46 {
47 public:
50  {
51  init();
52  }
53  virtual ~PNormMappingFunction() {}
54 
58  virtual void set_norm(float64_t p)
59  {
60  if(p<2.0)
61  {
62  SG_SWARNING("The norm (%f) should not be less than 2.0 and we use p=2.0 in this case\n", p);
63  }
64  else
65  m_p=p;
66  }
67 
75  {
76  SGVector<float64_t> dual_variable(variable.vlen);
77  float64_t q=1.0/(1.0-1.0/m_p);
78  projection(variable, dual_variable, q);
79  return dual_variable;
80  }
81 
88  virtual void update_variable(SGVector<float64_t> variable, SGVector<float64_t> dual_variable)
89  {
90  projection(dual_variable, variable, m_p);
91  }
92 
101  virtual void update_context(CMinimizerContext* context)
102  {
103  REQUIRE(context, "Contest must not NULL\n");
104  }
105 
112  virtual void load_from_context(CMinimizerContext* context)
113  {
114  REQUIRE(context, "Contest must not NULL\n");
115  }
116 protected:
119 
126  virtual void projection(SGVector<float64_t> input, SGVector<float64_t> output, float64_t degree)
127  {
128  REQUIRE(input.vlen==output.vlen,"The lenght (%d) of input and the length (%d) of output are diffent\n",
129  input.vlen, output.vlen);
130  float64_t scale=0.0;
131  for(index_t idx=0; idx<input.vlen; idx++)
132  {
133  scale += CMath::pow(CMath::abs(input[idx]),degree);
134  if (input[idx] >= 0.0)
135  output[idx]=CMath::pow(input[idx],degree-1);
136  else
137  output[idx]=-CMath::pow(-input[idx],degree-1);
138  }
139  scale=CMath::pow(scale,1.0-2.0/degree);
140  for(index_t idx=0; idx<input.vlen; idx++)
141  output[idx]/=scale;
142  }
143 
144 private:
145  void init()
146  {
147  m_p=2.0;
148  }
149 };
150 
151 }
152 
153 #endif
The base mapping function for mirror descend.
virtual void update_context(CMinimizerContext *context)
int32_t index_t
Definition: common.h:62
The class is used to serialize and deserialize variables for the optimization framework.
#define SG_SWARNING(...)
Definition: SGIO.h:178
#define REQUIRE(x,...)
Definition: SGIO.h:206
virtual SGVector< float64_t > get_dual_variable(SGVector< float64_t > variable)
index_t vlen
Definition: SGVector.h:494
double float64_t
Definition: common.h:50
virtual void set_norm(float64_t p)
all of classes and functions are contained in the shogun namespace
Definition: class_list.h:18
virtual void projection(SGVector< float64_t > input, SGVector< float64_t > output, float64_t degree)
This implements the P-norm mapping/projection function.
virtual void update_variable(SGVector< float64_t > variable, SGVector< float64_t > dual_variable)
void scale(Matrix A, Matrix B, typename Matrix::Scalar alpha)
Definition: Core.h:93
virtual void load_from_context(CMinimizerContext *context)
static int32_t pow(bool x, int32_t n)
Definition: Math.h:535
static T abs(T a)
Definition: Math.h:179

SHOGUN 机器学习工具包 - 项目文档