SHOGUN
3.2.1
首页
相关页面
模块
类
文件
文件列表
文件成员
全部
类
命名空间
文件
函数
变量
类型定义
枚举
枚举值
友元
宏定义
组
页
src
shogun
features
streaming
generators
GaussianBlobsDataGenerator.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) 2013 Heiko Strathmann
8
*/
9
10
#include <
shogun/features/streaming/generators/GaussianBlobsDataGenerator.h
>
11
12
using namespace
shogun;
13
14
CGaussianBlobsDataGenerator::CGaussianBlobsDataGenerator
() :
15
CStreamingDenseFeatures
<
float64_t
>()
16
{
17
init();
18
}
19
20
CGaussianBlobsDataGenerator::CGaussianBlobsDataGenerator
(
index_t
sqrt_num_blobs,
21
float64_t
distance
,
float64_t
stretch,
float64_t
angle) :
22
CStreamingDenseFeatures
<
float64_t
>()
23
{
24
init();
25
set_blobs_model
(sqrt_num_blobs, distance, stretch, angle);
26
}
27
28
CGaussianBlobsDataGenerator::~CGaussianBlobsDataGenerator
()
29
{
30
}
31
32
void
CGaussianBlobsDataGenerator::set_blobs_model
(
index_t
sqrt_num_blobs,
33
float64_t
distance
,
float64_t
stretch,
float64_t
angle)
34
{
35
m_sqrt_num_blobs
=sqrt_num_blobs;
36
m_distance
=
distance
;
37
m_stretch
=stretch;
38
m_angle
=angle;
39
40
/* precompute cholesky decomposition, start with rotation matrix */
41
SGMatrix<float64_t>
R(2, 2);
42
R(0, 0)=
CMath::cos
(angle);
43
R(0, 1)=-
CMath::sin
(angle);
44
R(1, 0)=
CMath::sin
(angle);
45
R(1, 1)=
CMath::cos
(angle);
46
47
/* diagonal eigenvalue matrix */
48
SGMatrix<float64_t>
L(2, 2);
49
L(0, 0)=
CMath::sqrt
(stretch);
50
L(1, 0)=0;
51
L(0, 1)=0;
52
L(1, 1)=1;
53
54
/* compute and save cholesky for sampling later on */
55
m_cholesky
=
SGMatrix<float64_t>::matrix_multiply
(R, L);
56
}
57
58
void
CGaussianBlobsDataGenerator::init()
59
{
60
SG_ADD
(&
m_sqrt_num_blobs
,
"sqrt_num_blobs"
,
"Number of Blobs per row"
,
61
MS_NOT_AVAILABLE
);
62
SG_ADD
(&
m_distance
,
"distance"
,
"Distance between blobs"
,
63
MS_NOT_AVAILABLE
);
64
SG_ADD
(&
m_stretch
,
"stretch"
,
"Stretch of blobs"
,
65
MS_NOT_AVAILABLE
);
66
SG_ADD
(&
m_angle
,
"angle"
,
"Angle of Blobs"
,
67
MS_NOT_AVAILABLE
);
68
SG_ADD
(&
m_cholesky
,
"cholesky"
,
"Cholesky factor of covariance matrix"
,
69
MS_NOT_AVAILABLE
);
70
71
m_sqrt_num_blobs
=1;
72
m_distance
=0;
73
m_stretch
=1;
74
m_angle
=0;
75
m_cholesky
=
SGMatrix<float64_t>
(2, 2);
76
m_cholesky
(0, 0)=1;
77
m_cholesky
(0, 1)=0;
78
m_cholesky
(1, 0)=0;
79
m_cholesky
(1, 1)=1;
80
81
unset_generic
();
82
}
83
84
bool
CGaussianBlobsDataGenerator::get_next_example
()
85
{
86
SG_SDEBUG
(
"entering CGaussianBlobsDataGenerator::get_next_example()\n"
);
87
88
/* allocate space */
89
SGVector<float64_t>
result=
SGVector<float64_t>
(2);
90
91
/* sample latent distribution to compute offsets */
92
index_t
x_offset=
CMath::random
(0,
m_sqrt_num_blobs
-1)*
m_distance
;
93
index_t
y_offset=
CMath::random
(0,
m_sqrt_num_blobs
-1)*
m_distance
;
94
95
/* sample from std Gaussian */
96
float64_t
x=
CMath::randn_double
();
97
float64_t
y=
CMath::randn_double
();
98
99
/* transform through cholesky and add offset */
100
result[0]=
m_cholesky
(0, 0)*x+
m_cholesky
(0, 1)*y+x_offset;
101
result[1]=
m_cholesky
(1, 0)*x+
m_cholesky
(1, 1)*y+y_offset;
102
103
/* save example back to superclass */
104
CGaussianBlobsDataGenerator::current_vector
=result;
105
106
SG_SDEBUG
(
"leaving CGaussianBlobsDataGenerator::get_next_example()\n"
);
107
return
true
;
108
}
109
110
void
CGaussianBlobsDataGenerator::release_example
()
111
{
112
SGVector<float64_t>
temp=
SGVector<float64_t>
();
113
CGaussianBlobsDataGenerator::current_vector
=temp;
114
}
115
SHOGUN
机器学习工具包 - 项目文档