Пример #1
0
void opf_OPFAgglomerativeLearning(Subgraph **sgtrain, Subgraph **sgeval){
    int n, i = 1;
    float Acc;

    /*while  there exists misclassified samples in sgeval*/
    do{
        fflush(stdout); fprintf(stdout, "\nrunning iteration ... %d ", i++);
        n = 0;
        opf_OPFTraining(*sgtrain);
        opf_OPFClassifying(*sgtrain, *sgeval);
        Acc = opf_Accuracy(*sgeval); fprintf(stdout," %f",Acc*100);
        opf_MoveMisclassifiedNodes(&(*sgeval), &(*sgtrain), &n);
        fprintf(stdout,"\nMisclassified nodes: %d",n);
    }while(n);
}
Пример #2
0
//Learning function: it executes the learning procedure for CompGraph replacing the
//missclassified samples in the evaluation set by non prototypes from
//training set -----
void opf_OPFLearning(Subgraph **sgtrain, Subgraph **sgeval){
	int i = 0, iterations = 10;
	float Acc = FLT_MIN, AccAnt = FLT_MIN,MaxAcc=FLT_MIN, delta;
	Subgraph *sg=NULL;

	do{
		AccAnt = Acc;
		fflush(stdout); fprintf(stdout, "\nrunning iteration ... %d ", i);
		opf_OPFTraining(*sgtrain);
		opf_OPFClassifying(*sgtrain, *sgeval);
		Acc = opf_Accuracy(*sgeval);
		if (Acc > MaxAcc){
		  MaxAcc = Acc;
		  if (sg!=NULL) DestroySubgraph(&sg);
		  sg = CopySubgraph(*sgtrain);
		}
		opf_SwapErrorsbyNonPrototypes(&(*sgtrain), &(*sgeval));
		fflush(stdout); fprintf(stdout,"opf_Accuracy in the evaluation set: %.2f %%\n", Acc*100);
		i++;
		delta = fabs(Acc-AccAnt);
	}while ((delta > 0.0001) && (i <= iterations));
	DestroySubgraph(&(*sgtrain));
	*sgtrain = sg;
}
Пример #3
0
int main(int argc, char **argv)
{

    if (argc != 5)
    {
        fprintf(stderr, "\nusage FeatureSelection <training set> <evaluating set> <testing set> <search space configuration file>\n");
        exit(-1);
    }

    SearchSpace *s = NULL;
    int i;
    double time_opt, time_classify, classification_error;
    timer tic, toc;
    FILE *f = NULL;
    TransferFunc optTransfer = NULL;
    Subgraph *Train = NULL, *Evaluate = NULL, *Merge = NULL, *Test = NULL, *newTrain = NULL, *newTest = NULL;

    Train = ReadSubgraph(argv[1]);
    Evaluate = ReadSubgraph(argv[2]);
    Test = ReadSubgraph(argv[3]);
    s = ReadSearchSpaceFromFile(argv[4], _WCA_);
    optTransfer = S2TransferFunction;

    for (i = 0; i < Train->nfeats; i++)
    {
        s->LB[i] = -20;
        s->LB[i] = 20;
    }

    fprintf(stderr, "\nInitializing search space ... ");
    InitializeSearchSpace(s, _WCA_);
    fprintf(stderr, "\nOk\n");

    fflush(stderr);
    fprintf(stderr, "\nRunning WCA ... ");
    gettimeofday(&tic, NULL);
    runWCA(s, FeatureSelectionOPF, Train, Evaluate, optTransfer);
    gettimeofday(&toc, NULL);
    fflush(stderr);
    fprintf(stderr, "\nOK\n");

    time_opt = ((toc.tv_sec - tic.tv_sec) * 1000.0 + (toc.tv_usec - tic.tv_usec) * 0.001) / 1000.0;
    fprintf(stdout, "\nOptimization time: %f seconds\n", time_opt);
    fflush(stderr);

    Merge = opf_MergeSubgraph(Train, Evaluate);

    fflush(stderr);
    fprintf(stderr, "\nWriting new training and testing sets ...\n");
    newTrain = CreateSubgraphFromSelectedFeatures(Merge, s->g);
    newTest = CreateSubgraphFromSelectedFeatures(Test, s->g);
    fprintf(stderr, "\nTraining set\n");
    WriteSubgraph(newTrain, "training.wca.dat");
    fprintf(stderr, "\n\nTesting set\n");
    WriteSubgraph(newTest, "testing.wca.dat");
    fflush(stderr);
    fprintf(stderr, "\nOK\n");

    opf_OPFTraining(newTrain);
    gettimeofday(&tic, NULL);
    opf_OPFClassifying(newTrain, newTest);
    gettimeofday(&toc, NULL);
    classification_error = opf_Accuracy(newTest);

    time_classify = ((toc.tv_sec - tic.tv_sec) * 1000.0 + (toc.tv_usec - tic.tv_usec) * 0.001) / 1000.0;
    fprintf(stdout, "\nClassification time: %f seconds\n", time_classify);
    fflush(stderr);

    f = fopen("best_feats.txt", "a");
    fprintf(f, "%d %d", newTrain->nfeats, (int)s->g[0]);
    for (i = 1; i < Train->nfeats; i++)
    {
        fprintf(f, " %d", (int)s->g[i]);
    }
    fprintf(f, "\n");
    fclose(f);

    fprintf(stderr, "\nAccuracy: %.2lf%%\n", 100 * classification_error);
    f = fopen("final_accuracy.txt", "a");
    fprintf(f, "%lf\n", classification_error);
    fclose(f);

    fflush(stderr);
    fprintf(stderr, "\nDeallocating memory ...");
    DestroySubgraph(&Train);
    DestroySubgraph(&Evaluate);
    DestroySubgraph(&Merge);
    DestroySubgraph(&Test);
    DestroySubgraph(&newTrain);
    DestroySubgraph(&newTest);
    fflush(stderr);
    fprintf(stderr, "\nOK\n");

    f = fopen("optimization.time", "a");
    fprintf(f, "%f %f\n", time_opt, time_classify);
    fclose(f);

    DestroySearchSpace(&s, _WCA_);

    return 0;
}
Пример #4
0
int main(int argc, char **argv)
{

    if (argc != 4)
    {
        fprintf(stderr, "\nusage CombinatorialOPF <training set> <testing set> <input combinatorial matrix>\n");
        exit(-1);
    }

    int i, j, acc_index, m, n;
    double time_comb, classification_accuracy, overall_accuracy = 0, **r;
    timer tic, toc;
    FILE *f = NULL;
    Subgraph *Train = NULL, *Test = NULL, *newTrain = NULL, *newTest = NULL;

    Train = ReadSubgraph(argv[1]);
    Test = ReadSubgraph(argv[2]);

    fprintf(stderr, "\nInitializing combinatorial OPF ... ");

    f = fopen(argv[3], "r");
    fscanf(f, "%d\n", &n);
    m = pow(2, n);
    r = (double **)calloc(m, sizeof(double *));
    for (i = 0; i < m; i++)
        r[i] = (double *)calloc(n, sizeof(double));

    for (i = 0; i < m; i++)
    {
        for (j = 0; j < n; j++)
        {
            fscanf(f, "%lf ", &r[i][j]);
        }
        fscanf(f, "\n");
    }
    fclose(f);
    fprintf(stderr, "\nOk\n");

    fflush(stderr);
    fprintf(stderr, "\nRunning OPF ... ");

    f = fopen("final_accuracy.txt", "a");
    gettimeofday(&tic, NULL);
    for (i = 0; i < m; i++)
    {
        newTrain = CreateSubgraphFromSelectedFeatures(Train, r[i]);
        newTest = CreateSubgraphFromSelectedFeatures(Test, r[i]);
        opf_OPFTraining(newTrain);
        opf_OPFClassifying(newTrain, newTest);
        classification_accuracy = opf_Accuracy(newTest);
        fprintf(f, "%lf\n", classification_accuracy);
        if (classification_accuracy > overall_accuracy)
        {
            overall_accuracy = classification_accuracy;
            acc_index = i;
        }
    }
    gettimeofday(&toc, NULL);
    fclose(f);
    fflush(stderr);
    fprintf(stderr, "\nOK\n");

    time_comb = ((toc.tv_sec - tic.tv_sec) * 1000.0 + (toc.tv_usec - tic.tv_usec) * 0.001) / 1000.0;
    fprintf(stdout, "\nCombination time: %f seconds\n", time_comb);
    fflush(stderr);

    f = fopen("best_feats.txt", "a");
    fprintf(f, "%d ", n);
    for (i = 0; i < n; i++)
    {
        fprintf(f, "%d ", (int)r[acc_index][i]);
    }
    fprintf(f, "\n");
    fclose(f);

    fflush(stderr);
    fprintf(stderr, "\nDeallocating memory ...");
    DestroySubgraph(&Train);
    DestroySubgraph(&Test);
    DestroySubgraph(&newTrain);
    DestroySubgraph(&newTest);
    fflush(stderr);
    fprintf(stderr, "\nOK\n");

    f = fopen("combination.time", "a");
    fprintf(f, "%f\n", time_comb);
    fclose(f);

    return 0;
}