SHOGUN
v2.0.0
Main Page
Related Pages
Modules
Classes
Files
File List
File Members
All
Classes
Namespaces
Files
Functions
Variables
Typedefs
Enumerations
Enumerator
Friends
Macros
Groups
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
SG_ADD
(&
bias
,
"bias"
,
"Bias b."
,
MS_NOT_AVAILABLE
);
21
SG_ADD
((
CSGObject
**) &
features
,
"features"
,
22
"Feature object."
,
MS_NOT_AVAILABLE
);
23
}
24
25
COnlineLinearMachine::~COnlineLinearMachine
()
26
{
27
// It is possible that a derived class may have already
28
// called SG_FREE() on the weight vector
29
if
(
w
!= NULL)
30
SG_FREE
(
w
);
31
SG_UNREF
(
features
);
32
}
33
34
CBinaryLabels
*
COnlineLinearMachine::apply_binary
(
CFeatures
* data)
35
{
36
SGVector<float64_t>
outputs =
apply_get_outputs
(data);
37
return
new
CBinaryLabels
(outputs);
38
}
39
40
CRegressionLabels
*
COnlineLinearMachine::apply_regression
(
CFeatures
* data)
41
{
42
SGVector<float64_t>
outputs =
apply_get_outputs
(data);
43
return
new
CRegressionLabels
(outputs);
44
}
45
46
SGVector<float64_t>
COnlineLinearMachine::apply_get_outputs
(
CFeatures
* data)
47
{
48
if
(data)
49
{
50
if
(!data->
has_property
(
FP_STREAMING_DOT
))
51
SG_ERROR
(
"Specified features are not of type CStreamingDotFeatures\n"
);
52
53
set_features
((
CStreamingDotFeatures
*) data);
54
}
55
56
ASSERT
(
features
);
57
ASSERT
(
features
->
has_property
(
FP_STREAMING_DOT
));
58
59
DynArray<float64_t>
* labels_dynarray=
new
DynArray<float64_t>
();
60
int32_t num_labels=0;
61
62
features
->
start_parser
();
63
while
(
features
->
get_next_example
())
64
{
65
float64_t
current_lab=
features
->
dense_dot
(
w
,
w_dim
) +
bias
;
66
67
labels_dynarray->append_element(current_lab);
68
num_labels++;
69
70
features
->
release_example
();
71
}
72
features
->
end_parser
();
73
74
SGVector<float64_t>
labels_array(num_labels);
75
for
(int32_t i=0; i<num_labels; i++)
76
labels_array.
vector
[i]=(*labels_dynarray)[i];
77
78
return
labels_array;
79
}
80
81
float32_t
COnlineLinearMachine::apply_one
(
float32_t
* vec, int32_t len)
82
{
83
return
SGVector<float32_t>::dot
(vec,
w
, len)+
bias
;
84
}
85
86
float32_t
COnlineLinearMachine::apply_to_current_example
()
87
{
88
return
features
->
dense_dot
(
w
,
w_dim
)+
bias
;
89
}
90
91
bool
COnlineLinearMachine::train_machine
(
CFeatures
*data)
92
{
93
if
(data)
94
{
95
if
(!data->
has_property
(
FP_STREAMING_DOT
))
96
SG_ERROR
(
"Specified features are not of type CStreamingDotFeatures\n"
);
97
set_features
((
CStreamingDotFeatures
*) data);
98
}
99
start_train
();
100
features
->
start_parser
();
101
while
(
features
->
get_next_example
())
102
{
103
train_example
(
features
,
features
->
get_label
());
104
features
->
release_example
();
105
}
106
107
features
->
end_parser
();
108
stop_train
();
109
110
return
true
;
111
}
SHOGUN
Machine Learning Toolbox - Documentation