-
-
Notifications
You must be signed in to change notification settings - Fork 1k
/
StreamingHashedDenseFeatures_unittest.cc
148 lines (121 loc) · 3.7 KB
/
StreamingHashedDenseFeatures_unittest.cc
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* Written (W) 2013 Evangelos Anagnostopoulos
*/
#include <shogun/lib/Hash.h>
#include <shogun/features/DenseFeatures.h>
#include <shogun/features/streaming/StreamingHashedDenseFeatures.h>
#include <gtest/gtest.h>
using namespace shogun;
TEST(StreamingHashedDenseFeaturesTest, dot)
{
index_t n=3;
index_t dim=10;
SGMatrix<float64_t> data(dim,n);
for (index_t i=0; i<n; i++)
{
for (index_t j=0; j<dim; j++)
data(j,i) = j + i * dim;
}
int32_t hashing_dim = 8;
CDenseFeatures<float64_t>* d_feats = new CDenseFeatures<float64_t>(data);
CStreamingHashedDenseFeatures<float64_t>* h_feats =
new CStreamingHashedDenseFeatures<float64_t>(d_feats, hashing_dim);
h_feats->start_parser();
index_t i;
for (i=0; i<n && h_feats->get_next_example(); i++)
{
SGVector<uint32_t> tmp(hashing_dim);
SGVector<uint32_t>::fill_vector(tmp, hashing_dim, 0);
for (index_t j=0; j<dim; j++)
{
uint32_t hash = CHash::MurmurHash3((uint8_t* ) &j, sizeof (index_t), j);
hash = hash % hashing_dim;
tmp[hash] += data(j,i);
}
float64_t dot_product = 0;
for (index_t j=0; j<hashing_dim; j++)
dot_product += tmp[j] * tmp[j];
float64_t feat_dot = h_feats->dot(h_feats);
EXPECT_EQ(feat_dot, dot_product);
h_feats->release_example();
}
h_feats->end_parser();
EXPECT_EQ(i, n);
SG_UNREF(h_feats);
}
TEST(StreamingHashedDenseFeaturesTest, dense_dot)
{
index_t n=3;
index_t dim=10;
SGMatrix<float64_t> data(dim,n);
for (index_t i=0; i<n; i++)
{
for (index_t j=0; j<dim; j++)
data(j,i) = j + i * dim;
}
int32_t hashing_dim = 8;
CDenseFeatures<float64_t>* d_feats = new CDenseFeatures<float64_t>(data);
CStreamingHashedDenseFeatures<float64_t>* h_feats =
new CStreamingHashedDenseFeatures<float64_t>(d_feats, hashing_dim);
h_feats->start_parser();
for (index_t i=0; i<n && h_feats->get_next_example(); i++)
{
SGVector<float32_t> tmp(hashing_dim);
SGVector<float32_t>::fill_vector(tmp, hashing_dim, 0);
for (index_t j=0; j<dim; j++)
{
uint32_t hash = CHash::MurmurHash3((uint8_t* ) &j, sizeof (index_t), j);
hash = hash % hashing_dim;
tmp[hash] += data(j,i);
}
float64_t dot_product = 0;
for (index_t j=0; j<hashing_dim; j++)
dot_product += tmp[j] * tmp[j];
float64_t feat_dot = h_feats->dense_dot(tmp.vector, tmp.vlen);
EXPECT_EQ(feat_dot, dot_product);
h_feats->release_example();
}
h_feats->end_parser();
SG_UNREF(h_feats);
}
TEST(StreamingHashedDenseFeaturesTest, add_to_dense)
{
index_t n=3;
index_t dim=10;
SGMatrix<float64_t> data(dim,n);
for (index_t i=0; i<n; i++)
{
for (index_t j=0; j<dim; j++)
data(j,i) = j + i * dim;
}
int32_t hashing_dim = 8;
CDenseFeatures<float64_t>* d_feats = new CDenseFeatures<float64_t>(data);
CStreamingHashedDenseFeatures<float64_t>* h_feats =
new CStreamingHashedDenseFeatures<float64_t>(d_feats, hashing_dim);
h_feats->start_parser();
for (index_t i=0; i<n && h_feats->get_next_example(); i++)
{
SGVector<float32_t> tmp(hashing_dim);
SGVector<float32_t>::fill_vector(tmp, hashing_dim, 0);
for (index_t j=0; j<dim; j++)
{
uint32_t hash = CHash::MurmurHash3((uint8_t* ) &j, sizeof (index_t), j);
hash = hash % hashing_dim;
tmp[hash] += data(j,i);
}
SGVector<float64_t> tmp2(hashing_dim);
for (index_t j=0; j<hashing_dim; j++)
tmp2[j] = 3 * tmp[j];
h_feats->add_to_dense_vec(2, tmp.vector, tmp.vlen);
for (index_t j=0; j<hashing_dim; j++)
EXPECT_EQ(tmp2[j], tmp[j]);
h_feats->release_example();
}
h_feats->end_parser();
SG_UNREF(h_feats);
}