-
-
Notifications
You must be signed in to change notification settings - Fork 1k
/
PNorm.cpp
124 lines (105 loc) · 2.52 KB
/
PNorm.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/*
* This software is distributed under BSD 3-clause license (see LICENSE file).
*
* Authors: Viktor Gal, Soeren Sonnenburg, Evgeniy Andreev, Bjoern Esser,
* Sergey Lisitsyn
*/
#include <shogun/preprocessor/PNorm.h>
#include <shogun/preprocessor/DensePreprocessor.h>
#include <shogun/mathematics/Math.h>
#include <shogun/features/Features.h>
#ifdef HAVE_LAPACK
#include <shogun/mathematics/lapack.h>
#endif
using namespace shogun;
CPNorm::CPNorm ()
: CDensePreprocessor<float64_t>(),
m_p (2.0)
{
register_param ();
}
CPNorm::CPNorm (double p)
: CDensePreprocessor<float64_t>(),
m_p (p)
{
ASSERT (m_p >= 1.0)
register_param ();
}
CPNorm::~CPNorm ()
{
}
/// clean up allocated memory
void CPNorm::cleanup ()
{
}
/// initialize preprocessor from file
bool CPNorm::load (FILE* f)
{
SG_SET_LOCALE_C;
SG_RESET_LOCALE;
return false;
}
/// save preprocessor init-data to file
bool CPNorm::save (FILE* f)
{
SG_SET_LOCALE_C;
SG_RESET_LOCALE;
return false;
}
/// apply preproc on feature matrix
/// result in feature matrix
/// return pointer to feature_matrix, i.e. f->get_feature_matrix();
SGMatrix<float64_t> CPNorm::apply_to_feature_matrix (CFeatures* features)
{
auto feature_matrix =
features->as<CDenseFeatures<float64_t>>()->get_feature_matrix();
for (int32_t i=0; i<feature_matrix.num_cols; i++)
{
float64_t* vec= &(feature_matrix.matrix[i*feature_matrix.num_rows]);
float64_t norm = get_pnorm (vec, feature_matrix.num_rows);
SGVector<float64_t>::scale_vector(1.0/norm, vec, feature_matrix.num_rows);
}
return feature_matrix;
}
/// apply preproc on single feature vector
/// result in feature matrix
SGVector<float64_t> CPNorm::apply_to_feature_vector (SGVector<float64_t> vector)
{
float64_t* normed_vec = SG_MALLOC(float64_t, vector.vlen);
float64_t norm = get_pnorm (vector.vector, vector.vlen);
for (int32_t i=0; i<vector.vlen; i++)
normed_vec[i]=vector.vector[i]/norm;
return SGVector<float64_t>(normed_vec,vector.vlen);
}
void CPNorm::set_pnorm (double pnorm)
{
ASSERT (pnorm >= 1.0)
m_p = pnorm;
register_param ();
}
double CPNorm::get_pnorm () const
{
return m_p;
}
void CPNorm::register_param ()
{
SG_ADD(&m_p, "norm", "P-norm parameter", MS_AVAILABLE);
}
inline float64_t CPNorm::get_pnorm (float64_t* vec, int32_t vec_len) const
{
float64_t norm = 0.0;
if (m_p == 1.0)
{
for (int i = 0; i < vec_len; ++i)
norm += fabs (vec[i]);
}
else if (m_p == 2.0)
{
norm = SGVector<float64_t>::twonorm(vec, vec_len);
}
else
{
norm = SGVector<float64_t>::qnorm(vec, vec_len, m_p);
}
return norm;
}