forked from sszuev/fastText_java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVector.java
More file actions
323 lines (297 loc) · 9.36 KB
/
Copy pathVector.java
File metadata and controls
323 lines (297 loc) · 9.36 KB
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
package cc.fasttext;
import cc.fasttext.io.FormatUtils;
import com.google.common.primitives.Floats;
import org.apache.commons.lang.Validate;
import org.apache.commons.math3.util.FastMath;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.function.DoubleUnaryOperator;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
/**
* Vector implementation.
* @see <a href='https://github.com/facebookresearch/fastText/blob/master/src/vector.cc'>vector.cc</a>
* @see <a href='https://github.com/facebookresearch/fastText/blob/master/src/vector.h'>vector.h</a>
*/
public class Vector {
private static final int PARALLEL_SIZE_THRESHOLD = Integer.parseInt(System.getProperty("parallel.vector.threshold",
String.valueOf(FastText.PARALLEL_THRESHOLD_FACTOR * 100)));
private float[] data;
public Vector(int size) {
this(new float[size]);
}
Vector(float[] data) {
this.data = data;
}
public int size() {
return data.length;
}
public float get(int i) {
return data[i];
}
public void set(int i, float value) {
data[i] = value;
}
public void compute(int i, DoubleUnaryOperator operator) {
Objects.requireNonNull(operator, "Null operator");
data[i] = (float) operator.applyAsDouble(data[i]);
}
float[] data() {
return data;
}
/**
* Returns a fixed-size list backed by the vectors data.
*
* @return List of floats
*/
public List<Float> getData() {
return Floats.asList(data);
}
public void clear() {
data = new float[data.length];
}
/**
* Original (c++) code:
* <pre>{@code real Vector::norm() const {
* real sum = 0;
* for (int64_t i = 0; i < m_; i++) {
* sum += data_[i] * data_[i];
* }
* return std::sqrt(sum);
* }
* }</pre>
*
* @return float
*/
public float norm() {
double sum;
if (FastText.USE_PARALLEL_COMPUTATION && size() > PARALLEL_SIZE_THRESHOLD) {
sum = IntStream.range(0, size()).parallel().mapToDouble(i -> data[i] * data[i]).sum();
} else {
sum = 0;
for (int i = 0; i < size(); i++) {
sum += data[i] * data[i];
}
}
return (float) FastMath.sqrt(sum);
}
/**
* Original (c++) code:
* <pre>{@code void Vector::addVector(const Vector& source) {
* assert(m_ == source.m_);
* for (int64_t i = 0; i < m_; i++) {
* data_[i] += source.data_[i];
* }
* }}</pre>
*
* @param source {@link Vector}
*/
public void addVector(Vector source) {
addVector(source, 1);
}
/**
* Sums up the vector with another one and some multiplier.
* Original (c++) code:
* <pre>{@code void Vector::addVector(const Vector& source, real s) {
* assert(m_ == source.m_);
* for (int64_t i = 0; i < m_; i++) {
* data_[i] += s * source.data_[i];
* }
* }}</pre>
*
* @param source {@link Vector}
* @param s float (see real.h)
*/
public void addVector(Vector source, float s) {
Validate.isTrue(size() == Objects.requireNonNull(source, "Null source vector").size(), "Wrong size of vector: " + size() + "!=" + source.size());
if (FastText.USE_PARALLEL_COMPUTATION && size() > PARALLEL_SIZE_THRESHOLD) {
IntStream.range(0, size()).parallel().forEach(i -> data[i] += s * source.data[i]);
return;
}
for (int i = 0; i < size(); i++) {
data[i] += s * source.data[i];
}
}
/**
* Original (c++) code:
* <pre>{@code
* void Vector::mul(real a) {
* for (int64_t i = 0; i < m_; i++) {
* data_[i] *= a;
* }
* }}</pre>
*
* @param a float
*/
public void mul(float a) {
if (FastText.USE_PARALLEL_COMPUTATION && size() > PARALLEL_SIZE_THRESHOLD) {
IntStream.range(0, size()).parallel().forEach(i -> data[i] *= a);
return;
}
for (int i = 0; i < size(); i++) {
data[i] *= a;
}
}
/**
* Original (c++) code:
* <pre>{@code
* void Vector::addRow(const Matrix& A, int64_t i) {
* assert(i >= 0);
* assert(i < A.m_);
* assert(m_ == A.n_);
* for (int64_t j = 0; j < A.n_; j++) {
* data_[j] += A.at(i, j);
* }
* }}</pre>
*
* @param matrix {@link Matrix}
* @param index (int64_t originally) matrix row num (m-dimension)
* @see #addRow(Matrix, int, float)
*/
public void addRow(Matrix matrix, int index) {
Validate.isTrue(index >= 0 && index < matrix.getM(), "Incompatible index (" + index + ") and matrix m-size (" + matrix.getM() + ")");
Validate.isTrue(size() == matrix.getN(), "Wrong matrix n-size: " + size() + " != " + matrix.getN());
if (matrix.isQuant()) {
addQRow((QMatrix) matrix, index);
return;
}
if (FastText.USE_PARALLEL_COMPUTATION && matrix.getN() > PARALLEL_SIZE_THRESHOLD) {
IntStream.range(0, matrix.getN()).parallel().forEach(j -> data[j] += matrix.at(index, j));
return;
}
for (int j = 0; j < matrix.getN(); j++) {
data[j] += matrix.at(index, j);
}
}
/**
* Original (c++) code:
* <pre>{@code
* void Vector::addRow(const QMatrix& A, int64_t i) {
* assert(i >= 0);
* A.addToVector(*this, i);
* }}</pre>
*
* @param matrix {@link QMatrix}
* @param i (int64_t originally) matrix row num (m-dimension)
*/
private void addQRow(QMatrix matrix, int i) {
Validate.isTrue(i >= 0);
matrix.addToVector(this, i);
}
/**
* Original (c++) code:
* <pre>{@code
* void Vector::addRow(const Matrix& A, int64_t i, real a) {
* assert(i >= 0);
* assert(i < A.m_);
* assert(m_ == A.n_);
* for (int64_t j = 0; j < A.n_; j++) {
* data_[j] += a * A.at(i, j);
* }
* }
* }</pre>
*
* @param matrix {@link Matrix}
* @param index m-dimension matrix coordinate
* @param factor float, multiplier
* @see #addRow(Matrix, int)
*/
public void addRow(Matrix matrix, int index, float factor) {
Validate.isTrue(index >= 0 && index < matrix.getM(), "Incompatible index (" + index + ") and matrix m-size (" + matrix.getM() + ")");
Validate.isTrue(size() == matrix.getN(), "Wrong matrix n-size: " + size() + " != " + matrix.getN());
if (FastText.USE_PARALLEL_COMPUTATION && matrix.getN() > PARALLEL_SIZE_THRESHOLD) {
IntStream.range(0, matrix.getN()).parallel().forEach(j -> data[j] += factor * matrix.at(index, j));
return;
}
for (int j = 0; j < matrix.getN(); j++) {
data[j] += factor * matrix.at(index, j);
}
}
/**
* Original (c++) code:
* <pre>{@code
* void Vector::mul(const Matrix& A, const Vector& vec) {
* assert(A.m_ == m_);
* assert(A.n_ == vec.m_);
* for (int64_t i = 0; i < m_; i++) {
* data_[i] = A.dotRow(vec, i);
* }
* }}</pre>
*
* @param matrix {@link Matrix}
* @param vector {@link Vector}
*/
public void mul(Matrix matrix, Vector vector) {
Validate.isTrue(matrix.getM() == size(), "Wrong matrix m-size: " + size() + " != " + matrix.getM());
Validate.isTrue(matrix.getN() == vector.size(), "Matrix n-size (" + matrix.getN() + ") and vector size (" + vector.size() + ") are not equal.");
if (FastText.USE_PARALLEL_COMPUTATION && size() > PARALLEL_SIZE_THRESHOLD) {
IntStream.range(0, size()).parallel().forEach(i -> data[i] = matrix.dotRow(vector, i));
return;
}
for (int i = 0; i < size(); i++) {
data[i] = matrix.dotRow(vector, i);
}
}
/**
* Original (c++) code:
* <pre>{@code int64_t Vector::argmax() {
* real max = data_[0];
* int64_t argmax = 0;
* for (int64_t i = 1; i < m_; i++) {
* if (data_[i] > max) {
* max = data_[i];
* argmax = i;
* }
* }
* return argmax;
* }}</pre>
*
* @return int (original : int64_t)
*/
public int argmax() {
float max = get(0);
int argmax = 0;
for (int i = 1; i < size(); i++) {
if (get(i) <= max) {
continue;
}
max = get(i);
argmax = i;
}
return argmax;
}
/**
* Returns a string representation of the vector.
* Used while printing to console and save vectors to file.
*
* @return vector as String
* @see FormatUtils#toString(float)
*/
@Override
public String toString() {
return getData().stream().map(FormatUtils::toString).collect(Collectors.joining(" "));
}
/**
* Added to debug purposes
*
* @param o vector
* @return boolean
*/
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Vector)) return false;
Vector vector = (Vector) o;
return Arrays.equals(data, vector.data);
}
/**
* Added to debug purposes
*
* @return int
*/
@Override
public int hashCode() {
return Arrays.hashCode(data);
}
}