SHOGUN  v1.1.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
OnlineLinearMachine.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 
12 #include <shogun/base/Parameter.h>
13 
14 using namespace shogun;
15 
17 : CMachine(), w_dim(0), w(NULL), bias(0), features(NULL)
18 {
19  m_parameters->add_vector(&w, &w_dim, "w", "Parameter vector w.");
20  m_parameters->add(&bias, "bias", "Bias b.");
21  m_parameters->add((CSGObject**) &features, "features", "Feature object.");
22 }
23 
25 {
26  // It is possible that a derived class may have already
27  // called SG_FREE() on the weight vector
28  if (w != NULL)
29  SG_FREE(w);
31 }
32 
33 bool COnlineLinearMachine::load(FILE* srcfile)
34 {
37  return false;
38 }
39 
40 bool COnlineLinearMachine::save(FILE* dstfile)
41 {
44  return false;
45 }
46 
48 {
51 
52  DynArray<float64_t>* labels_dynarray=new DynArray<float64_t>();
53  int32_t num_labels=0;
54 
56  while (features->get_next_example())
57  {
58  float64_t current_lab=features->dense_dot(w, w_dim) + bias;
59 
60  labels_dynarray->append_element(current_lab);
61  num_labels++;
62 
64  }
66 
67  SGVector<float64_t> labels_array(num_labels);
68  for (int32_t i=0; i<num_labels; i++)
69  labels_array.vector[i]=(*labels_dynarray)[i];
70 
71  return new CLabels(labels_array);
72 }
73 
75 {
76  if (!data)
77  SG_ERROR("No features specified\n");
78  if (!data->has_property(FP_STREAMING_DOT))
79  SG_ERROR("Specified features are not of type CStreamingDotFeatures\n");
81  return apply();
82 }
83 
85 {
86  return CMath::dot(vec, w, len)+bias;
87 }
88 
90 {
91  return features->dense_dot(w, w_dim)+bias;
92 }
bool has_property(EFeatureProperty p)
Definition: Features.cpp:337
#define SG_RESET_LOCALE
Definition: SGIO.h:67
virtual void start_parser()=0
virtual float32_t dense_dot(const float32_t *vec2, int32_t vec2_len)=0
The class Labels models labels, i.e. class assignments of objects.
Definition: Labels.h:35
virtual bool save(FILE *dstfile)
#define SG_ERROR(...)
Definition: SGIO.h:75
Parameter * m_parameters
Definition: SGObject.h:297
#define SG_SET_LOCALE_C
Definition: SGIO.h:66
A generic learning machine interface.
Definition: Machine.h:96
virtual void set_features(CStreamingDotFeatures *feat)
void add(bool *param, const char *name, const char *description="")
Definition: Parameter.cpp:23
virtual bool get_next_example()=0
#define ASSERT(x)
Definition: SGIO.h:102
Class SGObject is the base class of all shogun objects.
Definition: SGObject.h:76
virtual float32_t apply_to_current_example()
double float64_t
Definition: common.h:56
static float64_t dot(const bool *v1, const bool *v2, int32_t n)
compute dot product between v1 and v2 (blas optimized)
Definition: Math.h:668
Streaming features that support dot products among other operations.
#define SG_FREE(ptr)
Definition: memory.h:39
float float32_t
Definition: common.h:55
#define SG_UNREF(x)
Definition: SGObject.h:45
void add_vector(bool **param, index_t *length, const char *name, const char *description="")
Definition: Parameter.cpp:306
virtual void end_parser()=0
CStreamingDotFeatures * features
The class Features is the base class of all feature objects.
Definition: Features.h:56
virtual bool load(FILE *srcfile)
virtual void release_example()=0

SHOGUN Machine Learning Toolbox - Documentation