SHOGUN
3.2.1
首页
相关页面
模块
类
文件
文件列表
文件成员
全部
类
命名空间
文件
函数
变量
类型定义
枚举
枚举值
友元
宏定义
组
页
src
shogun
labels
DenseLabels.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-2009 Soeren Sonnenburg
8
* Written (W) 1999-2008 Gunnar Raetsch
9
* Written (W) 2011 Heiko Strathmann
10
* Copyright (C) 1999-2009 Fraunhofer Institute FIRST and Max-Planck-Society
11
*/
12
13
#include <
shogun/labels/Labels.h
>
14
#include <
shogun/labels/DenseLabels.h
>
15
#include <
shogun/lib/common.h
>
16
#include <
shogun/io/File.h
>
17
#include <
shogun/io/SGIO.h
>
18
#include <
shogun/mathematics/Math.h
>
19
#include <
shogun/base/Parameter.h
>
20
21
using namespace
shogun;
22
23
CDenseLabels::CDenseLabels
()
24
:
CLabels
()
25
{
26
init();
27
}
28
29
CDenseLabels::CDenseLabels
(int32_t num_lab)
30
:
CLabels
()
31
{
32
init();
33
m_labels
=
SGVector<float64_t>
(num_lab);
34
m_current_values
=
SGVector<float64_t>
(num_lab);
35
}
36
37
CDenseLabels::CDenseLabels
(
CFile
* loader)
38
:
CLabels
()
39
{
40
init();
41
load
(loader);
42
}
43
44
CDenseLabels::~CDenseLabels
()
45
{
46
}
47
48
void
CDenseLabels::init()
49
{
50
SG_ADD
(&
m_labels
,
"labels"
,
"The labels."
,
MS_NOT_AVAILABLE
);
51
}
52
53
void
CDenseLabels::set_to_one
()
54
{
55
set_to_const
(1.0);
56
}
57
58
void
CDenseLabels::zero
()
59
{
60
set_to_const
(0.0);
61
}
62
63
void
CDenseLabels::set_to_const
(
float64_t
c)
64
{
65
ASSERT
(
m_labels
.
vector
)
66
index_t
subset_size=
get_num_labels
();
67
for
(int32_t i=0; i<subset_size; i++)
68
{
69
m_labels
.
vector
[
m_subset_stack
->
subset_idx_conversion
(i)]=c;
70
m_current_values
.
vector
[
m_subset_stack
->
subset_idx_conversion
(i)]=c;
71
}
72
}
73
74
void
CDenseLabels::set_labels
(
SGVector<float64_t>
v)
75
{
76
if
(
m_subset_stack
->
has_subsets
())
77
SG_ERROR
(
"A subset is set, cannot set labels\n"
)
78
79
m_labels
= v;
80
}
81
82
SGVector<float64_t>
CDenseLabels::get_labels
()
83
{
84
if
(
m_subset_stack
->
has_subsets
())
85
return
get_labels_copy
();
86
87
return
m_labels
;
88
}
89
90
SGVector<float64_t>
CDenseLabels::get_labels_copy
()
91
{
92
if
(!
m_subset_stack
->
has_subsets
())
93
return
m_labels
.
clone
();
94
95
index_t
num_labels =
get_num_labels
();
96
SGVector<float64_t>
result(num_labels);
97
98
/* copy element wise because of possible subset */
99
for
(
index_t
i=0; i<num_labels; i++)
100
result[i] =
get_label
(i);
101
102
return
result;
103
}
104
105
SGVector<int32_t>
CDenseLabels::get_int_labels
()
106
{
107
SGVector<int32_t>
intlab(
get_num_labels
());
108
109
for
(int32_t i=0; i<
get_num_labels
(); i++)
110
intlab.
vector
[i] =
get_int_label
(i);
111
112
return
intlab;
113
}
114
115
void
CDenseLabels::set_int_labels
(
SGVector<int32_t>
lab)
116
{
117
if
(
m_subset_stack
->
has_subsets
())
118
SG_ERROR
(
"set_int_labels() is not possible on subset"
)
119
120
m_labels
=
SGVector<float64_t>
(lab.
vlen
);
121
122
for
(int32_t i=0; i<lab.
vlen
; i++)
123
set_int_label
(i, lab.
vector
[i]);
124
}
125
126
#if !defined(SWIGJAVA) && !defined(SWIGCSHARP)
127
void
CDenseLabels::set_int_labels
(
SGVector<int64_t>
lab)
128
{
129
if
(
m_subset_stack
->
has_subsets
())
130
SG_ERROR
(
"set_int_labels() is not possible on subset"
)
131
132
m_labels
=
SGVector<float64_t>
(lab.
vlen
);
133
134
for
(int32_t i=0; i<lab.
vlen
; i++)
135
set_int_label
(i, lab.
vector
[i]);
136
}
137
#endif
138
139
void
CDenseLabels::ensure_valid
(
const
char
* context)
140
{
141
if
(
m_labels
.
vector
== NULL)
142
SG_ERROR
(
"%s%sempty content (NULL) for labels\n"
, context?context:
""
, context?
": "
:
""
)
143
}
144
145
void
CDenseLabels::load
(
CFile
* loader)
146
{
147
remove_subset
();
148
m_labels
=
SGVector<float64_t>
();
149
m_labels
.
load
(loader);
150
}
151
152
void
CDenseLabels::save
(
CFile
* writer)
153
{
154
if
(
m_subset_stack
->
has_subsets
())
155
SG_ERROR
(
"save() is not possible on subset"
)
156
157
m_labels
.
save
(writer);
158
}
159
160
bool
CDenseLabels::set_label
(int32_t idx,
float64_t
label)
161
{
162
int32_t real_num=
m_subset_stack
->
subset_idx_conversion
(idx);
163
if
(
m_labels
.
vector
&& real_num<
get_num_labels
())
164
{
165
m_labels
.
vector
[real_num]=label;
166
return
true
;
167
}
168
else
169
return
false
;
170
}
171
172
bool
CDenseLabels::set_int_label
(int32_t idx, int32_t label)
173
{
174
int32_t real_num=
m_subset_stack
->
subset_idx_conversion
(idx);
175
if
(
m_labels
.
vector
&& real_num<
get_num_labels
())
176
{
177
m_labels
.
vector
[real_num] = (
float64_t
)label;
178
return
true
;
179
}
180
else
181
return
false
;
182
}
183
184
float64_t
CDenseLabels::get_label
(int32_t idx)
185
{
186
int32_t real_num=
m_subset_stack
->
subset_idx_conversion
(idx);
187
ASSERT
(
m_labels
.
vector
&& idx<
get_num_labels
())
188
return
m_labels
.
vector
[real_num];
189
}
190
191
int32_t
CDenseLabels::get_int_label
(int32_t idx)
192
{
193
int32_t real_num=
m_subset_stack
->
subset_idx_conversion
(idx);
194
ASSERT
(
m_labels
.
vector
&& idx<
get_num_labels
())
195
if
(
m_labels
.
vector
[real_num] !=
float64_t
((int32_t(
m_labels
.
vector
[real_num]))))
196
SG_ERROR
(
"label[%d]=%g is not an integer\n"
,
m_labels
.
vector
[real_num])
197
198
return
int32_t(
m_labels
.
vector
[real_num]);
199
}
200
201
int32_t
CDenseLabels::get_num_labels
()
const
202
{
203
return
m_subset_stack
->
has_subsets
()
204
?
m_subset_stack
->
get_size
() :
m_labels
.
vlen
;
205
}
SHOGUN
机器学习工具包 - 项目文档