SHOGUN
3.2.1
首页
相关页面
模块
类
文件
文件列表
文件成员
全部
类
命名空间
文件
函数
变量
类型定义
枚举
枚举值
友元
宏定义
组
页
src
shogun
machine
KernelMulticlassMachine.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) 2012 Chiyuan Zhang
8
* Written (W) 2012 Heiko Strathmann
9
* Copyright (C) 2012 Chiyuan Zhang
10
*/
11
12
#include <
shogun/lib/Set.h
>
13
#include <
shogun/machine/KernelMulticlassMachine.h
>
14
15
using namespace
shogun;
16
17
void
CKernelMulticlassMachine::store_model_features
()
18
{
19
CKernel
*kernel=
m_kernel
;
20
if
(!kernel)
21
SG_ERROR
(
"%s::store_model_features(): kernel is needed to store SV "
22
"features.\n"
,
get_name
());
23
24
CFeatures
* lhs=kernel->
get_lhs
();
25
CFeatures
* rhs=kernel->
get_rhs
();
26
if
(!lhs)
27
{
28
SG_ERROR
(
"%s::store_model_features(): kernel lhs is needed to store "
29
"SV features.\n"
,
get_name
());
30
}
31
32
/* this map will be abused as a map */
33
CSet<index_t>
all_sv;
34
for
(
index_t
i=0; i<
m_machines
->
get_num_elements
(); ++i)
35
{
36
CKernelMachine
*machine=(
CKernelMachine
*)
get_machine
(i);
37
for
(
index_t
j=0; j<machine->
get_num_support_vectors
(); ++j)
38
all_sv.
add
(machine->
get_support_vector
(j));
39
40
SG_UNREF
(machine);
41
}
42
43
/* convert map to vector of SV */
44
SGVector<index_t>
sv_idx(all_sv.
get_num_elements
());
45
for
(
index_t
i=0; i<sv_idx.vlen; ++i)
46
sv_idx[i]=*all_sv.
get_element_ptr
(i);
47
48
CFeatures
* sv_features=lhs->
copy_subset
(sv_idx);
49
50
/* now, features are replaced by concatenated SV features */
51
kernel->
init
(sv_features, rhs);
52
53
/* was SG_REF'ed by copy_subset */
54
SG_UNREF
(sv_features);
55
56
/* now the old SV indices have to be mapped to the new features */
57
58
/* update SV of all machines */
59
for
(int32_t i=0; i<
m_machines
->
get_num_elements
(); ++i)
60
{
61
CKernelMachine
*machine=(
CKernelMachine
*)
get_machine
(i);
62
63
/* for each machine, replace SV by index in sv_idx array */
64
for
(int32_t j=0; j<machine->
get_num_support_vectors
(); ++j)
65
{
66
/* get index of SV in old features */
67
index_t
current_sv_idx=machine->
get_support_vector
(j);
68
69
/* the position of this old index in the map is the position of
70
* the SV in the new features */
71
index_t
new_sv_idx=all_sv.
index_of
(current_sv_idx);
72
73
machine->
set_support_vector
(j, new_sv_idx);
74
}
75
76
SG_UNREF
(machine);
77
}
78
79
SG_UNREF
(lhs);
80
SG_UNREF
(rhs);
81
}
82
83
CKernelMulticlassMachine::CKernelMulticlassMachine
() :
CMulticlassMachine
(), m_kernel(NULL)
84
{
85
SG_ADD
((
CSGObject
**)&
m_kernel
,
"kernel"
,
"The kernel to be used"
,
MS_AVAILABLE
);
86
}
87
94
CKernelMulticlassMachine::CKernelMulticlassMachine
(
CMulticlassStrategy
*strategy,
CKernel
* kernel,
CKernelMachine
* machine,
CLabels
* labs) :
95
CMulticlassMachine
(strategy,(
CMachine
*)machine,labs), m_kernel(NULL)
96
{
97
set_kernel
(kernel);
98
SG_ADD
((
CSGObject
**)&
m_kernel
,
"kernel"
,
"The kernel to be used"
,
MS_AVAILABLE
);
99
}
100
102
CKernelMulticlassMachine::~CKernelMulticlassMachine
()
103
{
104
SG_UNREF
(
m_kernel
);
105
}
106
111
void
CKernelMulticlassMachine::set_kernel
(
CKernel
* k)
112
{
113
((
CKernelMachine
*)
m_machine
)->set_kernel(k);
114
SG_REF
(k);
115
SG_UNREF
(
m_kernel
);
116
m_kernel
=k;
117
}
118
119
CKernel
*
CKernelMulticlassMachine::get_kernel
()
120
{
121
SG_REF
(
m_kernel
);
122
return
m_kernel
;
123
}
124
125
bool
CKernelMulticlassMachine::init_machine_for_train
(
CFeatures
* data)
126
{
127
if
(data)
128
m_kernel
->
init
(data,data);
129
130
((
CKernelMachine
*)
m_machine
)->set_kernel(
m_kernel
);
131
132
return
true
;
133
}
134
135
bool
CKernelMulticlassMachine::init_machines_for_apply
(
CFeatures
* data)
136
{
137
if
(data)
138
{
139
/* set data to rhs for this kernel */
140
CFeatures
* lhs=
m_kernel
->
get_lhs
();
141
m_kernel
->
init
(lhs, data);
142
SG_UNREF
(lhs);
143
}
144
145
/* set kernel to all sub-machines */
146
for
(int32_t i=0; i<
m_machines
->
get_num_elements
(); i++)
147
{
148
CKernelMachine
*machine=
149
(
CKernelMachine
*)
m_machines
->
get_element
(i);
150
machine->
set_kernel
(
m_kernel
);
151
SG_UNREF
(machine);
152
}
153
154
return
true
;
155
}
156
157
bool
CKernelMulticlassMachine::is_ready
()
158
{
159
if
(
m_kernel
&&
m_kernel
->
get_num_vec_lhs
() &&
m_kernel
->
get_num_vec_rhs
())
160
return
true
;
161
162
return
false
;
163
}
164
165
CMachine
*
CKernelMulticlassMachine::get_machine_from_trained
(
CMachine
* machine)
166
{
167
return
new
CKernelMachine
((
CKernelMachine
*)machine);
168
}
169
170
int32_t
CKernelMulticlassMachine::get_num_rhs_vectors
()
171
{
172
return
m_kernel
->
get_num_vec_rhs
();
173
}
174
175
void
CKernelMulticlassMachine::add_machine_subset
(
SGVector<index_t>
subset)
176
{
177
SG_NOTIMPLEMENTED
178
}
179
180
void
CKernelMulticlassMachine::remove_machine_subset
()
181
{
182
SG_NOTIMPLEMENTED
183
}
184
185
SHOGUN
机器学习工具包 - 项目文档