summaryrefslogtreecommitdiff
path: root/gi/posterior-regularisation/prjava/src/hmm/HMMObjective.java
blob: 551210c0b6706531464191e6d9d37193fa158644 (plain)
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
package hmm;

import gnu.trove.TIntArrayList;
import optimization.gradientBasedMethods.ProjectedGradientDescent;
import optimization.gradientBasedMethods.ProjectedObjective;
import optimization.gradientBasedMethods.stats.OptimizerStats;
import optimization.linesearch.ArmijoLineSearchMinimizationAlongProjectionArc;
import optimization.linesearch.InterpolationPickFirstStep;
import optimization.linesearch.LineSearchMethod;
import optimization.projections.SimplexProjection;
import optimization.stopCriteria.CompositeStopingCriteria;
import optimization.stopCriteria.ProjectedGradientL2Norm;
import optimization.stopCriteria.StopingCriteria;
import optimization.stopCriteria.ValueDifference;

public class HMMObjective extends ProjectedObjective{

	
	private static final double GRAD_DIFF = 3;
	public static double INIT_STEP_SIZE=10;
	public static double VAL_DIFF=2000;
	
	private HMM hmm;
	double[] newPoint  ;
	
	//posterior[sent num][tok num][tag]=index into lambda
	private int posteriorMap[][][];
	//projection[word][tag].get(occurence)=index into lambda
	private TIntArrayList projectionMap[][];

	//Size of the simplex
	public double scale=10;
	private SimplexProjection projection;
	
	private int wordFreq[];
	private static int MIN_FREQ=3;
	private int numWordsToProject=0;
	
	private int n_param;
	
	public  double loglikelihood;
	
	public HMMObjective(HMM h){
		hmm=h;
		
		countWords();
		buildMap();

		gradient=new double [n_param];
		projection = new SimplexProjection(scale);
		newPoint  = new double[n_param];
		setInitialParameters(new double[n_param]);
		
	}
	
	/**@brief counts word frequency in the corpus
	 * 
	 */
	private void countWords(){
		wordFreq=new int [hmm.emit[0].length];
		for(int i=0;i<hmm.data.length;i++){
			for(int j=0;j<hmm.data[i].length;j++){
				wordFreq[hmm.data[i][j]]++;
			}
		}
	}
	
	/**@brief build posterior and projection indices
	 * 
	 */
	private void buildMap(){
		//number of sentences hidden states and words
		int n_states=hmm.trans.length;
		int n_words=hmm.emit[0].length;
		int n_sents=hmm.data.length;
		
		n_param=0;
		posteriorMap=new int[n_sents][][];
		projectionMap=new TIntArrayList[n_words][];
		for(int sentNum=0;sentNum<n_sents;sentNum++){
			int [] data=hmm.data[sentNum];
			posteriorMap[sentNum]=new int[data.length][n_states];
			numWordsToProject=0;
			for(int i=0;i<data.length;i++){
				int word=data[i];
				for(int state=0;state<n_states;state++){
					if(wordFreq[word]>MIN_FREQ){
						if(projectionMap[word]==null){
							projectionMap[word]=new TIntArrayList[n_states];
						}
						
						posteriorMap[sentNum][i][state]=n_param;
						if(projectionMap[word][state]==null){
							projectionMap[word][state]=new TIntArrayList();
							numWordsToProject++;
						}
						projectionMap[word][state].add(n_param);
						n_param++;
					}else{
						
						posteriorMap[sentNum][i][state]=-1;
					}
				}
			}
		}
	}
	
	@Override
	public double[] projectPoint(double[] point) {
		// TODO Auto-generated method stub
		for(int i=0;i<projectionMap.length;i++){
			
			if(projectionMap[i]==null){
				//this word is not constrained
				continue;
			}
			
			for(int j=0;j<projectionMap[i].length;j++){
				TIntArrayList instances=projectionMap[i][j];
				double[] toProject = new double[instances.size()];
				
				for (int k = 0; k < toProject.length; k++) {
					//	System.out.print(instances.get(k) + " ");
						toProject[k] = point[instances.get(k)];
				}
				
				projection.project(toProject);
				for (int k = 0; k < toProject.length; k++) {
					newPoint[instances.get(k)]=toProject[k];
				}
			}
		}
		return newPoint;
	}

	@Override
	public double[] getGradient() {
		// TODO Auto-generated method stub
		gradientCalls++;
		return gradient;
	}

	@Override
	public double getValue() {
		// TODO Auto-generated method stub
		functionCalls++;
		return loglikelihood;
	}
	

