forked from hunterhector/cmu-script
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNegativeTrainer.java
272 lines (226 loc) · 10.7 KB
/
NegativeTrainer.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
package edu.cmu.cs.lti.cds.annotators.script.train;
import edu.cmu.cs.lti.cds.dist.GlobalUnigrmHwLocalUniformArgumentDist;
import edu.cmu.cs.lti.cds.ml.features.FeatureExtractor;
import edu.cmu.cs.lti.cds.model.ContextElement;
import edu.cmu.cs.lti.cds.model.LocalArgumentRepre;
import edu.cmu.cs.lti.cds.model.LocalEventMentionRepre;
import edu.cmu.cs.lti.cds.utils.DataPool;
import edu.cmu.cs.lti.cds.utils.VectorUtils;
import edu.cmu.cs.lti.script.type.Article;
import edu.cmu.cs.lti.script.type.EventMention;
import edu.cmu.cs.lti.script.type.Sentence;
import edu.cmu.cs.lti.uima.annotator.AbstractLoggingAnnotator;
import edu.cmu.cs.lti.utils.TokenAlignmentHelper;
import gnu.trove.iterator.TObjectDoubleIterator;
import gnu.trove.map.TObjectDoubleMap;
import gnu.trove.map.hash.TObjectDoubleHashMap;
import org.apache.commons.lang3.tuple.Pair;
import org.apache.uima.UimaContext;
import org.apache.uima.analysis_engine.AnalysisEngineProcessException;
import org.apache.uima.fit.util.JCasUtil;
import org.apache.uima.jcas.JCas;
import org.apache.uima.resource.ResourceInitializationException;
import java.util.ArrayList;
import java.util.List;
/**
* Created with IntelliJ IDEA.
* User: zhengzhongliu
* Date: 10/31/14
* Time: 5:52 PM
*/
public class NegativeTrainer extends AbstractLoggingAnnotator {
public static final String PARAM_NEGATIVE_NUMBERS = "negativeNumbers";
TokenAlignmentHelper align = new TokenAlignmentHelper();
FeatureExtractor extractor = new FeatureExtractor();
int miniBatchDocNum = 100;
int numNoise = 25;
int skipGramN = 2;
int numArguments = 3;
double stepSize = 0.01;
GlobalUnigrmHwLocalUniformArgumentDist noiseDist = new GlobalUnigrmHwLocalUniformArgumentDist();
TObjectDoubleMap<String> cumulativeGradient = new TObjectDoubleHashMap<>();
double cumulativeObjective = 0;
@Override
public void initialize(UimaContext aContext) throws ResourceInitializationException {
super.initialize(aContext);
numNoise = (Integer) aContext.getConfigParameterValue(PARAM_NEGATIVE_NUMBERS);
}
@Override
public void process(JCas aJCas) throws AnalysisEngineProcessException {
Article article = JCasUtil.selectSingle(aJCas, Article.class);
// try {
// StochasticNegativeTrainer.trainOut.write(progressInfo(aJCas) + "\n");
// } catch (IOException e) {
// e.printStackTrace();
// }
if (DataPool.blackListedArticleId.contains(article.getArticleName())) {
//ignore this blacklisted file;
// logger.info("Ignored black listed file");
return;
}
align.loadWord2Stanford(aJCas);
List<ContextElement> chain = new ArrayList<>();
List<LocalArgumentRepre> arguments = new ArrayList<>();
for (Sentence sent : JCasUtil.select(aJCas, Sentence.class)) {
for (EventMention mention : JCasUtil.selectCovered(EventMention.class, sent)) {
LocalEventMentionRepre eventRep = LocalEventMentionRepre.fromEventMention(mention, align);
chain.add(new ContextElement(sent, eventRep));
for (LocalArgumentRepre arg : eventRep.getArgs()) {
arguments.add(arg);
}
}
}
//for each sample
for (int sampleIndex = 0; sampleIndex < chain.size(); sampleIndex++) {
ContextElement realSample = chain.get(sampleIndex);
TObjectDoubleMap<String> features = extractor.getFeatures(chain, realSample, sampleIndex, skipGramN, false);
Sentence sampleSent = realSample.getSent();
//generate noise samples
List<TObjectDoubleMap<String>> noiseSamples = new ArrayList<>();
for (int i = 0; i < numNoise; i++) {
Pair<LocalEventMentionRepre, Double> noise = noiseDist.draw(arguments, numArguments);
TObjectDoubleMap<String> noiseFeature = extractor.getFeatures(chain, new ContextElement(sampleSent, noise.getLeft()), sampleIndex, skipGramN, true);
if (noiseFeature != null) {
noiseSamples.add(noiseFeature);
}
}
//cumulative the gradient so far, and compute sample cost
double cumulativeObjective = gradientAscent(noiseSamples, features);
this.cumulativeObjective += cumulativeObjective;
}
DataPool.numSampleProcessed++;
if (DataPool.numSampleProcessed % miniBatchDocNum == 0) {
logger.info("Features learnt " + DataPool.weights.size());
logger.info("Processed " + DataPool.numSampleProcessed + " samples");
logger.info("Average gain for previous batch is : " + cumulativeObjective / miniBatchDocNum);
cumulativeObjective = 0;
update();
}
}
@Override
public void collectionProcessComplete() throws AnalysisEngineProcessException {
logger.info("Finish one epoch, totally " + DataPool.numSampleProcessed + " samples processed so far");
logger.info("Features stored " + DataPool.weights.size());
logger.info("Average cumulative gain for the residual batch: " + cumulativeObjective / (DataPool.numSampleProcessed % miniBatchDocNum));
update();
}
private double gradientAscent(List<TObjectDoubleMap<String>> noiseSamples, TObjectDoubleMap<String> dataSample) {
//start by assigning gradient as x_w
// g = x_w
TObjectDoubleMap<String> gradient = dataSample;
double scoreTrue = VectorUtils.dotProd(dataSample, DataPool.weights);
double sigmoidTrue = sigmoid(scoreTrue);
// try {
// StochasticNegativeTrainer.trainOut.write("Sigmoid true for " + dataSample + " is " + scoreTrue + "\n");
// } catch (IOException e) {
// e.printStackTrace();
// }
//multiple x_w by (1- sigmoid)
// g = ( Lw(u) - sigmoid(\theta * x_w) ) x_w, where Lw(u) = 1
VectorUtils.vectorScalarProduct(gradient, (1 - sigmoidTrue));
//calculate local objective
// log (sigmoid( score of true))
double localObjective = Math.log(sigmoidTrue);
for (TObjectDoubleMap<String> noiseSample : noiseSamples) {
double scoreNoise = VectorUtils.dotProd(noiseSample, DataPool.weights);
double sigmoidNoise = sigmoid(scoreNoise);
// try {
// StochasticNegativeTrainer.trainOut.write("Sigmoid noise for " + noiseSample + " is " + scoreNoise + "\n");
// } catch (IOException e) {
// e.printStackTrace();
// }
TObjectDoubleMap<String> noiseFeatures = noiseSample;
// log (sigmoid( - score of noise))
// i.e log ( 1 - sigmoid(score of noise))
localObjective += Math.log(1 - sigmoidNoise);
//multiple sigmiod_noise by noise features x_w'
// g += ( Lw'(u) - sigmoid(\theta * x_w') ) x_w', where Lw(u) = 0
//note that this directly change the noise feature itself
VectorUtils.vectorScalarProduct(noiseFeatures, sigmoidNoise);
VectorUtils.vectorMinus(gradient, noiseFeatures);
}
//update the cumulative gradient;
for (TObjectDoubleIterator<String> iter = gradient.iterator(); iter.hasNext(); ) {
iter.advance();
cumulativeGradient.adjustOrPutValue(iter.key(), iter.value(), iter.value());
// try {
// StochasticNegativeTrainer.trainOut.write(iter.key() + " " + iter.value() + "\n");
// } catch (IOException e) {
// e.printStackTrace();
// }
}
//return the objective
return localObjective;
}
private double sigmoid(double x) {
return 1 / (1 + Math.exp(-x));
}
private void update() {
// adaDeltaUpdate(1e-3, 0.95);
// adaGradUpdate(0.01);
stepSizeUpdate();
}
private void stepSizeUpdate() {
// update parameters
for (TObjectDoubleIterator<String> iter = cumulativeGradient.iterator(); iter.hasNext(); ) {
iter.advance();
double u = stepSize * iter.value();
// try {
// StochasticNegativeTrainer.trainOut.write("Update for " + iter.key() + " is " + u + " : " + stepSize + "*" + iter.value() + "\n");
// } catch (IOException e) {
// e.printStackTrace();
// }
DataPool.weights.adjustOrPutValue(iter.key(), u, u);
}
// empty the cumulative gradient
cumulativeGradient.clear();
}
private void adaGradUpdate(double eta) {
for (TObjectDoubleIterator<String> iter = cumulativeGradient.iterator(); iter.hasNext(); ) {
iter.advance();
double g = iter.value();
if (g != 0) {
double gSq = g * g;
double cumulativeGsq = DataPool.adaGradDelGradientSq.adjustOrPutValue(iter.key(), gSq, gSq);
double update = eta * g / Math.sqrt(cumulativeGsq);
if (Double.isNaN(g)) {
System.out.println(iter.key() + " " + iter.value() + update);
}
DataPool.weights.adjustOrPutValue(iter.key(), update, update);
}
}
}
//this implementation has some problems
private void adaDeltaUpdate(double decay, double epsilon) {
// update parameters
for (TObjectDoubleIterator<String> iter = cumulativeGradient.iterator(); iter.hasNext(); ) {
iter.advance();
//get the gradient
double g = iter.value();
double accum_g;
//accumulate gradient for adaDelta
if (DataPool.deltaGradientSq.containsKey(iter.key())) {
accum_g = DataPool.deltaGradientSq.get(iter.key()) * decay + (1 - decay) * g * g;
} else {
accum_g = (1 - decay) * g * g;
}
DataPool.deltaGradientSq.put(iter.key(), accum_g);
//compute delta i.e. step size
double delta;
//multiply update by RMS[delta X]_{t-1}
if (DataPool.deltaVarSq.containsKey(iter.key())) {
double oldDeltaVar = DataPool.deltaVarSq.get(iter.key());
delta = g * (Math.sqrt(oldDeltaVar) + epsilon) / Math.sqrt(accum_g + epsilon);
DataPool.deltaVarSq.put(iter.key(), oldDeltaVar * decay + (1 - decay) * delta * delta);
} else {
delta = g * epsilon / Math.sqrt(accum_g + epsilon);
DataPool.deltaVarSq.put(iter.key(), (1 - decay) * delta * delta);
}
System.out.println("delta " + delta);
//update weight for feature
DataPool.weights.adjustOrPutValue(iter.key(), delta, delta);
}
// empty the cumulative gradient
cumulativeGradient.clear();
}
}