File: svm_trans.c

package info (click to toggle)
bow 20020213-10
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k, lenny
  • size: 2,596 kB
  • ctags: 2,871
  • sloc: ansic: 36,321; lisp: 1,072; cpp: 969; makefile: 569; perl: 495; sh: 101
file content (413 lines) | stat: -rw-r--r-- 10,317 bytes parent folder | download | duplicates (2)
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
/* Copyright (C) 1999 Greg Schohn - gcs@jprc.com */

/* ********************* svm_trans.c **********************
 * Code for transductive SVM's */
 
#include <bow/svm.h>

/* anything > 1 will be verbose... */
//#define DEBUG 1

int transduce_svm(bow_wv **docs, int *yvect, int *trans_yvect_store, 
		  double *weights, double *tvals, double *a_b, 
		  double **W, int ndocs, int ntrans, int *up_nsv) {
  double   cs_pos, cs_neg;
  float   *cvect;
  int      nlabeled;
  int      nsolns;
  int      nswitches;
  int      nsv;
  int     *old_yvect;
  double   tb;
  double   thresh;
  double  *tmpdv;
  float   *trans_cvect;
  bow_wv **trans_docs; 
  int      trans_npos;   /* the # of docs that will get a + label */
  double  *trans_scores; /* copy of the scores after the first run */
  int     *trans_yvect;

  struct svm_smo_model smo_model; /* needed if smo is used... */
  int i,j,k;

#ifdef DEBUG  
  double *oldw;
#endif
#if DEBUG>2
  int foo;
#endif

  nsv = *up_nsv;
  nlabeled = ndocs-ntrans;

  trans_docs = &(docs[nlabeled]);
  trans_yvect = &(yvect[nlabeled]);
  /* don't want to crush existing labels? - so copy them */
  old_yvect = (int *) malloc(sizeof(int)*ntrans);

  for (i=0; i<ntrans; i++) {
    old_yvect[i] = trans_yvect[i];
  }

  cvect = (float *) malloc(sizeof(float)*ndocs);
  trans_cvect = &(cvect[nlabeled]);

  for (i=0; i<nlabeled; i++) {
    cvect[i] = (float) svm_C;
  }

  /* this is sortof necessary.  Because of the transduction algorithm, 
   * previous weights won't help since the bounds are iteratively 
   * increased at each step (ie. the inflow will be totally different
   * than the outflow).  */
  nsv = 0;
  /* get the values for the unlabeled docs later */
  for (i=0; i<nlabeled; i++) {
    weights[i] = 0.0;
    tvals[i] = 0.0;
  }

  /* this is done to simplify some of the higher level fns. */
  if (*W) {
    free(*W);
  }
  *W = NULL;

  /* create the initial model */
  solve_svm(docs, yvect, weights, &tb, W, nlabeled, tvals, cvect, &nsv);

  tmpdv = (double *) malloc(sizeof(double)*ntrans);
  trans_scores = (double *) malloc(sizeof(double)*ntrans);

  trans_npos = 0;
  /* classify all of the unlabeled docs */
  for (i=0; i<ntrans; i++) {
    if (svm_kernel_type == 0) {
      tmpdv[i] = evaluate_model_hyperplane(*W, tb, trans_docs[i]);
    } else {
      tmpdv[i] = evaluate_model_cache(docs, weights, yvect, tb, trans_docs[i], nsv);
    }
    trans_scores[i] = tmpdv[i];

    if (svm_trans_nobias) {
      trans_yvect[i] = (tmpdv[i] > 0.0) ? 1 : -1;
      if (tmpdv[i] > 0.0) {
	trans_npos ++;
      }
    }
  }

  if (!svm_trans_nobias) {
    qsort(tmpdv, ntrans, sizeof(double), d_cmp);

    if (svm_trans_npos) {
      /* sort happens in ascending order */
      thresh = tmpdv[ntrans-svm_trans_npos];
      trans_npos = svm_trans_npos;
    } else {
      /* k = npos in training set */
      for (i=k=0; i<nlabeled; i++) {
	if (yvect[i] > 0) {
	  k++;
	}
      }
      trans_npos = (int) ((k*ntrans)/nlabeled);
      thresh = tmpdv[ntrans-trans_npos];
    }
  } else {
    thresh = 0.0;
  }

#define EPSILON_CSTAR 1e-2
  cs_neg = EPSILON_CSTAR;
  cs_pos = EPSILON_CSTAR * (ntrans - trans_npos + 1)/trans_npos;

  for (i=k=0; i<ntrans; i++) {
    if (trans_scores[i] >= thresh && k < trans_npos) {
      trans_yvect[i] = 1;
      cvect[i+nlabeled] = cs_pos;
      k++;
    } else {
      trans_yvect[i] = -1;
      cvect[i+nlabeled] = cs_neg;
    }
  }

  free(trans_scores);

#if DEBUG > 1
  printf("trans_yvect: %d: ", foo++);
  for (i=0; i<ndocs-nlabeled; i++) {
    printf("%d ", trans_yvect[i]);
  }
  printf("\n");

  printf("trans cycle %d: ", foo++);
  for (i=0; i<ndocs; i++) {
    printf("%f ",weights[i]);
  }
  printf("\n");
#endif

  /* initialize what the tvect should look like for the unlabeled docs */
  if (svm_use_smo) {
    /* nothing needs to be done to the tvals since they are only valid for sv's */
    for (i=nlabeled; i<ndocs; i++) {
      weights[i] = 0.0;
    }
    
    /* setup model for future use */
    /* only smo_evaluate_error uses this so only that info needs to be setup */
    smo_model.docs = docs;
    smo_model.yvect = yvect;
    smo_model.ndocs = ndocs;
    smo_model.weights = weights;
  } else {
    for (i=nlabeled; i<ndocs; i++) {
      tvals[i] = 0.0;
      for (j=k=0; j<nsv; k++) {
	if (weights[k] > 0.0) {
	  tvals[i] += weights[k]*yvect[k]*svm_kernel_cache(docs[k],docs[i]);
	  j++;
	}
      }
    }
  }

  nsolns = 1;
  nswitches = 0;

  /* this will need to be done more intelligently since this fn can be in
   * an inner loop (active learning) */
  while ((cs_pos < svm_trans_cstar) || (cs_neg < svm_trans_cstar)) {
    int maxp=0, maxn=0;
    double maxpv, maxnv;

    /* switch loop */
    do {
      if (!svm_trans_smart_vals) {
	nsv = 0;
	/* get the values for the unlabeled docs later */
	for (i=0; i<ndocs; i++) {
	  weights[i] = 0.0;
	  tvals[i] = 0.0;
	}
	free(*W);
	*W = NULL;
      } else {
	/* if we don't need this - get rid of it */
	if (svm_kernel_type == 0 && 
	    (!svm_use_smo || !(nsolns % svm_trans_hyp_refresh))) {
	  free(*W);
	  *W = NULL;
	}
      }

      nsolns ++;
      solve_svm(docs, yvect, weights, &tb, W, ndocs, tvals, cvect, &nsv);

      /* this block should be replaced by fns to convert tmp values
       * into error values */
      for (i=0; i<ntrans; i++) {
	if (svm_kernel_type == 0) {
	  tmpdv[i] = evaluate_model_hyperplane(*W, tb, trans_docs[i]);
	} else {
	  tmpdv[i] = evaluate_model_cache(docs, weights, yvect, tb, trans_docs[i], nsv);
	}
	if (trans_yvect[i] == 1 && tmpdv[i] < 1.0) {
	  tmpdv[i] = 1.0 - tmpdv[i];
	} else if (trans_yvect[i] == -1 && tmpdv[i] > -1.0) {
	  tmpdv[i] = tmpdv[i] + 1.0;
	} else {
	  tmpdv[i] = 0.0;
	}
      }

      for (i=0, maxnv=maxpv=0.0; i<ntrans; i++) {
	if (trans_yvect[i] > 0) {
	  if (tmpdv[i] > maxpv) {
	    maxpv = tmpdv[i];
	    maxp = i;
	  }
	} else {
	  if (tmpdv[i] > maxnv) {
	    maxnv = tmpdv[i];
	    maxn = i;
	  }
	}
      }

      /* switch the largest 2 */
      if (maxpv > 0.0 && maxnv > 0.0 && maxpv + maxnv > 2.0) {
	nswitches++;
	//printf("switching %d & %d\n",maxp,maxn);
	if (bow_verbosity_level > 2) {
	  fprintf(stderr,"  switching %d & %d\n",maxp,maxn);
	} else {
	  fprintf(stderr,"\r\t\t\t\t\t\t\t\t\t\t\t\t\t\tswitch #%d",nswitches);
	}
	trans_yvect[maxp] *= -1;
	trans_yvect[maxn] *= -1;

	/* need to also fix the hyperplane */
	if (svm_kernel_type == 0 && (nsolns % svm_trans_hyp_refresh)) {
	  for (j=maxp,k=0; k<2; j=maxn,k++) {
	    for (i=0; i<trans_docs[j]->num_entries; i++) {
	      (*W)[trans_docs[j]->entry[i].wi] += 
		2 * trans_yvect[j] * weights[j+nlabeled] * trans_docs[j]->entry[i].weight;
	    }
	  }
	}

	if (svm_trans_smart_vals) {
	  if (svm_use_smo) {
	    double wi, wj;
	    int yi, yj;
	    yi = trans_yvect[maxn];
	    yj = trans_yvect[maxp];
	    wi = weights[nlabeled+maxn];
	    wj = weights[nlabeled+maxp];

	    /* set tmp vals */
	    for (k=0; k<ndocs; k++) {
	      if ((weights[k] < cvect[k] - svm_epsilon_a) && (weights[k] > svm_epsilon_a)) {
		tvals[k] += 2*(wi*yi*svm_kernel_cache(docs[k],trans_docs[maxn]) + 
			       wj*yj*svm_kernel_cache(docs[k],trans_docs[maxp]));
	      }
	    }

#ifdef DEBUG
	    /* sanity check */
	    smo_model.W = *W;
	    for (i=0; i<ndocs; i++) {
	      if ((weights[i] < cvect[i] - svm_epsilon_a) && (weights[i] > svm_epsilon_a)) {
		int tmp = tvals[i] - smo_evaluate_error(&smo_model, i);
		if (tmp < 0) tmp *= -1;
		if (tmp > 0.1) {
		  double herror, verror;
		  herror = smo_evaluate_error(&smo_model, i);
		  svm_kernel_type = 1;
		  verror = smo_evaluate_error(&smo_model, i);
		  fprintf(stderr, "bad temporary (i=%d, tv[i]=%f, actual_H=%f, actual_V=%f)\n",i,tvals[i],
			  herror, verror);
		  svm_kernel_type = 0;
		  fflush(stderr);
		  kill(getpid(),SIGSTOP);
		}
	      }
	    }
#endif
	  } else {
	    double wn, wp;
	    wn = weights[maxn+nlabeled] * trans_yvect[maxn];
	    wp = weights[maxp+nlabeled] * trans_yvect[maxp];
	  
	    for (k=0; k<ndocs; k++) {
	      tvals[k] += 2*(wn*svm_kernel_cache(docs[k],trans_docs[maxn]) + 
			     wp*svm_kernel_cache(docs[k],trans_docs[maxp]));
	    }
	  }
	}
      } else {
	break;
      }
    } while (1);

#if DEBUG > 1
    printf("trans cycle %d: ", foo++);
    for (i=0; i<ndocs; i++) {
      printf("%f ",weights[i]);
    }
    printf("\n");
#endif
    
    /* set the proper tvals for the unlabeled docs 
     * (since they will no longer be bound) */
    if (svm_use_smo) {
      smo_model.W = *W;
      for (i=nlabeled; i<ndocs; i++) {
	if ((weights[i] > cvect[i] - svm_epsilon_a) && 
	    (weights[i] < svm_trans_cstar)) {
	  tvals[i] = smo_evaluate_error(&smo_model, i);
	}
      }
    }

    cs_pos = MIN(cs_pos*1.5,svm_trans_cstar);
    cs_neg = MIN(cs_neg*1.5,svm_trans_cstar);

    fprintf(stderr,"\r\t\t\t\b\b\bc=(%f,%f)    ",cs_pos,cs_neg);
    
    for (i=0; i<ntrans; i++) {
      if (trans_yvect[i] == 1) 
	trans_cvect[i] = cs_pos;
      else
	trans_cvect[i] = cs_neg;
    }
  }

#ifdef DEBUG
  /* debugging - sensitivity check */
  oldw = *W;
  *W = (double *) malloc(sizeof(double) * bow_num_words());
  for (i=0; i<bow_num_words(); i++) {
    (*W)[i] = 0;
  }

  for (i=0; i<ndocs; i++) {
    for (j=0; j<docs[i]->num_entries; j++) {
      (*W)[docs[i]->entry[j].wi] += yvect[i] * weights[i] * docs[i]->entry[j].weight;
    }
  }

  for (i=0; i<bow_num_words(); i++) {
    oldw[i] -= (*W)[i];
    if (oldw[i] > 0 ) oldw[i] *= -1;
    if (oldw[i] > 0.01) {
      fprintf(stderr, "bad hyperplane (j=%d)\n",j);
      fflush(stderr);
      kill(getpid(),SIGSTOP);
    }
  }

#endif

  free(cvect);
  free(tmpdv);

#if DEBUG > 1
  fprintf(stderr, "trans_yvect1 ");
  for (i=0; i<ndocs;i++) {
    fprintf(stderr," %d",yvect[i]);
  }
  fprintf(stderr,"\n");
#endif

  if (trans_yvect_store) {
    for (i=0; i<ntrans; i++) {
      trans_yvect_store[i] = trans_yvect[i];
    }
  }

#if DEBUG > 1
  fprintf(stderr, "trans_yvect2 ");
  for (i=0; i<ntrans;i++) {
    fprintf(stderr," %d",trans_yvect_store[i]);
  }
  fprintf(stderr,"\n");
#endif

  for (i=0; i<ntrans; i++) {
    trans_yvect[i] = old_yvect[i];
  }

  free(old_yvect);

  *up_nsv = nsv;

  /* ?bug?  Need to add code to signal whether or not the weights have changed
   * its hard to believe that the weights don't change at least a 
   * little bit - right */
  return 1;
}