Repository navigation
Expand file tree
/
Copy pathLearn.java
More file actions
383 lines (338 loc) · 12.1 KB
/
Copy pathLearn.java
File metadata and controls
383 lines (338 loc) · 12.1 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
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
package com.ansj.vec;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import com.ansj.vec.util.MapCount;
import com.ansj.vec.domain.HiddenNeuron;
import com.ansj.vec.domain.Neuron;
import com.ansj.vec.domain.WordNeuron;
import com.ansj.vec.util.Haffman;
public class Learn {
private Map<String, Neuron> wordMap = new HashMap<>();
/**
* 训练多少个特征
*/
private int layerSize = 200;
/**
* 上下文窗口大小
*/
private int window = 5;
private double sample = 1e-3;
private double alpha = 0.025;
private double startingAlpha = alpha;
public int EXP_TABLE_SIZE = 1000;
private Boolean isCbow = false;
private double[] expTable = new double[EXP_TABLE_SIZE];
private int trainWordsCount = 0;
private int MAX_EXP = 6;
public Learn(Boolean isCbow, Integer layerSize, Integer window, Double alpha, Double sample) {
createExpTable();
if (isCbow != null) {
this.isCbow = isCbow;
}
if (layerSize != null)
this.layerSize = layerSize;
if (window != null)
this.window = window;
if (alpha != null)
this.alpha = alpha;
if (sample != null)
this.sample = sample;
}
public Learn() {
createExpTable();
}
/**
* trainModel
* @throws IOException
*/
private void trainModel(File file) throws IOException {
try (BufferedReader br = new BufferedReader(
new InputStreamReader(new FileInputStream(file)))) {
String temp = null;
long nextRandom = 5;
int wordCount = 0;
int lastWordCount = 0;
int wordCountActual = 0;
while ((temp = br.readLine()) != null) {
if (wordCount - lastWordCount > 10000) {
System.out
.println("alpha:" + alpha + "\tProgress: "
+ (int) (wordCountActual / (double) (trainWordsCount + 1) * 100)
+ "%");
wordCountActual += wordCount - lastWordCount;
lastWordCount = wordCount;
alpha = startingAlpha * (1 - wordCountActual / (double) (trainWordsCount + 1));
if (alpha < startingAlpha * 0.0001) {
alpha = startingAlpha * 0.0001;
}
}
String[] strs = temp.split(" ");
wordCount += strs.length;
List<WordNeuron> sentence = new ArrayList<WordNeuron>();
for (int i = 0; i < strs.length; i++) {
Neuron entry = wordMap.get(strs[i]);
if (entry == null) {
continue;
}
// The subsampling randomly discards frequent words while keeping the ranking same
if (sample > 0) {
double ran = (Math.sqrt(entry.freq / (sample * trainWordsCount)) + 1)
* (sample * trainWordsCount) / entry.freq;
nextRandom = nextRandom * 25214903917L + 11;
if (ran < (nextRandom & 0xFFFF) / (double) 65536) {
continue;
}
}
sentence.add((WordNeuron) entry);
}
for (int index = 0; index < sentence.size(); index++) {
nextRandom = nextRandom * 25214903917L + 11;
if (isCbow) {
cbowGram(index, sentence, (int) nextRandom % window);
} else {
skipGram(index, sentence, (int) nextRandom % window);
}
}
}
System.out.println("Vocab size: " + wordMap.size());
System.out.println("Words in train file: " + trainWordsCount);
System.out.println("sucess train over!");
}
}
/**
* skip gram 模型训练
* @param sentence
* @param neu1
*/
private void skipGram(int index, List<WordNeuron> sentence, int b) {
// TODO Auto-generated method stub
WordNeuron word = sentence.get(index);
int a, c = 0;
for (a = b; a < window * 2 + 1 - b; a++) {
if (a == window) {
continue;
}
c = index - window + a;
if (c < 0 || c >= sentence.size()) {
continue;
}
double[] neu1e = new double[layerSize];//误差项
//HIERARCHICAL SOFTMAX
List<Neuron> neurons = word.neurons;
WordNeuron we = sentence.get(c);
for (int i = 0; i < neurons.size(); i++) {
HiddenNeuron out = (HiddenNeuron) neurons.get(i);
double f = 0;
// Propagate hidden -> output
for (int j = 0; j < layerSize; j++) {
f += we.syn0[j] * out.syn1[j];
}
if (f <= -MAX_EXP || f >= MAX_EXP) {
continue;
} else {
f = (f + MAX_EXP) * (EXP_TABLE_SIZE / MAX_EXP / 2);
f = expTable[(int) f];
}
// 'g' is the gradient multiplied by the learning rate
double g = (1 - word.codeArr[i] - f) * alpha;
// Propagate errors output -> hidden
for (c = 0; c < layerSize; c++) {
neu1e[c] += g * out.syn1[c];
}
// Learn weights hidden -> output
for (c = 0; c < layerSize; c++) {
out.syn1[c] += g * we.syn0[c];
}
}
// Learn weights input -> hidden
for (int j = 0; j < layerSize; j++) {
we.syn0[j] += neu1e[j];
}
}
}
/**
* 词袋模型
* @param index
* @param sentence
* @param b
*/
private void cbowGram(int index, List<WordNeuron> sentence, int b) {
WordNeuron word = sentence.get(index);
int a, c = 0;
List<Neuron> neurons = word.neurons;
double[] neu1e = new double[layerSize];//误差项
double[] neu1 = new double[layerSize];//误差项
WordNeuron last_word;
for (a = b; a < window * 2 + 1 - b; a++)
if (a != window) {
c = index - window + a;
if (c < 0)
continue;
if (c >= sentence.size())
continue;
last_word = sentence.get(c);
if (last_word == null)
continue;
for (c = 0; c < layerSize; c++)
neu1[c] += last_word.syn0[c];
}
//HIERARCHICAL SOFTMAX
for (int d = 0; d < neurons.size(); d++) {
HiddenNeuron out = (HiddenNeuron) neurons.get(d);
double f = 0;
// Propagate hidden -> output
for (c = 0; c < layerSize; c++)
f += neu1[c] * out.syn1[c];
if (f <= -MAX_EXP)
continue;
else if (f >= MAX_EXP)
continue;
else
f = expTable[(int) ((f + MAX_EXP) * (EXP_TABLE_SIZE / MAX_EXP / 2))];
// 'g' is the gradient multiplied by the learning rate
// double g = (1 - word.codeArr[d] - f) * alpha;
// double g = f*(1-f)*( word.codeArr[i] - f) * alpha;
double g = f * (1 - f) * (word.codeArr[d] - f) * alpha;
//
for (c = 0; c < layerSize; c++) {
neu1e[c] += g * out.syn1[c];
}
// Learn weights hidden -> output
for (c = 0; c < layerSize; c++) {
out.syn1[c] += g * neu1[c];
}
}
for (a = b; a < window * 2 + 1 - b; a++) {
if (a != window) {
c = index - window + a;
if (c < 0)
continue;
if (c >= sentence.size())
continue;
last_word = sentence.get(c);
if (last_word == null)
continue;
for (c = 0; c < layerSize; c++)
last_word.syn0[c] += neu1e[c];
}
}
}
/**
* 统计词频
* @param file
* @throws IOException
*/
private void readVocab(File file) throws IOException {
MapCount<String> mc = new MapCount<>();
try (BufferedReader br = new BufferedReader(
new InputStreamReader(new FileInputStream(file)))) {
String temp = null;
while ((temp = br.readLine()) != null) {
String[] split = temp.split(" ");
trainWordsCount += split.length;
for (String string : split) {
mc.add(string);
}
}
}
for (Entry<String, Integer> element : mc.get().entrySet()) {
wordMap.put(element.getKey(), new WordNeuron(element.getKey(), element.getValue(),
layerSize));
}
}
/**
* Precompute the exp() table
* f(x) = x / (x + 1)
*/
private void createExpTable() {
for (int i = 0; i < EXP_TABLE_SIZE; i++) {
expTable[i] = Math.exp(((i / (double) EXP_TABLE_SIZE * 2 - 1) * MAX_EXP));
expTable[i] = expTable[i] / (expTable[i] + 1);
}
}
/**
* 根据文件学习
* @param file
* @throws IOException
*/
public void learnFile(File file) throws IOException {
readVocab(file);
new Haffman(layerSize).make(wordMap.values());
//查找每个神经元
for (Neuron neuron : wordMap.values()) {
((WordNeuron)neuron).makeNeurons() ;
}
trainModel(file);
}
/**
* 保存模型
*/
public void saveModel(File file) {
// TODO Auto-generated method stub
try (DataOutputStream dataOutputStream = new DataOutputStream(new BufferedOutputStream(
new FileOutputStream(file)))) {
dataOutputStream.writeInt(wordMap.size());
dataOutputStream.writeInt(layerSize);
double[] syn0 = null;
for (Entry<String, Neuron> element : wordMap.entrySet()) {
dataOutputStream.writeUTF(element.getKey());
syn0 = ((WordNeuron) element.getValue()).syn0;
for (double d : syn0) {
dataOutputStream.writeFloat(((Double) d).floatValue());
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public int getLayerSize() {
return layerSize;
}
public void setLayerSize(int layerSize) {
this.layerSize = layerSize;
}
public int getWindow() {
return window;
}
public void setWindow(int window) {
this.window = window;
}
public double getSample() {
return sample;
}
public void setSample(double sample) {
this.sample = sample;
}
public double getAlpha() {
return alpha;
}
public void setAlpha(double alpha) {
this.alpha = alpha;
this.startingAlpha = alpha;
}
public Boolean getIsCbow() {
return isCbow;
}
public void setIsCbow(Boolean isCbow) {
this.isCbow = isCbow;
}
public static void main(String[] args) throws IOException {
Learn learn = new Learn();
long start = System.currentTimeMillis() ;
learn.learnFile(new File("library/xh.txt"));
System.out.println("use time "+(System.currentTimeMillis()-start));
learn.saveModel(new File("library/javaVector"));
}
}