SHOGUN  3.2.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
DualVariationalGaussianLikelihood.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) The Shogun Machine Learning Toolbox
3  * Written (w) 2014 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 #ifdef HAVE_EIGEN3
33 
39 
40 using namespace Eigen;
41 
42 namespace shogun
43 {
44 
45 CDualVariationalGaussianLikelihood::CDualVariationalGaussianLikelihood()
47 {
48  init();
49 }
50 
52 {
53 }
54 
56 {
57  REQUIRE(m_likelihood, "The likelihood model must not be NULL\n");
59  REQUIRE(var_lik,
60  "The likelihood model (%s) does NOT support variational guassian inference\n",
62 
63  return var_lik;
64 }
65 
67 {
69  return var_lik->get_variational_expection();
70 }
71 
73 {
75  return var_lik->get_variational_first_derivative(param);
76 }
77 
79 {
82 }
83 
85 {
87  return var_lik->get_first_derivative_wrt_hyperparameter(param);
88 }
89 
92 {
94  var_lik->set_variational_distribution(mu, s2, lab);
95 }
96 
98 {
99  REQUIRE((strict_scale>0 && strict_scale<1),
100  "The strict_scale (%f) should be between 0 and 1 exclusively.\n",
101  strict_scale);
102  m_strict_scale=strict_scale;
103 }
104 
106 {
107  REQUIRE(direction.vlen==m_lambda.vlen,
108  "The length (%d) of direction should be same as the length (%d) of dual parameters\n",
109  direction.vlen, m_lambda.vlen);
110 
111  REQUIRE(step>=0,
112  "The step size (%f) should be non-negative\n", step);
113 
114  float64_t upper_bound=get_dual_upper_bound();
115  float64_t lower_bound=get_dual_lower_bound();
116 
117  ASSERT(upper_bound>=lower_bound);
118 
119  float64_t min_step=step;
120 
121  for (index_t i=0; i<direction.vlen; i++)
122  {
123  float64_t attempt=m_lambda[i]+step*direction[i];
124  float64_t adjust=0;
125 
126  if (direction[i]==0.0)
127  continue;
128 
129  if (lower_bound!=-CMath::INFTY && attempt<lower_bound)
130  {
131  adjust=(m_lambda[i]-lower_bound)/CMath::abs(direction[i]);
133  adjust*=(1-m_strict_scale);
134  if (adjust<min_step)
135  min_step=adjust;
136  }
137 
138  if (upper_bound!=CMath::INFTY && attempt>upper_bound)
139  {
140  adjust=(upper_bound-m_lambda[i])/CMath::abs(direction[i]);
142  adjust*=(1-m_strict_scale);
143  if (adjust<min_step)
144  min_step=adjust;
145  }
146  }
147 
148  return min_step;
149 }
150 
152 {
153  REQUIRE(lab, "Labels are required (lab should not be NULL)\n");
154 
155  REQUIRE((lambda.vlen==lab->get_num_labels()),
156  "Length of the vector of lambda (%d) "
157  "and number of labels (%d) should be the same\n",
158  lambda.vlen, lab->get_num_labels());
160  "Labels (%s) must be type of CBinaryLabels\n",
161  lab->get_name());
162 
163  m_lab=(((CBinaryLabels*)lab)->get_labels()).clone();
164 
165  //Convert the input label to standard label used in the class
166  //Note that Shogun uses -1 and 1 as labels and this class internally uses
167  //0 and 1 repectively.
168  for(index_t i = 0; i < m_lab.size(); ++i)
169  m_lab[i]=CMath::max(m_lab[i], 0.0);
170 
171  m_lambda=lambda;
172 
173  precompute();
174 }
175 
177 {
178  float64_t lower_bound=get_dual_lower_bound();
179  float64_t upper_bound=get_dual_upper_bound();
180 
181  for (index_t i=0; i<m_lambda.vlen; i++)
182  {
183  float64_t value=m_lambda[i];
184  if (value<lower_bound)
185  return false;
186  else
187  {
188  if (dual_lower_bound_strict() && value==lower_bound)
189  return false;
190  else
191  {
192  if (value>upper_bound)
193  return false;
194  else
195  {
196  if (dual_upper_bound_strict() && value==upper_bound)
197  return false;
198 
199  }
200  }
201  }
202 
203  }
204  return true;
205 }
206 
208 {
210 }
211 
212 void CDualVariationalGaussianLikelihood::init()
213 {
214  SG_ADD(&m_lambda, "lambda",
215  "Dual parameter for variational s2",
217 
218  SG_ADD(&m_is_valid, "is_valid",
219  "Is the Dual parameter valid",
221 
222  SG_ADD(&m_strict_scale, "strict_scale",
223  "The strict variable used in adjust_step_wrt_dual_parameter",
225 
226  m_is_valid=false;
227  m_strict_scale=1e-5;
228 }
229 
230 } /* namespace shogun */
231 #endif /* HAVE_EIGEN3 */
virtual const char * get_name() const =0
virtual SGVector< float64_t > get_first_derivative_wrt_hyperparameter(const TParameter *param) const
virtual ELabelType get_label_type() const =0
binary labels +1/-1
Definition: LabelTypes.h:18
virtual SGVector< float64_t > get_first_derivative_wrt_hyperparameter(const TParameter *param) const =0
virtual SGVector< float64_t > get_variational_first_derivative(const TParameter *param) const =0
int32_t index_t
Definition: common.h:62
The class Labels models labels, i.e. class assignments of objects.
Definition: Labels.h:43
static const float64_t INFTY
infinity
Definition: Math.h:1415
virtual CSGObject * clone()
Definition: SGObject.cpp:1302
virtual int32_t get_num_labels() const =0
virtual bool dual_lower_bound_strict() const =0
The variational Gaussian Likelihood base class. The variational distribution is Gaussian.
parameter struct
Definition: Parameter.h:32
#define REQUIRE(x,...)
Definition: SGIO.h:207
virtual float64_t get_dual_upper_bound() const =0
virtual void set_variational_distribution(SGVector< float64_t > mu, SGVector< float64_t > s2, const CLabels *lab)
virtual float64_t adjust_step_wrt_dual_parameter(SGVector< float64_t > direction, const float64_t step) const
#define ASSERT(x)
Definition: SGIO.h:202
double float64_t
Definition: common.h:50
virtual SGVector< float64_t > get_variational_expection()=0
static T max(T a, T b)
return the maximum of two integers
Definition: Math.h:162
virtual CVariationalGaussianLikelihood * get_variational_likelihood() const
virtual float64_t get_dual_lower_bound() const =0
virtual void set_dual_parameters(SGVector< float64_t > lambda, const CLabels *lab)
The Variational Likelihood base class.
Binary Labels for binary classification.
Definition: BinaryLabels.h:37
virtual void set_variational_distribution(SGVector< float64_t > mu, SGVector< float64_t > s2, const CLabels *lab)
virtual bool dual_upper_bound_strict() const =0
#define SG_ADD(...)
Definition: SGObject.h:67
virtual bool supports_derivative_wrt_hyperparameter() const =0
virtual SGVector< float64_t > get_variational_first_derivative(const TParameter *param) const
index_t vlen
Definition: SGVector.h:707
static T abs(T a)
return the absolute value of a number
Definition: Math.h:181

SHOGUN Machine Learning Toolbox - Documentation