SHOGUN
3.2.1
首页
相关页面
模块
类
文件
文件列表
文件成员
全部
类
命名空间
文件
函数
变量
类型定义
枚举
枚举值
友元
宏定义
组
页
src
shogun
multiclass
ecoc
ECOCStrategy.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
* Copyright (C) 2012 Chiyuan Zhang
9
*/
10
11
#include <
shogun/multiclass/ecoc/ECOCStrategy.h
>
12
#include <
shogun/labels/BinaryLabels.h
>
13
#include <
shogun/labels/MulticlassLabels.h
>
14
15
using namespace
shogun;
16
17
CECOCStrategy::CECOCStrategy
() :
CMulticlassStrategy
()
18
{
19
init();
20
}
21
22
CECOCStrategy::CECOCStrategy
(
CECOCEncoder
*encoder,
CECOCDecoder
*decoder)
23
:
CMulticlassStrategy
()
24
{
25
init();
26
m_encoder
=encoder;
27
m_decoder
=decoder;
28
SG_REF
(
m_encoder
);
29
SG_REF
(decoder);
30
}
31
32
void
CECOCStrategy::init()
33
{
34
m_encoder
=NULL;
35
m_decoder
=NULL;
36
37
SG_ADD
((
CSGObject
**)&
m_encoder
,
"encoder"
,
"ECOC Encoder"
,
MS_NOT_AVAILABLE
);
38
SG_ADD
((
CSGObject
**)&
m_decoder
,
"decoder"
,
"ECOC Decoder"
,
MS_NOT_AVAILABLE
);
39
}
40
41
CECOCStrategy::~CECOCStrategy
()
42
{
43
SG_UNREF
(
m_encoder
);
44
SG_UNREF
(
m_decoder
);
45
}
46
47
void
CECOCStrategy::train_start
(
CMulticlassLabels
*orig_labels,
CBinaryLabels
*train_labels)
48
{
49
CMulticlassStrategy::train_start
(orig_labels, train_labels);
50
51
m_codebook
=
m_encoder
->
create_codebook
(
m_num_classes
);
52
}
53
54
bool
CECOCStrategy::train_has_more
()
55
{
56
return
m_train_iter
<
m_codebook
.
num_rows
;
57
}
58
59
SGVector<int32_t>
CECOCStrategy::train_prepare_next
()
60
{
61
SGVector<int32_t>
subset(
m_orig_labels
->
get_num_labels
(),
false
);
62
int32_t tot=0;
63
for
(int32_t i=0; i <
m_orig_labels
->
get_num_labels
(); ++i)
64
{
65
int32_t label = ((
CMulticlassLabels
*)
m_orig_labels
)->get_int_label(i);
66
switch
(
m_codebook
(
m_train_iter
, label))
67
{
68
case
-1:
69
((
CBinaryLabels
*)
m_train_labels
)->set_label(i, -1);
70
subset[tot++]=i;
71
break
;
72
case
1:
73
((
CBinaryLabels
*) m_train_labels)->set_label(i, 1);
74
subset[tot++]=i;
75
break
;
76
default
:
77
// 0 means ignore
78
break
;
79
}
80
}
81
82
CMulticlassStrategy::train_prepare_next
();
83
return
SGVector<int32_t>
(subset.vector, tot,
true
);
84
}
85
86
int32_t
CECOCStrategy::decide_label
(
SGVector<float64_t>
outputs)
87
{
88
return
m_decoder
->
decide_label
(outputs,
m_codebook
);
89
}
90
91
int32_t
CECOCStrategy::get_num_machines
()
92
{
93
return
m_codebook
.
num_cols
;
94
}
SHOGUN
机器学习工具包 - 项目文档