	@Override
	public String toString() {
		// TODO Auto-generated method stub
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < parameters.length; i++) {
			sb.append(parameters[i]+" ");
			if(i%100==0){
				sb.append("\n");
			}
		}
		sb.append("\n");
		/*
		for (int i = 0; i < gradient.length; i++) {
			sb.append(gradient[i]+" ");
			if(i%100==0){
				sb.append("\n");
			}
		}
		sb.append("\n");
		*/
		return sb.toString();
	}

	
	/**
	 * @param seq
	 * @return posterior probability of each transition
	 */
	public double [][][]forwardBackward(int sentNum){
		int [] seq=hmm.data[sentNum];
		int n_states=hmm.trans.length;
		double a[][]=new double [seq.length][n_states];
		double b[][]=new double [seq.length][n_states];
		
		int len=seq.length;
		
		boolean  constrained=
			(projectionMap[seq[0]]!=null);

		//initialize the first step
		for(int i=0;i<n_states;i++){
			a[0][i]=hmm.emit[i][seq[0]]*hmm.pi[i];
			if(constrained){
				a[0][i]*=
					Math.exp(- parameters[ posteriorMap[sentNum][0][i] ] );
			}
			b[len-1][i]=1;
		}
		
		loglikelihood+=Math.log(hmm.l1norm(a[0]));		
		hmm.l1normalize(a[0]);
		hmm.l1normalize(b[len-1]);
		
		//forward
		for(int n=1;n<len;n++){
			
			constrained=
				(projectionMap[seq[n]]!=null);
			
			for(int i=0;i<n_states;i++){
				for(int j=0;j<n_states;j++){
					a[n][i]+=hmm.trans[j][i]*a[n-1][j];
				}
				a[n][i]*=hmm.emit[i][seq[n]];
				
				if(constrained){
					a[n][i]*=
						Math.exp(- parameters[ posteriorMap[sentNum][n][i] ] );
				}
				
			}
			loglikelihood+=Math.log(hmm.l1norm(a[n]));
			hmm.l1normalize(a[n]);
		}
		
		//temp variable for e^{-\lambda}
		double factor=1;
		//backward
		for(int n=len-2;n>=0;n--){
			
			constrained=
				(projectionMap[seq[n+1]]!=null);
			
			for(int i=0;i<n_states;i++){
				for(int j=0;j<n_states;j++){
					
					if(constrained){
						factor=
							Math.exp(- parameters[ posteriorMap[sentNum][n+1][j] ] );
					}else{
						factor=1;
					}
					
					b[n][i]+=hmm.trans[i][j]*b[n+1][j]*hmm.emit[j][seq[n+1]]*factor;
					
				}
			}
			hmm.l1normalize(b[n]);
		}
		
		//expected transition 
		double p[][][]=new double [seq.length][n_states][n_states];
		for(int n=0;n<len-1;n++){
			
			constrained=
				(projectionMap[seq[n+1]]!=null);
			
			for(int i=0;i<n_states;i++){
				for(int j=0;j<n_states;j++){
					
					if(constrained){
						factor=
							Math.exp(- parameters[ posteriorMap[sentNum][n+1][j] ] );
					}else{
						factor=1;
					}
					
					p[n][i][j]=a[n][i]*hmm.trans[i][j]*
						hmm.emit[j][seq[n+1]]*b[n+1][j]*factor;
					
				}
			}

			hmm.l1normalize(p[n]);
		}
		return p;
	}

	public void optimizeWithProjectedGradientDescent(){
		LineSearchMethod ls =
			new ArmijoLineSearchMinimizationAlongProjectionArc
				(new InterpolationPickFirstStep(INIT_STEP_SIZE));
		
		OptimizerStats stats = new OptimizerStats();
		
		
		ProjectedGradientDescent optimizer = new ProjectedGradientDescent(ls);
		StopingCriteria stopGrad = new ProjectedGradientL2Norm(GRAD_DIFF);
		StopingCriteria stopValue = new ValueDifference(VAL_DIFF);
		CompositeStopingCriteria compositeStop = new CompositeStopingCriteria();
		compositeStop.add(stopGrad);
		compositeStop.add(stopValue);
		
		optimizer.setMaxIterations(10);
		updateFunction();
		boolean succed = optimizer.optimize(this,stats,compositeStop);
		System.out.println("Ended optimzation Projected Gradient Descent\n" + stats.prettyPrint(1));
		if(succed){
			System.out.println("Ended optimization in " + optimizer.getCurrentIteration());
		}else{
			System.out.println("Failed to optimize");
		}
	}
	
	@Override
	public void setParameters(double[] params) {
		super.setParameters(params);
		updateFunction();
	}
	
	private void updateFunction(){
		
		updateCalls++;
		loglikelihood=0;
	
		for(int sentNum=0;sentNum<hmm.data.length;sentNum++){
			double [][][]p=forwardBackward(sentNum);
			
			for(int n=0;n<p.length-1;n++){
				for(int i=0;i<p[n].length;i++){
					if(projectionMap[hmm.data[sentNum][n]]!=null){
						double posterior=0;
						for(int j=0;j<p[n][i].length;j++){
							posterior+=p[n][i][j];
						}
						gradient[posteriorMap[sentNum][n][i]]=-posterior;
					}
				}
			}
			
			//the last state
			int n=p.length-2;
			for(int i=0;i<p[n].length;i++){
				if(projectionMap[hmm.data[sentNum][n+1]]!=null){
					
					double posterior=0;
					for(int j=0;j<p[n].length;j++){
						posterior+=p[n][j][i];
					}
					gradient[posteriorMap[sentNum][n+1][i]]=-posterior;
				
				}
			}	
		}
		
	}
	
}