-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathMatrixUtils.h
266 lines (226 loc) · 8.5 KB
/
MatrixUtils.h
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
/*
Copyright (C) 2004,2005,2006,2007,2008,2009,2010,2011,2012,2013 Cyrus Shaoul and Geoff Hollis
This file is part of HiDEx.
HiDEx 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.
HiDEx is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with HiDEx in the COPYING.txt file.
If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef MatrixUtils_H
#define MatrixUtils_H
#include "SDDB.h"
#include "Exception.h"
// Why are these two functions here? Because they use templates, and must be in a .h file!
/** Collapse matrix.
@param M Sparse matrix of co-occurrence values that has dimensions x and
y, where x is the window size and y is the size of the lexicon.
@param weights Array of integers that are used to multiply the
values in each position of the window before summing them.
@param realBehind The actual size of the backward window that is stored in
the db.
@param behind The desired size of the backward window
@param realAhead see above
@param ahead see above
@param context Array of integers that contains all the word-IDs of the
words that are part of the context (in this case, the N words with the
highest raw frequencies in the corpus)
@param context_size is the size of the context array
@param separate Flag for "separate mode". If it is TRUE, then
forward and backward values are summed separately. If it is FALSE, then
the forward and backward values are summed together. If true, it doubles
the size of the final matrix.
@param num_dimensions if separate is FALSE, it is the context size. If
separate is TRUE, it is 2 * context_size.
*/
/* void normalizeWeightedCooccurenceVector(Float cooccurenceVector[], int size, Float wordfrequency) */
/* { */
/* // normalize vectors by dividing by ofreq. */
/* // make reciprocal of 1 over non-zero frequncy */
/* // cerr << "Normalizing Word Frequency = " << wordfrequency << endl; */
/* Float wordfrequency = static_cast<Float>(_frequency[i]); */
/* Float adjustedfreq = (1.0 / (wordfrequency + 1.0)); */
/* for(int i = 0; i < size; i++) { */
/* if (cooccurenceVector[i]) { */
/* // cerr << "Adjusted real freq " << cooccurenceVector[i] << " to "; */
/* cooccurenceVector[i] *= adjustedfreq; */
/* // cerr << cooccurenceVector[i] << endl; */
/* } */
/* } */
/* } */
template <class T>
Float*computeWeightedCooccurenceVectorUnified(Matrix<T> *M,
const vector<int>& weights, int realBehind,
int behind, int realAhead,
int ahead, vector<int>& frequentWords)
{
// cerr << "Computing aggregate" << endl;
// Sanity check for window ranges.
// bool temp = (ahead <= realAhead);
assert(ahead <= realAhead);
// temp = (behind <= realBehind);
assert(behind <= realBehind);
int windowLen = behind+ahead;
int startpoint = realBehind-behind;
size_t contextsize = frequentWords.size();
// cerr << "converting array"<< endl;
int **array = M->toArray();
// cerr << "Make new array of Floats."<< endl;
Float *vect = new Float[contextsize];
// cerr << "Start aggregating vectors."<< endl;
for(size_t i = 0; i < contextsize; i++) {
// branch if using separate forward and backward contexts
Float val = 0.0;
// collapse all of the columns into a single cell
// cerr << "Check for NULLs."<< endl;
if (array[frequentWords[i]] == NULL) {
val = 0.0;
}
else {
// cerr << "Multiply values"<< endl;
for (int k = 0; k < windowLen; k++) {
// cerr << " Value : " << i << "," << k << " = " << array[frequentWords[i]][k+startpoint]<< endl;
val += static_cast<Float>((array[frequentWords[i]][k+startpoint] * weights[k]));
}
}
vect[i] = val;
// cerr << "Set aggregated value: " << val << endl;
}
// normalizeWeightedCooccurenceVector(vect, frequentWords.size(), wordfrequency, normalization);
// cerr << "Delete array"<< endl;
delete [] array;
return vect;
}
template <class T>
Float*computeWeightedCooccurenceVectorSeparate(Matrix<T> *M, const vector<int>& weights, int realBehind,
int behind, int realAhead, int ahead, vector<int>& frequentWords)
{
// bool temp = (ahead <= realAhead);
assert(ahead <= realAhead);
// temp = (behind <= realBehind);
assert(behind <= realBehind);
size_t num_dimensions = 2 * frequentWords.size();
size_t context_size = frequentWords.size();
int windowLen = behind+ahead;
int startpoint = realBehind-behind;
int **array = M->toArray();
Float *vect = new Float[num_dimensions];
for (size_t p=0; p < num_dimensions; p++)
vect[p]=0.0;
// branch if using separate forward and backward contexts
// if (windowLen % 2 != 0)
// {
// throw Exception("Window length was not an even number! exiting!");
// }
// debugging... printing data....
/* int fullw = realAhead + realBehind; */
/* for (size_t x = 0; x < 100; x++) { */
/* if (array[x] != NULL) { */
/* cerr << "Row #" << x << " :"; */
/* for (int y = 0; y < fullw; y++) { */
/* cerr << array[x][y] << " "; */
/* } */
/* cerr << endl; */
/* } */
/* } */
for(size_t i = 0; i < context_size; i++)
{
Float valforw = 0.0;
Float valback = 0.0;
if (array[frequentWords[i]] == NULL)
{
valforw = 0.0;
valforw = 0.0;
}
else
{
// cerr << "Full Window Width = " << windowLen << endl;
// cerr << "Window Start Point = " << startpoint << endl;
// cerr << "For Word: " << frequentWords[i] << " -- " ;
for (int k = 0; k < (windowLen - ahead); k++)
{
valback += static_cast<Float>((array[frequentWords[i]][k+startpoint] * weights[k]));
// cerr << "Position = " << k << ", Valback = " << valback << " , ";
}
for (int k = (windowLen - ahead); k < windowLen; k++)
{
valforw += static_cast<Float>((array[frequentWords[i]][k+startpoint] * weights[k]));
// cerr << "Position = " << k << " Valforw = " << valforw << " ";
}
// cerr << endl;
}
vect[i] = valback;
assert((i+ context_size) < num_dimensions);
vect[i + context_size] = valforw;
}
//debug
// for (int x = 0; x < num_dimensions; x++) {
// if (vect[x] != 0.0)
// cerr << "At: " << x << " value: " << vect[x] << endl;
// }
// normalizeWeightedCooccurenceVector(vect, num_dimensions, wordfrequency);
// cerr << "Num Dimensions = " << num_dimensions << endl;
// for (int x = 0; x < num_dimensions; x++) {
// if (vect[x] != 0.0)
// cerr << "At: " << x << " value: " << vect[x] << endl;
// }
delete [] array;
return vect;
}
template <class T>
Float computeVariance(Matrix<T> *M,
int realBehind,
int realAhead,
size_t numdimensions
)
{
//see http://en.wikipedia.org/wiki/Algorithms_for_calculating_variance
// for info on algorithm.
// cerr << "Computing Variance." << endl;
int **array = M->toArray();
// initialize variables.
Float variance = 0.0;
Float n = 0.0;
Float mean = 0.0;
Float M2 = 0.0;
Float delta = 0.0;
Float x = 0.0;
int windowLen = realBehind+realAhead;
// cerr << "Start aggregating vectors."<< endl;
for(size_t i = 0; i < numdimensions; i++) {
// collapse all of the columns into a single cell
if (array[i] != NULL) {
for (int k = 0; k < windowLen; k++) {
n++;
x = static_cast<Float>(array[i][k]);
delta = x - mean;
mean = mean + (delta/n);
M2 = M2 + (delta * (x - mean));
}
} else {
for (int k = 0; k < windowLen; k++) {
n++;
x = 0.0;
delta = x - mean;
mean = mean + (delta/n);
M2 = M2 + (delta * (x - mean));
}
}
}
if (M2 > 0.0) {
variance = M2/(n-1);
} else {
variance = 0.0;
}
// cerr << "Variance = " << variance << endl;
// cerr << "Delete array"<< endl;
delete [] array;
return variance;
}
#endif // MatrixUtils_H