SHOGUN
3.2.1
首页
相关页面
模块
类
文件
文件列表
文件成员
全部
类
命名空间
文件
函数
变量
类型定义
枚举
枚举值
友元
宏定义
组
页
src
shogun
kernel
LinearKernel.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
* Written (W) 1999-2010 Soeren Sonnenburg
8
* Copyright (C) 1999-2009 Fraunhofer Institute FIRST and Max-Planck-Society
9
* Copyright (C) 2010 Berlin Institute of Technology
10
*/
11
12
#include <
shogun/lib/common.h
>
13
#include <
shogun/io/SGIO.h
>
14
#include <
shogun/features/Features.h
>
15
#include <
shogun/features/DotFeatures.h
>
16
#include <
shogun/kernel/LinearKernel.h
>
17
18
using namespace
shogun;
19
20
CLinearKernel::CLinearKernel
()
21
:
CDotKernel
(0)
22
{
23
properties
|=
KP_LINADD
;
24
}
25
26
CLinearKernel::CLinearKernel
(
CDotFeatures
* l,
CDotFeatures
* r)
27
:
CDotKernel
(0)
28
{
29
properties
|=
KP_LINADD
;
30
init
(l,r);
31
}
32
33
CLinearKernel::~CLinearKernel
()
34
{
35
cleanup
();
36
}
37
38
bool
CLinearKernel::init(
CFeatures
* l,
CFeatures
* r)
39
{
40
CDotKernel::init(l, r);
41
42
return
init_normalizer
();
43
}
44
45
void
CLinearKernel::cleanup
()
46
{
47
delete_optimization
();
48
49
CKernel::cleanup
();
50
}
51
52
void
CLinearKernel::add_to_normal
(int32_t idx,
float64_t
weight)
53
{
54
((
CDotFeatures
*)
lhs
)->add_to_dense_vec(
55
normalizer
->
normalize_lhs
(weight, idx), idx,
normal
.
vector
,
normal
.
size
());
56
set_is_initialized
(
true
);
57
}
58
59
bool
CLinearKernel::init_optimization
(
60
int32_t num_suppvec, int32_t* sv_idx,
float64_t
* alphas)
61
{
62
clear_normal
();
63
64
for
(int32_t i=0; i<num_suppvec; i++)
65
add_to_normal
(sv_idx[i], alphas[i]);
66
67
set_is_initialized
(
true
);
68
return
true
;
69
}
70
71
bool
CLinearKernel::init_optimization
(
CKernelMachine
* km)
72
{
73
clear_normal
();
74
75
int32_t num_suppvec=km->
get_num_support_vectors
();
76
77
for
(int32_t i=0; i<num_suppvec; i++)
78
add_to_normal
(km->
get_support_vector
(i), km->
get_alpha
(i));
79
80
set_is_initialized
(
true
);
81
return
true
;
82
}
83
84
bool
CLinearKernel::delete_optimization
()
85
{
86
normal
=
SGVector<float64_t>
();
87
set_is_initialized
(
false
);
88
89
return
true
;
90
}
91
92
float64_t
CLinearKernel::compute_optimized
(int32_t idx)
93
{
94
ASSERT
(
get_is_initialized
())
95
float64_t
result = ((
CDotFeatures
*)
rhs
)->
96
dense_dot(idx,
normal
.
vector
,
normal
.
size
());
97
return
normalizer
->
normalize_rhs
(result, idx);
98
}
SHOGUN
机器学习工具包 - 项目文档