SHOGUN
v3.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
delete
labels_dynarray;
79
return
labels_array;
80
}
81
82
float32_t
COnlineLinearMachine::apply_one
(
float32_t
* vec, int32_t len)
83
{
84
return
SGVector<float32_t>::dot
(vec,
w
, len)+
bias
;
85
}
86
87
float32_t
COnlineLinearMachine::apply_to_current_example
()
88
{
89
return
features
->
dense_dot
(
w
,
w_dim
)+
bias
;
90
}
91
92
bool
COnlineLinearMachine::train_machine
(
CFeatures
*data)
93
{
94
if
(data)
95
{
96
if
(!data->
has_property
(
FP_STREAMING_DOT
))
97
SG_ERROR
(
"Specified features are not of type CStreamingDotFeatures\n"
)
98
set_features
((
CStreamingDotFeatures
*) data);
99
}
100
start_train
();
101
features
->
start_parser
();
102
while
(
features
->
get_next_example
())
103
{
104
train_example
(
features
,
features
->
get_label
());
105
features
->
release_example
();
106
}
107
108
features
->
end_parser
();
109
stop_train
();
110
111
return
true
;
112
}
SHOGUN
Machine Learning Toolbox - Documentation