SHOGUN
v2.0.0
Main Page
Related Pages
Modules
Classes
Files
File List
File Members
All
Classes
Namespaces
Files
Functions
Variables
Typedefs
Enumerations
Enumerator
Friends
Macros
Groups
Pages
src
shogun
structure
StructuredModel.cpp
Go to the documentation of this file.
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 Fernando José Iglesias García
8
* Copyright (C) 2012 Fernando José Iglesias García
9
*/
10
11
#include <
shogun/structure/StructuredModel.h
>
12
13
using namespace
shogun;
14
15
CStructuredModel::CStructuredModel
() :
CSGObject
()
16
{
17
init();
18
}
19
20
CStructuredModel::CStructuredModel
(
21
CFeatures
* features,
22
CStructuredLabels
* labels)
23
:
CSGObject
()
24
{
25
init();
26
27
m_features
= features;
28
m_labels
= labels;
29
30
SG_REF
(features);
31
SG_REF
(labels);
32
}
33
34
CStructuredModel::~CStructuredModel
()
35
{
36
SG_UNREF
(
m_labels
);
37
SG_UNREF
(
m_features
);
38
}
39
40
void
CStructuredModel::init_opt
(
41
SGMatrix< float64_t >
& A,
42
SGVector< float64_t >
a,
43
SGMatrix< float64_t >
B,
44
SGVector< float64_t >
& b,
45
SGVector< float64_t >
lb,
46
SGVector< float64_t >
ub,
47
SGMatrix< float64_t >
& C)
48
{
49
SG_ERROR
(
"init_opt is not implemented for %s!\n"
,
get_name
());
50
}
51
52
void
CStructuredModel::set_labels
(
CStructuredLabels
* labels)
53
{
54
SG_UNREF
(
m_labels
);
55
SG_REF
(labels);
56
m_labels
= labels;
57
}
58
59
CStructuredLabels
*
CStructuredModel::get_labels
()
60
{
61
SG_REF
(
m_labels
);
62
return
m_labels
;
63
}
64
65
void
CStructuredModel::set_features
(
CFeatures
* features)
66
{
67
SG_UNREF
(
m_features
);
68
SG_REF
(features);
69
m_features
= features;
70
}
71
72
CFeatures
*
CStructuredModel::get_features
()
73
{
74
SG_REF
(
m_features
);
75
return
m_features
;
76
}
77
78
SGVector< float64_t >
CStructuredModel::get_joint_feature_vector
(
79
int32_t feat_idx,
80
int32_t lab_idx)
81
{
82
CStructuredData
* label =
m_labels
->
get_label
(lab_idx);
83
SGVector< float64_t >
ret =
get_joint_feature_vector
(feat_idx, label);
84
SG_UNREF
(label);
85
86
return
ret;
87
}
88
89
SGVector< float64_t >
CStructuredModel::get_joint_feature_vector
(
90
int32_t feat_idx,
91
CStructuredData
* y)
92
{
93
SG_ERROR
(
"compute_joint_feature(int32_t, CStructuredData*) is not "
94
"implemented for %s!\n"
,
get_name
());
95
96
return
SGVector< float64_t >
();
97
}
98
99
float64_t
CStructuredModel::delta_loss
(int32_t ytrue_idx,
CStructuredData
* ypred)
100
{
101
REQUIRE
(ytrue_idx >= 0 || ytrue_idx < m_labels->get_num_labels(),
102
"The label index must be inside [0, num_labels-1]\n"
);
103
104
CStructuredData
* ytrue =
m_labels
->
get_label
(ytrue_idx);
105
float64_t
ret =
delta_loss
(ytrue, ypred);
106
SG_UNREF
(ytrue);
107
108
return
ret;
109
}
110
111
float64_t
CStructuredModel::delta_loss
(
CStructuredData
* y1,
CStructuredData
* y2)
112
{
113
SG_ERROR
(
"delta_loss(CStructuredData*, CStructuredData*) is not "
114
"implemented for %s!\n"
,
get_name
());
115
116
return
0.0;
117
}
118
119
void
CStructuredModel::init()
120
{
121
SG_ADD
((
CSGObject
**) &
m_labels
,
"m_labels"
,
"Structured labels"
,
122
MS_NOT_AVAILABLE
);
123
SG_ADD
((
CSGObject
**) &
m_features
,
"m_features"
,
"Feature vectors"
,
124
MS_NOT_AVAILABLE
);
125
126
m_features
= NULL;
127
m_labels
= NULL;
128
}
129
130
bool
CStructuredModel::check_training_setup
()
const
131
{
132
// Nothing to do here
133
return
true
;
134
}
135
136
int32_t
CStructuredModel::get_num_aux
()
const
137
{
138
return
0;
139
}
140
141
int32_t
CStructuredModel::get_num_aux_con
()
const
142
{
143
return
0;
144
}
145
146
float64_t
CStructuredModel::risk
(
float64_t
* subgrad,
float64_t
* W,
TMultipleCPinfo
* info)
147
{
148
int32_t from=0, to=0;
149
if
(info)
150
{
151
from = info->
_from
;
152
to = (info->
N
== 0) ?
m_features
->
get_num_vectors
() : from+info->
N
;
153
}
154
else
155
{
156
from = 0;
157
to =
m_features
->
get_num_vectors
();
158
}
159
160
int32_t dim = this->
get_dim
();
161
float64_t
R = 0.0;
162
for
(int32_t i=0; i<dim; i++)
163
subgrad[i] = 0;
164
165
for
(int32_t i=from; i<to; i++)
166
{
167
CResultSet
* result = this->
argmax
(
SGVector<float64_t>
(W,dim,
false
), i,
true
);
168
SGVector<float64_t>
psi_pred = result->
psi_pred
;
169
SGVector<float64_t>
psi_truth = result->
psi_truth
;
170
SGVector<float64_t>::vec1_plus_scalar_times_vec2
(subgrad, 1.0, psi_pred.
vector
, dim);
171
SGVector<float64_t>::vec1_plus_scalar_times_vec2
(subgrad, -1.0, psi_truth.
vector
, dim);
172
R += result->
score
;
173
SG_UNREF
(result);
174
}
175
176
return
R;
177
}
SHOGUN
Machine Learning Toolbox - Documentation