Package fr.lip6.jkernelmachines.kernel.extra

Examples of fr.lip6.jkernelmachines.kernel.extra.CustomTrainTestMatrixKernel


  @Before
  public void setUp() throws Exception {
    GaussianGenerator g = new GaussianGenerator(10, 5.0f, 1.0);
    train = g.generateList(100);
   
    svm = new DoubleLLSVM();
    svm.setK(4);
    svm.setNn(2);
  }
View Full Code Here


      // save current classifier
      model.classifier = svm;
    } else if ("pegasos".equalsIgnoreCase(classifierBox.getSelectedItem()
        .toString())) {
      DoublePegasosSVM svm = new DoublePegasosSVM();

      svm.setLambda(1. / (train.size() * Double
          .parseDouble(regularizationField.getText())));
      svm.setK(train.size() / 20);
      svm.setT(10 * train.size());
      svm.train(localTrain);

      // info
      classnameLabel.setText(svm.getClass().getSimpleName());
      svLabel.setText("N/A");

      // save current classifier
      model.classifier = svm;
    } else if ("simplemkl".equalsIgnoreCase(classifierBox.getSelectedItem()
        .toString())) {
      SimpleMKL<double[]> svm = new SimpleMKL<double[]>();
      svm.setC(Double.parseDouble(regularizationField.getText()));

      double[] G = { 0.05, 0.1, 0.2, 0.4, 0.8, 1.6, 3.2, 6.4, 12.8, 25.6 };
//      int dim = train.get(0).sample.length;
      for (double g : G) {
        svm.addKernel(new DoubleGaussL2(g));
//        // for(int i = 0 ; i < dim ; i++) {
//        // IndexDoubleGaussL2 k = new IndexDoubleGaussL2(i);
//        // k.setGamma(g);
//        // svm.addKernel(k);
//        // }
      }
      for (int d = 1; d < 5; d++) {
        svm.addKernel(new DoublePolynomial(d));
        svm.addKernel(new DoubleHPolynomial(d));
      }
      svm.train(localTrain);

      // info
      classnameLabel.setText(svm.getClass().getSimpleName());
      double[] alphas = svm.getAlphas();
      int sv = 0;
      for (int s = 0; s < alphas.length; s++) {
        if (alphas[s] != 0) {
          sv++;
        }
      }
      svLabel.setText("" + sv);
      validate();
      // save current classifier
      model.classifier = svm;
    } else if ("qnpkl".equalsIgnoreCase(classifierBox.getSelectedItem()
        .toString())) {
      DoubleQNPKL svm = new DoubleQNPKL();
      svm.setC(Double.parseDouble(regularizationField.getText()));
      DebugPrinter.setDebugLevel(2);

      svm.train(localTrain);
      // info
      classnameLabel.setText(svm.getClass().getSimpleName());
      double[] alphas = svm.getAlphas();
      int sv = 0;
      for (int s = 0; s < alphas.length; s++) {
        if (alphas[s] != 0) {
          sv++;
        }
View Full Code Here

  {
    debug.println(2, "training on "+train.size()+" train data and "+test.size()+" test data");
   
    //first training
    debug.print(3, "first training ");
    svm = new DoublePegasosSVM();
    svm.setLambda(lambda);
    svm.setK(k);
    svm.setT(T);
    svm.setT0(t0);
    svm.train(train);
    debug.println(3, " done.");
   
    //affect numplus highest output to plus class
    debug.println(3, "affecting 1 to the "+numplus+" highest output");
    SortedSet<TrainingSample<double[]>> sorted = new TreeSet<TrainingSample<double[]>>(new Comparator<TrainingSample<double[]>>(){

      @Override
      public int compare(TrainingSample<double[]> o1, TrainingSample<double[]> o2) {
        int ret = (new Double(svm.valueOf(o2.sample))).compareTo(svm.valueOf(o1.sample));
        if(ret == 0)
          ret = -1;
        return ret;
      }
     
    });
    sorted.addAll(test);
    debug.println(3, "sorted size : "+sorted.size()+" test size : "+test.size());
    int n = 0;
    for(TrainingSample<double[]> t : sorted)
    {
      if(n <= numplus)
        t.label = 1;
      else
        t.label = -1;
      n++;
    }
   
    double C = 1. / (train.size()*lambda) ;
    double Cminus = 1e-5;
    double Cplus = 1e-5 * numplus/(test.size() - numplus);
   
    while(Cminus < C || Cplus < C)
    {
      //solve full problem
      ArrayList<TrainingSample<double[]>> full = new ArrayList<TrainingSample<double[]>>();
      full.addAll(train);
      full.addAll(test);
     
      debug.print(3, "full training ");
      svm = new DoublePegasosSVM();
      svm.setLambda(lambda);
      svm.setK(k);
      svm.setT(T);
      svm.setT0(t0);
      svm.train(full);
      debug.println(3, "done.");
     
      boolean changed = false;
     
      do
      {
        changed = false;
        //0. computing error
        final Map<TrainingSample<double[]>, Double> errorCache = new HashMap<TrainingSample<double[]>, Double>();
        for(TrainingSample<double[]> t : test)
        {
          double err1 = 1. - t.label * svm.valueOf(t.sample);
          errorCache.put(t, err1);
        }
        debug.println(3, "Error cache done.");
       
        // 1 . sort by descending error
        sorted = new TreeSet<TrainingSample<double[]>>(new Comparator<TrainingSample<double[]>>(){

          @Override
          public int compare(TrainingSample<double[]> o1,
              TrainingSample<double[]> o2) {
            int ret = errorCache.get(o2).compareTo(errorCache.get(o1));
            if(ret == 0)
              ret = -1;
            return ret;
          }
        });
        sorted.addAll(test);
        List<TrainingSample<double[]>> sortedList = new ArrayList<TrainingSample<double[]>>();
        sortedList.addAll(sorted);
       
       
        debug.println(3, "sorting done, checking couple");
       
        // 2 . test all couple by decreasing error order
//        for(TrainingSample<T> i1 : sorted)
        for(int i = 0 ; i < sortedList.size(); i++)
        {
          TrainingSample<double[]> i1 = sortedList.get(i);
//          for(TrainingSample<T> i2 : sorted)
          for(int j = i+1; j < sortedList.size(); j++)
          {
            TrainingSample<double[]> i2 = sortedList.get(j);
            if(examine(i1, i2, errorCache))
            {
              debug.println(3, "couple found !");
              changed = true;
              break;
            }
          }
          if(changed)
            break;
        }

        if(changed)
        {
          debug.println(3, "re-training");
          svm = new DoublePegasosSVM();
          svm.setLambda(lambda);
          svm.setK(k);
          svm.setT(T);
          svm.setT0(t0);
          svm.train(full);
View Full Code Here

//      double lambdat = 1e-1;
      boolean bias = false;
      int K = 10;
     
      //3.1 train pegasos
      DoublePegasosSVM peg = new DoublePegasosSVM();
      peg.setLambda(lambda);
      peg.setK(K);
      peg.setT(T);
      peg.setT0(t0);
      peg.setBias(bias);
      peg.train(train);


      //3.2 train transductive pegasos
//      DoubleGaussL2 kernel = new DoubleGaussL2();
//      kernel.setGamma(0.01);
      S3VMLightPegasos tpeg = new S3VMLightPegasos();
      tpeg.setLambda(lambda);
      tpeg.setK(K);
      tpeg.setT(T);
      tpeg.setT0(t0);
      tpeg.setBias(bias);
//      tpeg.setVerbosityLevel(3);
      tpeg.setNumplus(200);
      tpeg.train(train, test);
     

      //6. test svm
      int nbErr = 0;
      int tpegErr = 0;
      for(TrainingSample<double[]> t : test)
      {
        int y = t.label;
        double value = peg.valueOf(t.sample);
        if(y*value < 0)
          nbErr++;
        double pegVal = tpeg.valueOf(t.sample);
        if(y*pegVal < 0)
        {
          tpegErr++;
//          System.out.println("y : "+y+" value : "+value+" nbErr : "+nbErr+" pegVal : "+pegVal+" pegErr : "+tpegErr);
        }

//        System.out.println("y : "+y+" value : "+value+" nbErr : "+nbErr+" pegVal : "+pegVal+" pegErr : "+tpegErr);


      }

      pegerr += nbErr;
      pegstd += nbErr*nbErr;
      tpegerr += tpegErr;
      tpegstd += tpegErr*tpegErr;

      //7.1 compute w for pegasos
      double w[] = peg.getW();
      System.out.println("peg : w : "+Arrays.toString(w));
      System.out.println("peg : bias : "+peg.getB());
      System.out.println("peg : ||w|| : "+k.valueOf(w, w));

      //7.2 w from transductive pegasos
      System.out.println("tpeg : w : "+Arrays.toString(tpeg.getW()));
      System.out.println("tpeg : bias : "+tpeg.getB());
View Full Code Here

      validate();
      // save current classifier
      model.classifier = svm;
    } else if ("qnpkl".equalsIgnoreCase(classifierBox.getSelectedItem()
        .toString())) {
      DoubleQNPKL svm = new DoubleQNPKL();
      svm.setC(Double.parseDouble(regularizationField.getText()));
      DebugPrinter.setDebugLevel(2);

      svm.train(localTrain);
      // info
      classnameLabel.setText(svm.getClass().getSimpleName());
      double[] alphas = svm.getAlphas();
      int sv = 0;
      for (int s = 0; s < alphas.length; s++) {
        if (alphas[s] != 0) {
          sv++;
        }
View Full Code Here

      return;
    }
    System.out.println("Test features loaded.");

    // classifier
    DoubleSAG svm = new DoubleSAG();
    svm.setE(10);

    // AP evaluation
    ApEvaluator<double[]> ape = new ApEvaluator<double[]>(svm, train, test);
    System.out.println("training...");
    ape.evaluate();
View Full Code Here

      validate();
      // save current classifier
      model.classifier = svm;
    } else if ("sag".equalsIgnoreCase(classifierBox.getSelectedItem()
        .toString())) {
      DoubleSAG svm = new DoubleSAG();
      svm.setLambda(1. / (train.size() * Double
          .parseDouble(regularizationField.getText())));
      svm.setE(10);
      svm.train(localTrain);

      // info
      classnameLabel.setText(svm.getClass().getSimpleName());
      svLabel.setText("N/A");

      // save current classifier
      model.classifier = svm;
    } else if ("pegasos".equalsIgnoreCase(classifierBox.getSelectedItem()
        .toString())) {
      DoublePegasosSVM svm = new DoublePegasosSVM();

      svm.setLambda(1. / (train.size() * Double
          .parseDouble(regularizationField.getText())));
      svm.setK(train.size() / 20);
      svm.setT(10 * train.size());
      svm.train(localTrain);

      // info
      classnameLabel.setText(svm.getClass().getSimpleName());
      svLabel.setText("N/A");

      // save current classifier
      model.classifier = svm;
    } else if ("simplemkl".equalsIgnoreCase(classifierBox.getSelectedItem()
        .toString())) {
      SimpleMKL<double[]> svm = new SimpleMKL<double[]>();
      svm.setC(Double.parseDouble(regularizationField.getText()));

      double[] G = { 0.05, 0.1, 0.2, 0.4, 0.8, 1.6, 3.2, 6.4, 12.8, 25.6 };
//      int dim = train.get(0).sample.length;
      for (double g : G) {
        svm.addKernel(new DoubleGaussL2(g));
//        // for(int i = 0 ; i < dim ; i++) {
//        // IndexDoubleGaussL2 k = new IndexDoubleGaussL2(i);
//        // k.setGamma(g);
//        // svm.addKernel(k);
//        // }
      }
      for (int d = 1; d < 5; d++) {
        svm.addKernel(new DoublePolynomial(d));
        svm.addKernel(new DoubleHPolynomial(d));
      }
      svm.train(localTrain);

      // info
      classnameLabel.setText(svm.getClass().getSimpleName());
      double[] alphas = svm.getAlphas();
      int sv = 0;
      for (int s = 0; s < alphas.length; s++) {
        if (alphas[s] != 0) {
          sv++;
        }
      }
      svLabel.setText("" + sv);
      validate();
      // save current classifier
      model.classifier = svm;
    } else if ("qnpkl".equalsIgnoreCase(classifierBox.getSelectedItem()
        .toString())) {
      DoubleQNPKL svm = new DoubleQNPKL();
      svm.setC(Double.parseDouble(regularizationField.getText()));
      DebugPrinter.setDebugLevel(2);

      svm.train(localTrain);
      // info
      classnameLabel.setText(svm.getClass().getSimpleName());
      double[] alphas = svm.getAlphas();
      int sv = 0;
      for (int s = 0; s < alphas.length; s++) {
        if (alphas[s] != 0) {
          sv++;
        }
View Full Code Here

  {
    debug.println(2, "training on "+train.size()+" train data and "+test.size()+" test data");
   
    //first training
    debug.print(3, "first training ");
    svm = new DoubleSGDQN();
    DoubleSGDQN.VERBOSE = false;
    svm.train(train);
    debug.println(3, " done.");
   
    //affect numplus highest output to plus class
    debug.println(3, "affecting 1 to the "+numplus+" highest output");
    SortedSet<TrainingSample<double[]>> sorted = new TreeSet<TrainingSample<double[]>>(new Comparator<TrainingSample<double[]>>(){

      @Override
      public int compare(TrainingSample<double[]> o1, TrainingSample<double[]> o2) {
        int ret = (new Double(svm.valueOf(o2.sample))).compareTo(svm.valueOf(o1.sample));
        if(ret == 0)
          ret = -1;
        return ret;
      }
     
    });
    sorted.addAll(test);
    debug.println(4, "sorted size : "+sorted.size()+" test size : "+test.size());
    int n = 0;
    for(TrainingSample<double[]> t : sorted)
    {
      if(n <= numplus)
        t.label = 1;
      else
        t.label = -1;
      n++;
    }
   
    double Cminus = 1e-5;
    double Cplus = 1e-5 * numplus/(test.size() - numplus);
   
    while(Cminus < C || Cplus < C)
    {
      //solve full problem
      ArrayList<TrainingSample<double[]>> full = new ArrayList<TrainingSample<double[]>>();
      full.addAll(train);
      full.addAll(test);
     
      debug.print(3, "full training ");
      svm = new DoubleSGDQN();
      svm.setC((Cminus+Cplus)/2.);
      svm.train(full);
      debug.println(3, "done.");
     
      boolean changed = false;
     
      do
      {
        changed = false;
        //0. computing error
        final Map<TrainingSample<double[]>, Double> errorCache = new HashMap<TrainingSample<double[]>, Double>();
        for(TrainingSample<double[]> t : test)
        {
          double err1 = 1. - t.label * svm.valueOf(t.sample);
          errorCache.put(t, err1);
        }
        debug.println(3, "Error cache done.");
       
        // 1 . sort by descending error
        sorted = new TreeSet<TrainingSample<double[]>>(new Comparator<TrainingSample<double[]>>(){

          @Override
          public int compare(TrainingSample<double[]> o1,
              TrainingSample<double[]> o2) {
            int ret = errorCache.get(o2).compareTo(errorCache.get(o1));
            if(ret == 0)
              ret = -1;
            return ret;
          }
        });
        sorted.addAll(test);
        List<TrainingSample<double[]>> sortedList = new ArrayList<TrainingSample<double[]>>();
        sortedList.addAll(sorted);
       
       
        debug.println(3, "sorting done, checking couple");
       
        // 2 . test all couple by decreasing error order
//        for(TrainingSample<T> i1 : sorted)
        for(int i = 0 ; i < sortedList.size(); i++)
        {
          TrainingSample<double[]> i1 = sortedList.get(i);
//          for(TrainingSample<T> i2 : sorted)
          for(int j = i+1; j < sortedList.size(); j++)
          {
            TrainingSample<double[]> i2 = sortedList.get(j);
            if(examine(i1, i2, errorCache))
            {
              debug.println(3, "couple found !");
              changed = true;
              break;
            }
          }
          if(changed)
            break;
        }

        if(changed)
        {
          debug.println(3, "re-training");
          svm = new DoubleSGDQN();
          svm.setC((Cminus+Cplus)/2.);
          svm.train(full);
        }
      }
      while(changed);
View Full Code Here

  @Before
  public void setUp() throws Exception {
    GaussianGenerator g = new GaussianGenerator(2, 50.0f, 1.0);
    train = g.generateList(10);

    svm = new S3VMLightPegasos();
  }
View Full Code Here


      //3.2 train transductive pegasos
//      DoubleGaussL2 kernel = new DoubleGaussL2();
//      kernel.setGamma(0.01);
      S3VMLightPegasos tpeg = new S3VMLightPegasos();
      tpeg.setLambda(lambda);
      tpeg.setK(K);
      tpeg.setT(T);
      tpeg.setT0(t0);
      tpeg.setBias(bias);
//      tpeg.setVerbosityLevel(3);
      tpeg.setNumplus(200);
      tpeg.train(train, test);
     

      //6. test svm
      int nbErr = 0;
      int tpegErr = 0;
      for(TrainingSample<double[]> t : test)
      {
        int y = t.label;
        double value = peg.valueOf(t.sample);
        if(y*value < 0)
          nbErr++;
        double pegVal = tpeg.valueOf(t.sample);
        if(y*pegVal < 0)
        {
          tpegErr++;
//          System.out.println("y : "+y+" value : "+value+" nbErr : "+nbErr+" pegVal : "+pegVal+" pegErr : "+tpegErr);
        }

//        System.out.println("y : "+y+" value : "+value+" nbErr : "+nbErr+" pegVal : "+pegVal+" pegErr : "+tpegErr);


      }

      pegerr += nbErr;
      pegstd += nbErr*nbErr;
      tpegerr += tpegErr;
      tpegstd += tpegErr*tpegErr;

      //7.1 compute w for pegasos
      double w[] = peg.getW();
      System.out.println("peg : w : "+Arrays.toString(w));
      System.out.println("peg : bias : "+peg.getB());
      System.out.println("peg : ||w|| : "+k.valueOf(w, w));

      //7.2 w from transductive pegasos
      System.out.println("tpeg : w : "+Arrays.toString(tpeg.getW()));
      System.out.println("tpeg : bias : "+tpeg.getB());
      System.out.println("tpeg : ||w|| : "+k.valueOf(tpeg.getW(), tpeg.getW()));

      //8. comparing smo and peg
      System.out.println("< peg, tpeg > : "+(k.valueOf(w, tpeg.getW())/Math.sqrt(k.valueOf(w, w)*k.valueOf(tpeg.getW(), tpeg.getW()))));
    }
   
    //final stats
    System.out.println();
    pegstd = Math.sqrt(pegstd/maxcv - (pegerr/maxcv)*(pegerr/maxcv));
 
View Full Code Here

TOP

Related Classes of fr.lip6.jkernelmachines.kernel.extra.CustomTrainTestMatrixKernel

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.