-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
Word2VecPerformer.java
425 lines (318 loc) · 13.9 KB
/
Word2VecPerformer.java
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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
/*
*
* * Copyright 2015 Skymind,Inc.
* *
* * Licensed under the Apache License, Version 2.0 (the "License");
* * you may not use this file except in compliance with the License.
* * You may obtain a copy of the License at
* *
* * http://www.apache.org/licenses/LICENSE-2.0
* *
* * Unless required by applicable law or agreed to in writing, software
* * distributed under the License is distributed on an "AS IS" BASIS,
* * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* * See the License for the specific language governing permissions and
* * limitations under the License.
*
*/
package org.deeplearning4j.models.word2vec;
import org.apache.commons.math3.util.FastMath;
import org.canova.api.conf.Configuration;
import org.deeplearning4j.models.embeddings.inmemory.InMemoryLookupTable;
import org.deeplearning4j.scaleout.aggregator.JobAggregator;
import org.deeplearning4j.scaleout.api.statetracker.StateTracker;
import org.deeplearning4j.scaleout.job.Job;
import org.deeplearning4j.scaleout.perform.WorkerPerformer;
import org.deeplearning4j.scaleout.perform.WorkerPerformerFactory;
import org.deeplearning4j.scaleout.statetracker.hazelcast.HazelCastStateTracker;
import org.deeplearning4j.text.invertedindex.InvertedIndex;
import org.nd4j.linalg.api.buffer.DataBuffer;
import org.nd4j.linalg.api.ndarray.INDArray;
import org.nd4j.linalg.factory.Nd4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.atomic.AtomicLong;
/**
* Base line word 2 vec performer
*
* @author Adam Gibson
*/
public class Word2VecPerformer implements WorkerPerformer {
private int vectorLength = 50;
public final static String NAME_SPACE = "org.deeplearning4j.scaleout.perform.models.word2vec";
public final static String VECTOR_LENGTH = NAME_SPACE + ".length";
public final static String ADAGRAD = NAME_SPACE + ".adagrad";
public final static String NEGATIVE = NAME_SPACE + ".negative";
public final static String NUM_WORDS = NAME_SPACE + ".numwords";
public final static String TABLE = NAME_SPACE + ".table";
public final static String WINDOW = NAME_SPACE + ".window";
public final static String ALPHA = NAME_SPACE + ".alpha";
public final static String MIN_ALPHA = NAME_SPACE + ".minalpha";
public final static String TOTAL_WORDS = NAME_SPACE + ".totalwords";
public final static String NUM_WORDS_SO_FAR = NAME_SPACE + ".wordssofar";
public final static String ITERATIONS = NAME_SPACE + ".iterations";
double[] expTable = new double[1000];
static double MAX_EXP = 6;
private boolean useAdaGrad = false;
private double negative = 5;
private int numWords = 1;
private INDArray table;
private int window = 5;
private AtomicLong nextRandom = new AtomicLong(5);
private double alpha = 0.025;
private double minAlpha = 1e-2;
private int totalWords = 1;
private int iterations = 5;
private StateTracker stateTracker;
private static final Logger log = LoggerFactory.getLogger(Word2VecPerformer.class);
private int lastChecked = 0;
public Word2VecPerformer(StateTracker stateTracker) {
this.stateTracker = stateTracker;
}
public Word2VecPerformer() {}
@Override
public void perform(Job job) {
if(job.getWork() instanceof Word2VecWork) {
double numWordsSoFar = stateTracker.count(NUM_WORDS_SO_FAR);
Word2VecWork work = (Word2VecWork) job.getWork();
if(work == null)
return;
List<List<VocabWord>> sentences = work.getSentences();
double alpha2 = Math.max(minAlpha, alpha * (1 - (1.0 * numWordsSoFar / (double) totalWords)));
int totalNewWords = 0;
for(List<VocabWord> sentence : sentences) {
for(int i = 0; i < iterations; i++)
trainSentence(sentence, work, alpha2);
totalNewWords += sentence.size();
}
double newWords = totalNewWords + numWordsSoFar;
double diff = Math.abs(newWords - lastChecked);
if(diff >= 10000) {
lastChecked = (int) newWords;
log.info("Words so far " + newWords + " out of " + totalWords);
}
job.setResult((Serializable) Arrays.asList(work.addDeltas()));
stateTracker.increment(NUM_WORDS_SO_FAR,totalNewWords);
}
else if(job.getWork() instanceof Collection) {
double numWordsSoFar = stateTracker.count(NUM_WORDS_SO_FAR);
Collection<Word2VecWork> coll = (Collection<Word2VecWork>) job.getWork();
double alpha2 = Math.max(minAlpha, alpha * (1 - (1.0 * numWordsSoFar / (double) totalWords)));
int totalNewWords = 0;
List<Word2VecResult> deltas = new ArrayList<>();
for(Word2VecWork work : coll) {
List<List<VocabWord>> sentences = work.getSentences();
for(List<VocabWord> sentence : sentences) {
trainSentence(sentence,work,alpha2);
totalNewWords += sentence.size();
deltas.add(work.addDeltas());
}
}
double newWords = totalNewWords + numWordsSoFar;
double diff = Math.abs(newWords - lastChecked);
if(diff >= 10000) {
lastChecked = (int) newWords;
log.info("Words so far " + newWords + " out of " + totalWords);
}
job.setResult((Serializable) deltas);
stateTracker.increment(NUM_WORDS_SO_FAR,totalNewWords);
}
}
@Override
public void update(Object... o) {
}
@Override
public void setup(Configuration conf) {
vectorLength = conf.getInt(VECTOR_LENGTH,50);
useAdaGrad = conf.getBoolean(ADAGRAD, false);
negative = conf.getFloat(NEGATIVE, 5);
numWords = conf.getInt(NUM_WORDS, 1);
window = conf.getInt(WINDOW, 5);
alpha = conf.getFloat(ALPHA, 0.025f);
minAlpha = conf.getFloat(MIN_ALPHA, 1e-2f);
totalWords = conf.getInt(NUM_WORDS,1);
iterations = conf.getInt(ITERATIONS,5);
initExpTable();
String connectionString = conf.get(STATE_TRACKER_CONNECTION_STRING);
log.info("Creating state tracker with connection string "+ connectionString);
if(stateTracker == null)
try {
stateTracker = new HazelCastStateTracker(connectionString);
} catch (Exception e) {
e.printStackTrace();
}
if(negative > 0) {
try {
ByteArrayInputStream bis = new ByteArrayInputStream(conf.get(TABLE).getBytes());
DataInputStream dis = new DataInputStream(bis);
table = Nd4j.read(dis);
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* Configure the configuration based on the table and index
* @param table the table
* @param index the index
* @param conf the configuration
*/
public static void configure(InMemoryLookupTable table,InvertedIndex index,Configuration conf) {
conf.setInt(VECTOR_LENGTH, table.getVectorLength());
conf.setBoolean(ADAGRAD, table.isUseAdaGrad());
conf.setFloat(NEGATIVE, (float) table.getNegative());
conf.setFloat(ALPHA,(float) table.getLr().get());
conf.setInt(NUM_WORDS, index.totalWords());
conf.set(JobAggregator.AGGREGATOR, Word2VecJobAggregator.class.getName());
conf.set(WorkerPerformerFactory.WORKER_PERFORMER, Word2VecPerformerFactory.class.getName());
table.resetWeights();
if(table.getNegative() > 0) {
ByteArrayOutputStream bis = new ByteArrayOutputStream();
try {
DataOutputStream ois = new DataOutputStream(bis);
Nd4j.write(table.getTable(),ois);
} catch (IOException e) {
e.printStackTrace();
}
conf.set(Word2VecPerformer.TABLE,new String(bis.toByteArray()));
}
}
/**
* Train on a list of vocab words
* @param sentence the list of vocab words to train on
*/
public void trainSentence(final List<VocabWord> sentence,Word2VecWork work,double alpha) {
if(sentence == null || sentence.isEmpty())
return;
for(int i = 0; i < sentence.size(); i++) {
if(sentence.get(i).getWord().endsWith("STOP"))
continue;
nextRandom.set(nextRandom.get() * 25214903917L + 11);
skipGram(i, sentence, (int) nextRandom.get() % window,work,alpha);
}
}
/**
* Train via skip gram
* @param i
* @param sentence
*/
public void skipGram(int i,List<VocabWord> sentence, int b,Word2VecWork work,double alpha) {
final VocabWord word = sentence.get(i);
if(word == null || sentence.isEmpty())
return;
int end = window * 2 + 1 - b;
for(int a = b; a < end; a++) {
if(a != window) {
int c = i - window + a;
if(c >= 0 && c < sentence.size()) {
VocabWord lastWord = sentence.get(c);
iterateSample(work, word, lastWord,alpha);
}
}
}
}
/**
* Iterate on the given 2 vocab words
*
* @param w1 the first word to iterate on
* @param w2 the second word to iterate on
*/
public void iterateSample(Word2VecWork work,VocabWord w1, VocabWord w2,double alpha) {
if(w2 == null || w2.getIndex() < 0)
return;
if( work.getVectors().get(w2.getWord()) == null) {
log.warn("No vector found for word " + w2.getWord());
return;
}
if( work.getVectors().get(w1.getWord()) == null) {
log.warn("No vector found for word " + w1.getWord());
return;
}
//current word vector
INDArray l1 = work.getVectors().get(w2.getWord()).getSecond();
//error for current word and context
INDArray neu1e = Nd4j.create(vectorLength);
for(int i = 0; i < w1.getCodeLength(); i++) {
int code = w1.getCodes().get(i);
int point = w1.getPoints().get(i);
//other word vector
if(work.getIndexes().get(point) == null) {
//log.warn("Work index for point " + point + " was null");
continue;
}
if(work.getSyn1Vectors().get(work.getIndexes().get(point).getWord()) == null) {
log.warn("Syn1 vectors for " + work.getIndexes().get(point).getWord() + " was null");
continue;
}
INDArray syn1 = work.getSyn1Vectors().get(work.getIndexes().get(point).getWord());
double dot = Nd4j.getBlasWrapper().dot(l1,syn1);
if(dot < -MAX_EXP || dot >= MAX_EXP)
continue;
int idx = (int) ((dot + MAX_EXP) * ((double) expTable.length / MAX_EXP / 2.0));
if(idx >= expTable.length)
continue;
//score
double f = expTable[idx];
//gradient
double g = (1 - code - f) * (useAdaGrad ? w1.getGradient(i, alpha) : alpha);
if(neu1e.data().dataType() == DataBuffer.Type.DOUBLE) {
Nd4j.getBlasWrapper().axpy(g, syn1, neu1e);
Nd4j.getBlasWrapper().axpy(g, l1, syn1);
}
else {
Nd4j.getBlasWrapper().axpy((float) g, syn1, neu1e);
Nd4j.getBlasWrapper().axpy((float) g, l1, syn1);
}
}
//negative sampling
if(negative > 0) {
int target = w1.getIndex();
int label;
INDArray syn1Neg = work.getNegativeVectors().get(work.getIndexes().get(target).getWord()).getSecond();
for (int d = 0; d < negative + 1; d++) {
if (d == 0) {
label = 1;
} else {
nextRandom.set(nextRandom.get() * 25214903917L + 11);
target = table.getInt((int) (nextRandom.get() >> 16) % table.length());
if (target == 0)
target = (int) nextRandom.get() % (numWords - 1) + 1;
if (target == w1.getIndex())
continue;
label = 0;
}
double f = Nd4j.getBlasWrapper().dot(l1, syn1Neg);
double g;
if (f > MAX_EXP)
g = useAdaGrad ? w1.getGradient(target, (label - 1)) : (label - 1) * alpha;
else if (f < -MAX_EXP)
g = (label - 0) * (useAdaGrad ? w1.getGradient(target, alpha) : alpha);
else
g = useAdaGrad ? w1.getGradient(target, label - expTable[(int)((f + MAX_EXP) * (expTable.length / MAX_EXP / 2))]) : (label - expTable[(int)((f + MAX_EXP) * (expTable.length / MAX_EXP / 2))]) * alpha;
if(syn1Neg.data().dataType() == DataBuffer.Type.DOUBLE)
Nd4j.getBlasWrapper().axpy(g,neu1e,l1);
else
Nd4j.getBlasWrapper().axpy((float) g,neu1e,l1);
if(syn1Neg.data().dataType() == DataBuffer.Type.DOUBLE)
Nd4j.getBlasWrapper().axpy(g,syn1Neg,l1);
else
Nd4j.getBlasWrapper().axpy((float) g,syn1Neg,l1);
}
}
if(neu1e.data().dataType() == DataBuffer.Type.DOUBLE)
Nd4j.getBlasWrapper().axpy(1.0,neu1e,l1);
else
Nd4j.getBlasWrapper().axpy(1.0f,neu1e,l1);
}
private void initExpTable() {
for (int i = 0; i < expTable.length; i++) {
double tmp = FastMath.exp((i / (double) expTable.length * 2 - 1) * MAX_EXP);
expTable[i] = tmp / (tmp + 1.0);
}
}
}