SHOGUN  4.1.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>
15 
16 using namespace shogun;
17 
19 : CMachine(), bias(0), features(NULL)
20 {
21  init();
22 }
23 
25  bias(0), features(NULL)
26 {
27  set_w(machine->get_w().clone());
28  set_bias(machine->get_bias());
29 
30  init();
31 }
32 
33 void CLinearMachine::init()
34 {
35  SG_ADD(&w, "w", "Parameter vector w.", MS_NOT_AVAILABLE);
36  SG_ADD(&bias, "bias", "Bias b.", MS_NOT_AVAILABLE);
37  SG_ADD((CSGObject**) &features, "features", "Feature object.",
39 }
40 
41 
43 {
45 }
46 
48 {
49  return features->dense_dot(vec_idx, w.vector, w.vlen) + bias;
50 }
51 
53 {
54  SGVector<float64_t> outputs = apply_get_outputs(data);
55  return new CRegressionLabels(outputs);
56 }
57 
59 {
60  SGVector<float64_t> outputs = apply_get_outputs(data);
61  return new CBinaryLabels(outputs);
62 }
63 
65 {
66  if (data)
67  {
68  if (!data->has_property(FP_DOT))
69  SG_ERROR("Specified features are not of type CDotFeatures\n")
70 
71  set_features((CDotFeatures*) data);
72  }
73 
74  if (!features)
75  return SGVector<float64_t>();
76 
77  int32_t num=features->get_num_vectors();
78  ASSERT(num>0)
80 
81  float64_t* out=SG_MALLOC(float64_t, num);
82  features->dense_dot_range(out, 0, num, NULL, w.vector, w.vlen, bias);
83  return SGVector<float64_t>(out,num);
84 }
85 
87 {
88  return w;
89 }
90 
92 {
93  w=src_w;
94 }
95 
97 {
98  bias=b;
99 }
100 
102 {
103  return bias;
104 }
105 
107 {
108  SG_REF(feat);
110  features=feat;
111 }
112 
114 {
115  SG_REF(features);
116  return features;
117 }
118 
120 {
121 }
122 
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 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_vectors() const =0
#define SG_ERROR(...)
Definition: SGIO.h:129
virtual CDotFeatures * get_features()
Features that support dot products among other operations.
Definition: DotFeatures.h:44
#define SG_REF(x)
Definition: SGObject.h:51
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:112
double float64_t
Definition: common.h:50
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
CDotFeatures * features
#define SG_UNREF(x)
Definition: SGObject.h:52
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:209
Binary Labels for binary classification.
Definition: BinaryLabels.h:37
virtual void set_bias(float64_t b)
#define SG_ADD(...)
Definition: SGObject.h:81
bool has_property(EFeatureProperty p) const
Definition: Features.cpp:295

SHOGUN Machine Learning Toolbox - Documentation