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
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
11
#include <
shogun/machine/LinearMachine.h
>
12
#include <
shogun/labels/RegressionLabels.h
>
13
#include <
shogun/base/Parameter.h
>
14
15
using namespace
shogun;
16
17
CLinearMachine::CLinearMachine
()
18
:
CMachine
(), bias(0), features(NULL)
19
{
20
init();
21
}
22
23
CLinearMachine::CLinearMachine
(
CLinearMachine
* machine) :
CMachine
(),
24
bias(0), features(NULL)
25
{
26
set_w
(machine->
get_w
().
clone
());
27
set_bias
(machine->
get_bias
());
28
29
init();
30
}
31
32
void
CLinearMachine::init()
33
{
34
SG_ADD
(&
w
,
"w"
,
"Parameter vector w."
,
MS_NOT_AVAILABLE
);
35
SG_ADD
(&
bias
,
"bias"
,
"Bias b."
,
MS_NOT_AVAILABLE
);
36
SG_ADD
((
CSGObject
**) &
features
,
"features"
,
"Feature object."
,
37
MS_NOT_AVAILABLE
);
38
}
39
40
41
CLinearMachine::~CLinearMachine
()
42
{
43
SG_UNREF
(
features
);
44
}
45
46
float64_t
CLinearMachine::apply_one
(int32_t vec_idx)
47
{
48
return
features
->
dense_dot
(vec_idx,
w
.
vector
,
w
.
vlen
) +
bias
;
49
}
50
51
CRegressionLabels
*
CLinearMachine::apply_regression
(
CFeatures
* data)
52
{
53
SGVector<float64_t>
outputs =
apply_get_outputs
(data);
54
return
new
CRegressionLabels
(outputs);
55
}
56
57
CBinaryLabels
*
CLinearMachine::apply_binary
(
CFeatures
* data)
58
{
59
SGVector<float64_t>
outputs =
apply_get_outputs
(data);
60
return
new
CBinaryLabels
(outputs);
61
}
62
63
SGVector<float64_t>
CLinearMachine::apply_get_outputs
(
CFeatures
* data)
64
{
65
if
(data)
66
{
67
if
(!data->
has_property
(
FP_DOT
))
68
SG_ERROR
(
"Specified features are not of type CDotFeatures\n"
);
69
70
set_features
((
CDotFeatures
*) data);
71
}
72
73
if
(!
features
)
74
return
SGVector<float64_t>
();
75
76
int32_t num=
features
->
get_num_vectors
();
77
ASSERT
(num>0);
78
ASSERT
(
w
.
vlen
==
features
->
get_dim_feature_space
());
79
80
float64_t
* out=
SG_MALLOC
(
float64_t
, num);
81
features
->
dense_dot_range
(out, 0, num, NULL,
w
.
vector
,
w
.
vlen
,
bias
);
82
return
SGVector<float64_t>
(out,num);
83
}
SHOGUN
Machine Learning Toolbox - Documentation