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
latent
LatentModel.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 Viktor Gal
8
* Copyright (C) 2012 Viktor Gal
9
*/
10
11
#include <
shogun/latent/LatentModel.h
>
12
#include <
shogun/labels/BinaryLabels.h
>
13
14
using namespace
shogun;
15
16
CLatentModel::CLatentModel
()
17
: m_features(NULL), m_labels(NULL)
18
{
19
register_parameters();
20
}
21
22
CLatentModel::CLatentModel
(
CLatentFeatures
* feats,
CLatentLabels
* labels)
23
: m_features(feats), m_labels(labels)
24
{
25
register_parameters();
26
SG_REF
(
m_features
);
27
SG_REF
(
m_labels
);
28
}
29
30
CLatentModel::~CLatentModel
()
31
{
32
SG_UNREF
(
m_labels
);
33
SG_UNREF
(
m_features
);
34
}
35
36
int32_t
CLatentModel::get_num_vectors
()
const
37
{
38
return
m_features
->
get_num_vectors
();
39
}
40
41
void
CLatentModel::set_labels
(
CLatentLabels
* labs)
42
{
43
SG_UNREF
(
m_labels
);
44
SG_REF
(labs);
45
m_labels
= labs;
46
}
47
48
CLatentLabels
*
CLatentModel::get_labels
()
const
49
{
50
SG_REF
(
m_labels
);
51
return
m_labels
;
52
}
53
54
void
CLatentModel::set_features
(
CLatentFeatures
* feats)
55
{
56
SG_UNREF
(
m_features
);
57
SG_REF
(feats);
58
m_features
= feats;
59
}
60
61
void
CLatentModel::argmax_h
(
const
SGVector<float64_t>
& w)
62
{
63
int32_t num =
get_num_vectors
();
64
CBinaryLabels
* y =
CBinaryLabels::obtain_from_generic
(
m_labels
->
get_labels
());
65
ASSERT
(num > 0);
66
ASSERT
(num ==
m_labels
->
get_num_labels
());
67
68
69
// argmax_h only for positive examples
70
for
(int32_t i = 0; i < num; ++i)
71
{
72
if
(y->
get_label
(i) == 1)
73
{
74
// infer h and set it for the argmax_h <w,psi(x,h)>
75
CData
* latent_data =
infer_latent_variable
(w, i);
76
m_labels
->
set_latent_label
(i, latent_data);
77
}
78
}
79
}
80
81
void
CLatentModel::register_parameters()
82
{
83
m_parameters
->
add
((
CSGObject
**) &
m_features
,
"features"
,
"Latent features"
);
84
m_parameters
->
add
((
CSGObject
**) &
m_labels
,
"labels"
,
"Latent labels"
);
85
}
86
SHOGUN
Machine Learning Toolbox - Documentation