-
Notifications
You must be signed in to change notification settings - Fork 4
/
inner_product_test.cpp
206 lines (166 loc) · 7.05 KB
/
inner_product_test.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
// #######################################
// # Copyright (C) 2020-2023 Otmar Ertl. #
// # All rights reserved. #
// #######################################
// Bessa, Aline, et al. "Weighted Minwise Hashing Beats Linear Sketching for
// Inner Product Estimation." arXiv preprint arXiv:2301.05811 (2023) describes
// how weighted minwise hashing can be used for inner product estimation. This
// code demonstrates that their approach can be simplified using TreeMinHash
// which is likely faster and does not require the unnatural discretization of
// input vectors.
#include "bitstream_random.hpp"
#include "data_generation.hpp"
#include "weighted_minwise_hashing.hpp"
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <random>
using namespace std;
class RNGFunction {
const uint64_t seed;
public:
typedef tmh::WyrandBitStream RngType;
RNGFunction(uint64_t seed) : seed(seed) {}
tmh::WyrandBitStream operator()(uint64_t x) const {
return tmh::WyrandBitStream(x, seed);
}
tmh::WyrandBitStream operator()(uint64_t x, uint64_t y) const {
return tmh::WyrandBitStream(x, y, seed);
}
};
class InnerProductSketch {
const vector<pair<double, double>> hashValueList;
double norm;
const double exponent;
public:
InnerProductSketch(const vector<pair<double, double>> &hashValueList,
double norm, double exponent)
: hashValueList(hashValueList), norm(norm), exponent(exponent) {}
static double estimate_inner_product(const InnerProductSketch &sketch1,
const InnerProductSketch &sketch2) {
assert(sketch1.exponent == sketch2.exponent);
assert(sketch1.hashValueList.size() == sketch2.hashValueList.size());
const size_t m = sketch1.hashValueList.size();
double exponent = sketch1.exponent;
double estimatorSum = 0;
double minHashSum = 0;
for (size_t j = 0; j < m; ++j) {
auto &[hash1, val1] = sketch1.hashValueList[j];
auto &[hash2, val2] = sketch2.hashValueList[j];
if (hash1 == hash2) {
estimatorSum +=
(val1 * val2) / pow(min(fabs(val1), fabs(val2)), exponent);
}
minHashSum += min(hash1, hash2);
}
const double estimatedUnionSize =
static_cast<double>(m) * static_cast<double>(m) / minHashSum;
return sketch1.norm * sketch2.norm * (estimatorSum / m) *
estimatedUnionSize;
}
};
class InnerProductSketchCalculator {
tmh::TreeMinHash<RNGFunction> treeMinHash;
const double exponent;
public:
InnerProductSketchCalculator(uint32_t m, uint64_t seed, double exponent = 2)
: treeMinHash(m, RNGFunction(seed),
1. // set maximum supported weight equal to 1, as all
// weights will be <= 1. due to normalization
),
exponent(exponent) {}
InnerProductSketch compute(const vector<pair<uint64_t, double>> &input) {
unordered_map<uint64_t, double> keyToValueMap(input.cbegin(), input.cend());
// calculate norm
const double norm =
sqrt(accumulate(input.cbegin(), input.cend(), 0., [](auto sum, auto x) {
return sum + x.second * x.second;
}));
// normalized and squared input for TreeMinHash
vector<pair<uint64_t, double>> keyNormalizedSquaredValueList(input.size());
transform(input.cbegin(), input.cend(),
keyNormalizedSquaredValueList.begin(), [&](auto &keyValue) {
return make_pair(keyValue.first,
pow(fabs(keyValue.second) / norm, exponent));
});
// weighted minhash
const vector<pair<uint64_t, double>> weightedMinHashResult =
treeMinHash(keyNormalizedSquaredValueList);
// setup data for inner product sketch
vector<pair<double, double>> hashValueList(weightedMinHashResult.size());
transform(weightedMinHashResult.cbegin(), weightedMinHashResult.cend(),
hashValueList.begin(), [&](auto &keyHash) {
return make_pair(keyHash.second,
keyToValueMap[keyHash.first] / norm);
});
return InnerProductSketch(hashValueList, norm, exponent);
}
};
int main(int argc, char *argv[]) {
uint64_t numCycles = 1000;
uint64_t sizeOfWeightList = 1000;
uint32_t m = 1 << 14;
// test different exponents
vector<double> exponentValues = {0, 0.5, 1, 1.5, 2, 3};
uint64_t seedForWeights = UINT64_C(0x0c2954b1cb065f32);
uint64_t seedForKeys = UINT64_C(0x11da3e19c9262418);
uint64_t seedForTreeMinHash = UINT64_C(0xda47270740231451);
// generate some weight vectors
mt19937_64 rngForWeights(seedForWeights);
uniform_real_distribution<double> nonZeroValueDistribution1(-2.3, 1);
uniform_real_distribution<double> nonZeroValueDistribution2(-3, 10);
bernoulli_distribution zeroDistribution1(0.1);
bernoulli_distribution zeroDistribution2(0.2);
double trueInnerProduct = 0.;
vector<pair<double, double>> weights(sizeOfWeightList);
for (uint64_t i = 0; i < sizeOfWeightList; ++i) {
double weight1 = zeroDistribution1(rngForWeights)
? 0.
: nonZeroValueDistribution1(rngForWeights);
double weight2 = zeroDistribution2(rngForWeights)
? 0.
: nonZeroValueDistribution2(rngForWeights);
weights[i] = {weight1, weight2};
trueInnerProduct += weight1 * weight2;
}
for (double exponent : exponentValues) {
mt19937_64 rngForKeys(seedForKeys);
double sumError = 0;
double sumSquaredError = 0;
InnerProductSketchCalculator sketch_calculator(m, seedForTreeMinHash,
exponent);
for (uint64_t j = 0; j < numCycles; ++j) {
// create random input by associating random keys with given weights
vector<pair<uint64_t, double>> input1;
vector<pair<uint64_t, double>> input2;
for (uint64_t i = 0; i < weights.size(); ++i) {
uint64_t key = rngForKeys();
double weight1 = weights[i].first;
double weight2 = weights[i].second;
if (weight1 != 0.)
input1.emplace_back(key, weight1);
if (weight2 != 0.)
input2.emplace_back(key, weight2);
}
// compute sketches for inner product
auto sketch1 = sketch_calculator.compute(input1);
auto sketch2 = sketch_calculator.compute(input2);
// estimate inner product
double estimatedInnerProduct =
InnerProductSketch::estimate_inner_product(sketch1, sketch2);
double error = estimatedInnerProduct - trueInnerProduct;
sumError += error;
sumSquaredError += error * error;
}
const double relativeEstimationBias =
sumError / fabs(trueInnerProduct) / numCycles;
const double relativeEstimationRmse =
sqrt(sumSquaredError) / fabs(trueInnerProduct) / numCycles;
cout << "exponent = " << exponent << endl;
cout << "true inner product = " << trueInnerProduct << endl;
cout << "relative estimation bias = " << relativeEstimationBias << endl;
cout << "relative estimation rmse = " << relativeEstimationRmse << endl;
cout << endl;
}
return 0;
}