SHOGUN  4.2.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
LinearMachine.cpp
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) 1999-2009 Soeren Sonnenburg
8  * Copyright (C) 1999-2009 Fraunhofer Institute FIRST and Max-Planck-Society
9  */
10 
14 #include <shogun/labels/Labels.h>
16 
17 using namespace shogun;
18 using namespace Eigen;
19 
21 {
22  init();
23 }
24 
26 {
27  init();
28 
29  set_compute_bias(comput_bias);
30 }
31 
33 {
34  init();
35 
36  set_w(machine->get_w().clone());
37  set_bias(machine->get_bias());
39 }
40 
41 void CLinearMachine::init()
42 {
43  bias = 0;
44  features = NULL;
45  m_compute_bias = true;
46 
47  SG_ADD(&w, "w", "Parameter vector w.", MS_NOT_AVAILABLE);
48  SG_ADD(&bias, "bias", "Bias b.", MS_NOT_AVAILABLE);
49  SG_ADD((CSGObject**) &features, "features", "Feature object.",
51 }
52 
53 
55 {
57 }
58 
60 {
61  return features->dense_dot(vec_idx, w.vector, w.vlen) + bias;
62 }
63 
65 {
66  SGVector<float64_t> outputs = apply_get_outputs(data);
67  return new CRegressionLabels(outputs);
68 }
69 
71 {
72  SGVector<float64_t> outputs = apply_get_outputs(data);
73  return new CBinaryLabels(outputs);
74 }
75 
77 {
78  if (data)
79  {
80  if (!data->has_property(FP_DOT))
81  SG_ERROR("Specified features are not of type CDotFeatures\n")
82 
83  set_features((CDotFeatures*) data);
84  }
85 
86  if (!features)
87  return SGVector<float64_t>();
88 
89  int32_t num=features->get_num_vectors();
90  ASSERT(num>0)
92 
93  float64_t* out=SG_MALLOC(float64_t, num);
94  features->dense_dot_range(out, 0, num, NULL, w.vector, w.vlen, bias);
95  return SGVector<float64_t>(out,num);
96 }
97 
99 {
100  return w;
101 }
102 
104 {
105  w=src_w;
106 }
107 
109 {
110  bias=b;
111 }
112 
114 {
115  return bias;
116 }
117 
118 void CLinearMachine::set_compute_bias(bool comput_bias)
119 {
120  m_compute_bias = comput_bias;
121 }
122 
124 {
125  return m_compute_bias;
126 }
127 
129 {
130  SG_REF(feat);
132  features=feat;
133 }
134 
136 {
137  SG_REF(features);
138  return features;
139 }
140 
142 {
143 }
144 
146 {
147  REQUIRE(m_labels,"No labels set\n");
148 
149  if (!data)
150  data=features;
151 
152  REQUIRE(data,"No features provided and no featured previously set\n");
153 
155  "Number of training vectors (%d) does not match number of labels (%d)\n",
157 
158  SGVector<float64_t> outputs = apply_get_outputs(data);
159 
160  int32_t num_vec=data->get_num_vectors();
161 
162  Map<VectorXd> eigen_outputs(outputs,num_vec);
163  Map<VectorXd> eigen_labels(((CRegressionLabels*)m_labels)->get_labels(),num_vec);
164 
165  set_bias((eigen_labels - eigen_outputs).mean()) ;
166 }
167 
168 
170 {
171  /* not allowed to train on locked data */
172  if (m_data_locked)
173  {
174  SG_ERROR("train data_lock() was called, only train_locked() is"
175  " possible. Call data_unlock if you want to call train()\n",
176  get_name());
177  }
178 
179  if (train_require_labels())
180  {
181  REQUIRE(m_labels,"No labels given",this->get_name());
182 
184  }
185 
186  bool result = train_machine(data);
187 
188  if(m_compute_bias)
189  compute_bias(data);
190 
191  return result;
192 }
virtual CBinaryLabels * apply_binary(CFeatures *data=NULL)
virtual void dense_dot_range(float64_t *output, int32_t start, int32_t stop, float64_t *alphas, float64_t *vec, int32_t dim, float64_t b)
Definition: DotFeatures.cpp:67
virtual bool train(CFeatures *data=NULL)
virtual SGVector< float64_t > apply_get_outputs(CFeatures *data)
Real Labels are real-valued labels.
virtual CRegressionLabels * apply_regression(CFeatures *data=NULL)
virtual void set_w(const SGVector< float64_t > src_w)
virtual float64_t dense_dot(int32_t vec_idx1, const float64_t *vec2, int32_t vec2_len)=0
virtual int32_t get_num_labels() const =0
Definition: SGMatrix.h:20
virtual int32_t get_num_vectors() const =0
CLabels * m_labels
Definition: Machine.h:361
#define SG_ERROR(...)
Definition: SGIO.h:129
#define REQUIRE(x,...)
Definition: SGIO.h:206
bool m_data_locked
Definition: Machine.h:370
virtual bool train_machine(CFeatures *data=NULL)
Definition: Machine.h:318
virtual CDotFeatures * get_features()
Features that support dot products among other operations.
Definition: DotFeatures.h:44
#define SG_REF(x)
Definition: SGObject.h:54
A generic learning machine interface.
Definition: Machine.h:143
virtual int32_t get_dim_feature_space() const =0
virtual float64_t apply_one(int32_t vec_idx)
index_t vlen
Definition: SGVector.h:494
#define ASSERT(x)
Definition: SGIO.h:201
Class SGObject is the base class of all shogun objects.
Definition: SGObject.h:115
double float64_t
Definition: common.h:50
virtual const char * get_name() const
virtual CLabels * get_labels()
Definition: Machine.cpp:76
virtual bool get_compute_bias()
SGVector< float64_t > w
virtual void set_features(CDotFeatures *feat)
Class LinearMachine is a generic interface for all kinds of linear machines like classifiers.
Definition: LinearMachine.h:63
virtual void store_model_features()
virtual SGVector< float64_t > get_w() const
virtual void set_compute_bias(bool compute_bias)
CDotFeatures * features
#define SG_UNREF(x)
Definition: SGObject.h:55
all of classes and functions are contained in the shogun namespace
Definition: class_list.h:18
The class Features is the base class of all feature objects.
Definition: Features.h:68
virtual float64_t get_bias()
SGVector< T > clone() const
Definition: SGVector.cpp:207
Binary Labels for binary classification.
Definition: BinaryLabels.h:37
virtual void set_bias(float64_t b)
virtual bool train_require_labels() const
Definition: Machine.h:354
#define SG_ADD(...)
Definition: SGObject.h:84
bool has_property(EFeatureProperty p) const
Definition: Features.cpp:295
virtual void ensure_valid(const char *context=NULL)=0
void compute_bias(CFeatures *data)

SHOGUN Machine Learning Toolbox - Documentation