SHOGUN  4.1.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
ElasticNetPenalty.h
Go to the documentation of this file.
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 ELASTICNETPENALTY_H
33 #define ELASTICNETPENALTY_H
37 #include <shogun/lib/config.h>
39 namespace shogun
40 {
55 {
56 public:
58  :SparsePenalty() {init();}
59 
61  {
62  delete m_l1_penalty;
63  delete m_l2_penalty;
64  }
65 
70  virtual void set_l1_ratio(float64_t ratio)
71  {
72  REQUIRE(ratio>0.0 && ratio<1.0, "");
73  m_l1_ratio=ratio;
74  }
75 
82  virtual float64_t get_penalty(float64_t variable)
83  {
84  check_ratio();
85  float64_t penalty=m_l1_ratio*m_l1_penalty->get_penalty(variable);
86  penalty+=(1.0-m_l1_ratio)*m_l2_penalty->get_penalty(variable);
87  return penalty;
88  }
89 
97  float64_t gradient_of_variable)
98  {
99  check_ratio();
100  float64_t grad=m_l1_ratio*m_l1_penalty->get_penalty_gradient(variable, gradient_of_variable);
101  grad+=(1.0-m_l1_ratio)*m_l2_penalty->get_penalty_gradient(variable, gradient_of_variable);
102  return grad;
103  }
104 
111  {
113  }
114 
120  float64_t proximal_weight)
121  {
122  check_ratio();
123  m_l1_penalty->update_variable_for_proximity(variable, proximal_weight*m_l1_ratio);
124  }
125 
131  virtual void update_context(CMinimizerContext* context)
132  {
133  REQUIRE(context, "Context must set\n");
134  m_l1_penalty->update_context(context);
135  m_l2_penalty->update_context(context);
136  }
137 
142  virtual void load_from_context(CMinimizerContext* context)
143  {
144  REQUIRE(context, "Context must set\n");
147  }
148 
154  virtual float64_t get_sparse_variable(float64_t variable, float64_t penalty_weight)
155  {
156  check_ratio();
157  return m_l1_penalty->get_sparse_variable(variable, penalty_weight*m_l1_ratio);
158  }
159 
160 protected:
161 
163  virtual void check_ratio()
164  {
165  REQUIRE(m_l1_ratio>0, "l1_ratio must set\n");
166  }
167 
170 
173 
176 
177 private:
179  void init()
180  {
181  m_l1_ratio=0;
182  m_l1_penalty=new L1Penalty();
183  m_l2_penalty=new L2Penalty();
184  }
185 };
186 
187 }
188 
189 #endif
virtual void load_from_context(CMinimizerContext *context)
Definition: L1Penalty.h:118
The is the base class for L1 penalty/regularization within the FirstOrderMinimizer framework...
Definition: L1Penalty.h:52
virtual float64_t get_penalty(float64_t variable)
Definition: L2Penalty.h:61
virtual void update_context(CMinimizerContext *context)
Definition: L2Penalty.h:83
The class is used to serialize and deserialize variables for the optimization framework.
The is the base class for ElasticNet penalty/regularization within the FirstOrderMinimizer framework...
#define REQUIRE(x,...)
Definition: SGIO.h:206
virtual float64_t get_penalty_gradient(float64_t variable, float64_t gradient_of_variable)
Definition: L2Penalty.h:75
The base class for sparse penalty/regularization used in minimization.
Definition: SparsePenalty.h:46
virtual float64_t get_penalty_gradient(float64_t variable, float64_t gradient_of_variable)
Definition: L1Penalty.h:79
virtual void load_from_context(CMinimizerContext *context)
virtual void update_variable_for_proximity(SGVector< float64_t > variable, float64_t proximal_weight)
virtual void update_context(CMinimizerContext *context)
Definition: L1Penalty.h:109
virtual float64_t get_sparse_variable(float64_t variable, float64_t penalty_weight)
virtual float64_t get_penalty_gradient(float64_t variable, float64_t gradient_of_variable)
static const float64_t epsilon
Definition: libbmrm.cpp:25
virtual float64_t get_penalty(float64_t variable)
virtual void set_rounding_epsilon(float64_t epsilon)
Definition: L1Penalty.h:87
double float64_t
Definition: common.h:50
The class implements L2 penalty/regularization within the FirstOrderMinimizer framework.
Definition: L2Penalty.h:46
virtual void set_rounding_epsilon(float64_t epsilon)
virtual void update_variable_for_proximity(SGVector< float64_t > variable, float64_t proximal_weight)
Definition: L1Penalty.h:97
virtual void update_context(CMinimizerContext *context)
all of classes and functions are contained in the shogun namespace
Definition: class_list.h:18
virtual float64_t get_sparse_variable(float64_t variable, float64_t penalty_weight)
Definition: L1Penalty.h:128
virtual void set_l1_ratio(float64_t ratio)
virtual float64_t get_penalty(float64_t variable)
Definition: L1Penalty.h:66
virtual void load_from_context(CMinimizerContext *context)
Definition: L2Penalty.h:92

SHOGUN Machine Learning Toolbox - Documentation