SHOGUN
3.2.1
首页
相关页面
模块
类
文件
文件列表
文件成员
全部
类
命名空间
文件
函数
变量
类型定义
枚举
枚举值
友元
宏定义
组
页
src
shogun
regression
LinearRidgeRegression.cpp
浏览该文件的文档.
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
* Copyright (C) 2012 Soeren Sonnenburg
8
*/
9
10
#include <
shogun/lib/config.h
>
11
12
#ifdef HAVE_LAPACK
13
#include <
shogun/regression/LinearRidgeRegression.h
>
14
#include <
shogun/mathematics/lapack.h
>
15
#include <
shogun/mathematics/Math.h
>
16
#include <
shogun/labels/RegressionLabels.h
>
17
18
using namespace
shogun;
19
20
CLinearRidgeRegression::CLinearRidgeRegression
()
21
:
CLinearMachine
()
22
{
23
init();
24
}
25
26
CLinearRidgeRegression::CLinearRidgeRegression
(
float64_t
tau,
CDenseFeatures<float64_t>
* data,
CLabels
* lab)
27
:
CLinearMachine
()
28
{
29
init();
30
31
m_tau
=tau;
32
set_labels
(lab);
33
set_features
(data);
34
}
35
36
void
CLinearRidgeRegression::init()
37
{
38
m_tau
=1e-6;
39
40
SG_ADD
(&
m_tau
,
"tau"
,
"Regularization parameter"
,
MS_AVAILABLE
);
41
}
42
43
bool
CLinearRidgeRegression::train_machine
(
CFeatures
* data)
44
{
45
if
(!
m_labels
)
46
SG_ERROR
(
"No labels set\n"
)
47
48
if
(!data)
49
data=
features
;
50
51
if
(!data)
52
SG_ERROR
(
"No features set\n"
)
53
54
if
(
m_labels
->
get_num_labels
() != data->
get_num_vectors
())
55
SG_ERROR
(
"Number of training vectors does not match number of labels\n"
)
56
57
if
(data->
get_feature_class
() !=
C_DENSE
)
58
SG_ERROR
(
"Expected Dense Features\n"
)
59
60
if
(data->
get_feature_type
() !=
F_DREAL
)
61
SG_ERROR
(
"Expected Real Features\n"
)
62
63
CDenseFeatures<float64_t>
* feats=(
CDenseFeatures<float64_t>
*) data;
64
int32_t num_feat=feats->
get_num_features
();
65
int32_t num_vec=feats->
get_num_vectors
();
66
67
// Get kernel matrix
68
SGMatrix<float64_t>
kernel_matrix(num_feat,num_feat);
69
SGVector<float64_t>
y(num_feat);
70
71
// init
72
kernel_matrix.
zero
();
73
y.
zero
();
74
75
for
(int32_t i=0; i<num_feat; i++)
76
kernel_matrix.
matrix
[i+i*num_feat]+=
m_tau
;
77
78
for (int32_t i=0; i<num_vec; i++)
79
{
80
SGVector<float64_t>
v = feats->
get_feature_vector
(i);
81
ASSERT
(v.
vlen
==num_feat)
82
83
cblas_dger(CblasColMajor, num_feat,num_feat, 1.0, v.
vector
,1,
84
v.
vector
,1, kernel_matrix.
matrix
, num_feat);
85
86
cblas_daxpy(num_feat, ((
CRegressionLabels
*)
m_labels
)->get_label(i), v.
vector
, 1, y.
vector
, 1);
87
88
feats->
free_feature_vector
(v, i);
89
}
90
91
clapack_dposv(CblasRowMajor,CblasUpper, num_feat, 1, kernel_matrix.
matrix
, num_feat,
92
y.
vector
, num_feat);
93
94
set_w
(y);
95
96
return
true
;
97
}
98
99
bool
CLinearRidgeRegression::load
(FILE* srcfile)
100
{
101
SG_SET_LOCALE_C
;
102
SG_RESET_LOCALE
;
103
return
false
;
104
}
105
106
bool
CLinearRidgeRegression::save
(FILE* dstfile)
107
{
108
SG_SET_LOCALE_C
;
109
SG_RESET_LOCALE
;
110
return
false
;
111
}
112
#endif
SHOGUN
机器学习工具包 - 项目文档