SHOGUN
v1.1.0
Main Page
Related Pages
Classes
Files
File List
File Members
All
Classes
Namespaces
Files
Functions
Variables
Typedefs
Enumerations
Enumerator
Friends
Macros
Pages
src
shogun
machine
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
11
#include <
shogun/machine/OnlineLinearMachine.h
>
12
#include <
shogun/base/Parameter.h
>
13
14
using namespace
shogun;
15
16
COnlineLinearMachine::COnlineLinearMachine
()
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
24
COnlineLinearMachine::~COnlineLinearMachine
()
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
);
30
SG_UNREF
(
features
);
31
}
32
33
bool
COnlineLinearMachine::load
(FILE* srcfile)
34
{
35
SG_SET_LOCALE_C
;
36
SG_RESET_LOCALE
;
37
return
false
;
38
}
39
40
bool
COnlineLinearMachine::save
(FILE* dstfile)
41
{
42
SG_SET_LOCALE_C
;
43
SG_RESET_LOCALE
;
44
return
false
;
45
}
46
47
CLabels
*
COnlineLinearMachine::apply
()
48
{
49
ASSERT
(
features
);
50
ASSERT
(
features
->
has_property
(
FP_STREAMING_DOT
));
51
52
DynArray<float64_t>
* labels_dynarray=
new
DynArray<float64_t>
();
53
int32_t num_labels=0;
54
55
features
->
start_parser
();
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
63
features
->
release_example
();
64
}
65
features
->
end_parser
();
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
74
CLabels
*
COnlineLinearMachine::apply
(
CFeatures
* data)
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"
);
80
set_features
((
CStreamingDotFeatures
*) data);
81
return
apply
();
82
}
83
84
float32_t
COnlineLinearMachine::apply
(
float32_t
* vec, int32_t len)
85
{
86
return
CMath::dot
(vec,
w
, len)+
bias
;
87
}
88
89
float32_t
COnlineLinearMachine::apply_to_current_example
()
90
{
91
return
features
->
dense_dot
(
w
,
w_dim
)+
bias
;
92
}
SHOGUN
Machine Learning Toolbox - Documentation