Exemplo n.º 1
0
int main(int argc, char* argv[]) {

  double *w; /* weight vector */
  long m, i;
  double C, epsilon;
  LEARN_PARM learn_parm;
  KERNEL_PARM kernel_parm;
  char trainfile[1024];
  char modelfile[1024];
  int MAX_ITER;
  /* new struct variables */
  SVECTOR **fycache, *diff, *fy;
  EXAMPLE *ex;
	SAMPLE alldata;
  SAMPLE sample;
	SAMPLE val;
  STRUCT_LEARN_PARM sparm;
  STRUCTMODEL sm;
  
  double primal_obj;
  double stop_crit; 
	char itermodelfile[2000];

	/* self-paced learning variables */
	double init_spl_weight;
	double spl_weight;
	double spl_factor;
	int *valid_examples;
 

  /* read input parameters */
	my_read_input_parameters(argc, argv, trainfile, modelfile, &learn_parm, &kernel_parm, &sparm, 
													&init_spl_weight, &spl_factor); 

  epsilon = learn_parm.eps;
  C = learn_parm.svm_c;
  MAX_ITER = learn_parm.maxiter;

  /* read in examples */
  alldata = read_struct_examples(trainfile,&sparm);
  int ntrain = (int) round(1.0*alldata.n); /* no validation set */
	if(ntrain < alldata.n)
	{
 	 long *perm = randperm(alldata.n);
 	 sample = generate_train_set(alldata, perm, ntrain);
 	 val = generate_validation_set(alldata, perm, ntrain);
 	 free(perm);
	}
	else
	{
		sample = alldata;
	}
  ex = sample.examples;
  m = sample.n;
  
  /* initialization */
  init_struct_model(alldata,&sm,&sparm,&learn_parm,&kernel_parm); 

  w = create_nvector(sm.sizePsi);
  clear_nvector(w, sm.sizePsi);
  sm.w = w; /* establish link to w, as long as w does not change pointer */

  /* some training information */
  printf("C: %.8g\n", C);
	printf("spl weight: %.8g\n",init_spl_weight);
  printf("epsilon: %.8g\n", epsilon);
  printf("sample.n: %d\n", sample.n); 
  printf("sm.sizePsi: %ld\n", sm.sizePsi); fflush(stdout);


  /* prepare feature vector cache for correct labels with imputed latent variables */
  fycache = (SVECTOR**)malloc(m*sizeof(SVECTOR*));
  for (i=0;i<m;i++) {
    fy = psi(ex[i].x, ex[i].y, &sm, &sparm);
    diff = add_list_ss(fy);
    free_svector(fy);
    fy = diff;
    fycache[i] = fy;
  }

 	/* learn initial weight vector using all training examples */
	valid_examples = (int *) malloc(m*sizeof(int));     

  /* errors for validation set */

  double cur_loss, best_loss = DBL_MAX;
  int loss_iter;


	/* initializations */
	spl_weight = init_spl_weight;

	/* solve biconvex self-paced learning problem */
	primal_obj = alternate_convex_search(w, m, MAX_ITER, C, epsilon, fycache, ex, &sm, &sparm, valid_examples, spl_weight);
	printf("primal objective: %.4f\n", primal_obj);
	fflush(stdout);
	//alternate_convex_search(w, m, MAX_ITER, C, epsilon, fycache, ex, &sm, &sparm, valid_examples, spl_weight);
	int nValid = 0;
	for (i=0;i<m;i++) {
		if(valid_examples[i]) {
			nValid++;
		}
	}

		

	if(ntrain < alldata.n) {
		cur_loss = compute_current_loss(val,&sm,&sparm);
		printf("CURRENT LOSS: %f\n",cur_loss);
	}
  

  /* write structural model */
  write_struct_model(modelfile, &sm, &sparm);
  // skip testing for the moment  

  /* free memory */
  free_struct_sample(alldata);
	if(ntrain < alldata.n)
	{
		free(sample.examples);
		free(val.examples);
	}
  free_struct_model(sm, &sparm);
  for(i=0;i<m;i++) {
    free_svector(fycache[i]);
  }
  free(fycache);

	free(valid_examples);
   
  return(0); 
  
}
int main(int argc, char* argv[]) {

  // The file to create the online version of the code

  printf("Runs with F1 loss in the loss-augmented objective .. only positive data .. with weighting of Fscores .. no regions file");

//  double *w; /* weight vector */
  double C, epsilon, Cdash;
  LEARN_PARM learn_parm;
  KERNEL_PARM kernel_parm;
  char trainfile[1024];
  char modelfile[1024];
  int MAX_ITER;

  SAMPLE sample;
  STRUCT_LEARN_PARM sparm;
  STRUCTMODEL sm;

  /* read input parameters */
  my_read_input_parameters(argc, argv, trainfile, modelfile, &learn_parm, &kernel_parm, &sparm);

  epsilon = learn_parm.eps;
  C = learn_parm.svm_c;
  Cdash = learn_parm.Cdash;
  MAX_ITER = learn_parm.maxiter;

  /* read in examples */
  //strcpy(trainfile, "dataset/reidel_trainSVM.small.data");
  sample = read_struct_examples(trainfile,&sparm);
  
  /* initialization */
  init_struct_model(sample,&sm,&sparm,&learn_parm,&kernel_parm);

  // (OnlineSVM : Commenting 'w' as they are replaced by 'w_iters'
//  w = create_nvector(sm.sizePsi);
//  clear_nvector(w, sm.sizePsi);
//  sm.w = w; /* establish link to w, as long as w does not change pointer */

		double *zeroes = create_nvector(sm.sizePsi);
		clear_nvector(zeroes, sm.sizePsi);

//  printf("Addr. of w (init) %x\t%x\n",w,sm.w);

 		time_t time_start_full, time_end_full;
		int eid,totalEpochs=learn_parm.totalEpochs;
		int chunkid, numChunks=learn_parm.numChunks;
		double primal_obj_sum, primal_obj;
		char chunk_trainfile[1024];
		SAMPLE * chunk_dataset = (SAMPLE *) malloc(sizeof(SAMPLE)*numChunks);

		/**
			   * If we have ‘k’ instances and do ‘n’ epochs, after processing each chunk we update the weight.
			   * Since we do ‘k’ updates, we will have ‘k’ weight vectors after each epoch.
			   * After ‘n’ epochs, we will have ‘k*n’ weight vectors.
		*/
		// --------------------------------------------------------------------------------------------------------------------------------
		double ***w_iters = (double**) malloc(totalEpochs*sizeof(double**));
//		printf("--2: After 1st malloc -- %x; sz = %d\n", w_iters, totalEpochs*sizeof(double**));
		for(eid = 0; eid < totalEpochs; eid++){
			w_iters[eid] = (double*) malloc(numChunks*sizeof(double*));
//			printf("2.5... id = %d, .. allocated ... %x; sz = %d\n",eid, w_iters[eid],numChunks*sizeof(double*));
		}
		printf("--3: After 2nd malloc \n");
		for(eid = 0; eid < totalEpochs; eid++){
			for(chunkid = 0; chunkid < numChunks; chunkid++){
				w_iters[eid][chunkid] = create_nvector(sm.sizePsi);
//				printf("Confirming memory location : %x\n",w_iters[eid][chunkid]);
				clear_nvector(w_iters[eid][chunkid], sm.sizePsi);
			}
		}
		sm.w_iters = w_iters;
		printf("(ONLINE SVM) Completed the memory alloc for the parameters\n");
		// --------------------------------------------------------------------------------------------------------------------------------

		/**
		 * Having divided the dataset (X,Y) into set of 'k' chunks / sub-datasets (X_1,Y_1) ... (X_k, Y_k)
		 * Do the following do while routine for one set of datapoints (sub-datasets)
		 */
		// --------------------------------------------------------------------------------------------------------------------------------
		printf("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX Changed .... Calling Java to split dataset\n");
		char *cmd = malloc(1000);
		strcpy(cmd,"java -Xmx1G -cp java/bin:java/lib/* "
				" javaHelpers.splitDataset ");
		strcat(cmd, trainfile);
		strcat(cmd, " ");
		char numChunks_str[10]; sprintf(numChunks_str, "%d", numChunks);
		strcat(cmd, numChunks_str);
		strcat(cmd, " ");

		printf("Executing cmd : %s\n", cmd);fflush(stdout);
		system(cmd);
		// --------------------------------------------------------------------------------------------------------------------------------

		for(chunkid = 0; chunkid < numChunks; chunkid++)
		{
			memset(chunk_trainfile, 0, 1024);
			strcat(chunk_trainfile,trainfile);
			strcat(chunk_trainfile,".chunks/chunk."); // NOTE: Name hard-coded according to the convention used to create chunked files
			char chunkid_str[10];sprintf(chunkid_str, "%d", chunkid);
			strcat(chunk_trainfile,chunkid_str);
			printf("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX Changed .... Reading chunked dataset\n");
			printf("Chunk trainfile : %s\n",chunk_trainfile);
			chunk_dataset[chunkid] = read_struct_examples_chunk(chunk_trainfile);
		}

		time(&time_start_full);
		for(eid = 0; eid < totalEpochs; eid++)
		{
			printf("(ONLINE LEARNING) : EPOCH %d\n",eid);
			primal_obj_sum = 0.0;
			for(chunkid = 0; chunkid < numChunks; chunkid++) // NOTE: Chunkid starts from 1 and goes upto numChumks
			{

				int sz = sample.n / numChunks;
				int datasetStartIdx = (chunkid) * sz;
				int chunkSz = (numChunks-1 == chunkid) ? (sample.n - ((numChunks-1)*sz) ) : (sz);

				primal_obj = optimizeMultiVariatePerfMeasure(chunk_dataset[chunkid], datasetStartIdx, chunkSz,
						&sm, &sparm, C, Cdash, epsilon, MAX_ITER, &learn_parm, trainfile, w_iters, eid, chunkid, numChunks, zeroes);

				printf("(ONLINE LEARNING) : FINISHED PROCESSING CHUNK (PSEUDO-DATAPOINT) %d of %d\n",chunkid+1, numChunks);
				primal_obj_sum += primal_obj;
				printf("(OnlineSVM) : Processed pseudo-datapoint -- primal objective sum: %.4f\n", primal_obj_sum);

			}

			// After the completion of one epoch, warm start the 2nd epoch with the values of the
			// weight vectors seen at the end of the last chunk in previous epoch
			if(eid + 1 < totalEpochs){
				 //init w_iters[eid+1][0] to w_iters[eid][numChunks-1]
				 copy_vector(w_iters[eid+1][0], w_iters[eid][numChunks-1], sm.sizePsi);
				 printf("(ONLINE LEARNING) : WARM START ACROSS EPOCHS ..... DONE....\n");
			}

			printf("(OnlineSVM) : EPOCH COMPLETE -- primal objective: %.4f\n", primal_obj);
			printf("(ONLINE LEARNING) : EPOCH %d DONE! .....\n",eid);

		}

		time(&time_end_full);
		char msg[20];
		sprintf(msg,"(ONLINE LEARNING) : Total Time Taken : ");
		print_time(time_start_full, time_end_full, msg);

printf("(ONLINE LEARNING) Reached here\n");
  /* write structural model */
  write_struct_model_online(modelfile, &sm, &sparm, totalEpochs, numChunks);
  // skip testing for the moment  
  printf("(ONLINE LEARNING) Complete dumping\n");

  /* free memory */ //TODO: Need to change this ...
  free_struct_sample(sample);
  free_struct_model(sm, &sparm);

  return(0); 
  
}
int main(int argc, char* argv[]) {

    double *w; /* weight vector */
    int outer_iter;
    long m, i;
    double C, epsilon;
    LEARN_PARM learn_parm;
    KERNEL_PARM kernel_parm;
    char trainfile[1024];
    char modelfile[1024];
    int MAX_ITER;
    /* new struct variables */
    SVECTOR **fycache, *diff, *fy;
    EXAMPLE *ex;
    SAMPLE sample;
    STRUCT_LEARN_PARM sparm;
    STRUCTMODEL sm;

    //double decrement;
    double primal_obj;//, last_primal_obj;
    //double cooling_eps;
    //double stop_crit;

    DebugConfiguration::VerbosityLevel = VerbosityLevel::None;

    /* read input parameters */
    my_read_input_parameters(argc, argv, trainfile, modelfile, &learn_parm, &kernel_parm, &sparm);

    epsilon = learn_parm.eps;
    C = learn_parm.svm_c;
    MAX_ITER = learn_parm.maxiter;

    /* read in examples */
    sample = read_struct_examples(trainfile,&sparm);
    ex = sample.examples;
    m = sample.n;

    /* initialization */
    init_struct_model(sample,&sm,&sparm,&learn_parm,&kernel_parm);
    w = sm.w;

    //w = create_nvector(sm.sizePsi);
    //clear_nvector(w, sm.sizePsi);
    //sm.w = w; /* establish link to w, as long as w does not change pointer */

    /* some training information */
    printf("C: %.8g\n", C);
    printf("epsilon: %.8g\n", epsilon);
    printf("sample.n: %ld\n", sample.n);
    printf("sm.sizePsi: %ld\n", sm.sizePsi);
    fflush(stdout);

    /* impute latent variable for first iteration */
    init_latent_variables(&sample,&learn_parm,&sm,&sparm);

    /* prepare feature vector cache for correct labels with imputed latent variables */
    fycache = (SVECTOR**)malloc(m*sizeof(SVECTOR*));
    for (i=0; i<m; i++) {
        fy = psi(ex[i].x, ex[i].y, ex[i].h, &sm, &sparm);

        /* DEBUG */
        printf("true_psi[%d]=", i);
        for (int j = 0; j < sm.sizePsi; ++j)
            printf("%.4lf ", fy->words[j].weight);
        printf("\n");

        diff = add_list_ss(fy);
        free_svector(fy);
        fy = diff;
        fycache[i] = fy;
    }

    /* outer loop: latent variable imputation */
    outer_iter = 1;
    //last_primal_obj = 0;
    //decrement = 0;
    //cooling_eps = 0.5*C*epsilon;
    //while ((outer_iter<=MIN_OUTER_ITER)||((!stop_crit)&&(outer_iter<MAX_OUTER_ITER))) {
    while (outer_iter<MAX_OUTER_ITER) {
        LearningTracker::NextOuterIteration();
        printf("OUTER ITER %d\n", outer_iter);
        /* cutting plane algorithm */
        primal_obj = cutting_plane_algorithm(w, m, MAX_ITER, C, /*cooling_eps, */fycache, ex, &sm, &sparm);

        /* compute decrement in objective in this outer iteration */
        /*
        decrement = last_primal_obj - primal_obj;
        last_primal_obj = primal_obj;
        printf("primal objective: %.4f\n", primal_obj);
        printf("decrement: %.4f\n", decrement); fflush(stdout);
        stop_crit = (decrement<C*epsilon)&&(cooling_eps<0.5*C*epsilon+1E-8);
        cooling_eps = -decrement*0.01;
        cooling_eps = MAX(cooling_eps, 0.5*C*epsilon);
        printf("cooling_eps: %.8g\n", cooling_eps); */

        /* print new weights */
        printf("W=");
        for (i = 1; i <= sm.sizePsi; ++i)
            printf("%.3f ", sm.w[i]);
        printf("\n");

        /* Save model */
        char modelfile_tmp[1024];
        sprintf(modelfile_tmp, "%s.%d", modelfile, outer_iter);
        write_struct_model(modelfile_tmp, &sm, &sparm);

        /* impute latent variable using updated weight vector */
        for (i=0; i<m; i++) {
            free_latent_var(ex[i].h);
            ex[i].h = infer_latent_variables(ex[i].x, ex[i].y, &sm, &sparm);
        }
        /* re-compute feature vector cache */
        for (i=0; i<m; i++) {
            free_svector(fycache[i]);
            fy = psi(ex[i].x, ex[i].y, ex[i].h, &sm, &sparm);

            /* DEBUG */
            printf("true_psi[%d]=", i);
            for (int j = 0; j < sm.sizePsi; ++j)
                printf("%.4lf ", fy->words[j].weight);
            printf("\n");

            diff = add_list_ss(fy);
            free_svector(fy);
            fy = diff;
            fycache[i] = fy;
        }

        outer_iter++;
    } // end outer loop


    /* write structural model */
    write_struct_model(modelfile, &sm, &sparm);
    // skip testing for the moment

    /* free memory */
    free_struct_sample(sample);
    free_struct_model(sm, &sparm);
    for(i=0; i<m; i++) {
        free_svector(fycache[i]);
    }
    free(fycache);

    return(0);

}