Package fr.lip6.jkernelmachines.kernel.extra.bag

Examples of fr.lip6.jkernelmachines.kernel.extra.bag.UnormalizedListKernel



      //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


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

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

    SDCADensity<double[]> sdca = new SDCADensity<double[]>(k);
    sdca.setC(100);
    sdca.train(train);

    DoubleGaussianMixtureModel gmm = new DoubleGaussianMixtureModel(1);
    gmm.train(train);

    SimpleMKLDensity<double[]> mkl = new SimpleMKLDensity<double[]>();
    for (int x = 0; x < dimension; x++) {
      mkl.addKernel(new IndexDoubleGaussL2(x));
    }
    mkl.setC(100);
    mkl.train(train);

    ArrayList<double[]> test = new ArrayList<double[]>();
    // 4. generate positive test samples
    for (int i = 0; i < nbPosTest; i++) {
      double[] t = new double[dimension];
      for (int x = 0; x < dimension; x++) {
        t[x] = ran.nextGaussian();
      }

      test.add(t);
    }

    // 6. test svm
    for (double[] t : test) {
      double value = svm.valueOf(t);
      double pvalue = parzen.valueOf(t);
      double dvalue = sdca.valueOf(t);
      double gvalue = gmm.valueOf(t);
      double mvalue = mkl.valueOf(t);

      System.out.println("smo: " + value + ", parzen: "
          + pvalue + ", sdca: " + dvalue + " , gmm: "
          + gvalue + " , mkl: " + mvalue);
View Full Code Here

  /**
   * Test method for {@link fr.lip6.jkernelmachines.density.DoubleGaussianMixtureModel#train(java.util.List)}.
   */
  @Test
  public final void testTrainListOfdouble() {
    DoubleGaussianMixtureModel gmm = new DoubleGaussianMixtureModel(2);
    gmm.train(train);
   
    for(double[] x : train) {
      assertTrue(gmm.valueOf(x) > 0);
      assertTrue(gmm.valueOf(x) <= 1);
    }
  }
View Full Code Here

    // train gmm
    List<double[]> list = new ArrayList<>(l.size());
    for(TrainingSample<double[]> t : l) {
      list.add(t.sample);
    }
    km = new DoubleKMeans(K);
    km.train(list);
    debug.println(1, "KM trained");
   
    // compute likelihoods
    list.clear();
View Full Code Here

      System.out
          .println("usage: VOCExample trainfile.voc trainfile.fvec testfile.voc testfile.fvec");
      return;
    }

    FvecImporter fvecimp = new FvecImporter();

    // read train features
    List<double[]> feat = null;
    try {
      feat = fvecimp.readFile(args[1]);
    } catch (IOException e1) {
      System.out.println("Unable to read train features: " + args[1]);
      return;
    }

    // read voc train file
    List<TrainingSample<double[]>> train = new ArrayList<TrainingSample<double[]>>();
    try {
      LineNumberReader lin = new LineNumberReader(new FileReader(args[0]));
      String line;
      int i = 0;
      while ((line = lin.readLine()) != null) {
        // get label from second field. ex: "000012 -1"
        int label = Integer.parseInt(line.split("[ ]+")[1]);
        train.add(new TrainingSample<double[]>(feat.get(i), label));
        i++;
      }
      lin.close();
    } catch (FileNotFoundException e) {
      System.out
          .println("trainfile.voc : " + args[0] + " was not found.");
      return;
    } catch (IOException e) {
      System.out
          .println("Error while parsing trainfile.voc : " + args[0]);
      return;
    }
    System.out.println("Train features loaded.");

    // load test features
    try {
      feat = fvecimp.readFile(args[3]);
    } catch (IOException e1) {
      System.out.println("Unable to read test features: " + args[3]);
      return;
    }
View Full Code Here

   * Test method for {@link fr.lip6.jkernelmachines.io.FvecImporter#readFile(java.lang.String)}.
   */
  @Test
  public final void testReadFile() {
    try {
      FvecImporter fvecimp = new FvecImporter();
      List<double[]> l = fvecimp.readFile("resources/dict.fvec");
      assertEquals(32, l.size());
      assertEquals(40, l.get(0).length);
    }
    catch(IOException e) {

View Full Code Here

   * Test method for {@link fr.lip6.jkernelmachines.io.FvecImporter#writeFile(java.lang.String, java.util.List<double[]>)}.
   */
  @Test
  public final void testWriteFile() {
    try {
      FvecImporter fvecimp = new FvecImporter();
     
      List<double[]> l = new ArrayList<double[]>();
      double[] d = new double[10];
      for(int i = 0 ; i < 10 ; i++)
        d[i] = i;
      l.add(d);
     
      fvecimp.writeFile("resources/testwrite.fvec", l);
     
      File f = new File("resources/testwrite.fvec");
     
      assertEquals(4+4*10, f.length());
     
     
      l = fvecimp.readFile("resources/testwrite.fvec");
      assertEquals(10, l.get(0).length);
      for(int i = 0 ; i < 10 ; i++)
        assertEquals(i, l.get(0)[i], 1e-7);
     
      f.delete();
View Full Code Here

    if ("lasvm"
        .equalsIgnoreCase(classifierBox.getSelectedItem().toString())) {
      Kernel<double[]> k = new DoubleLinear();
      if ("GaussianL2".equalsIgnoreCase(kernelBox.getSelectedItem()
          .toString())) {
        k = new DoubleGaussL2(Double.parseDouble(kernelParamTextField
            .getText()));
      } else if ("TriangleL2".equalsIgnoreCase(kernelBox
          .getSelectedItem().toString())) {
        k = new DoubleTriangleL2(
            Double.parseDouble(kernelParamTextField.getText()));
      } else if ("Polynomial".equalsIgnoreCase(kernelBox
          .getSelectedItem().toString())) {
        k = new DoublePolynomial(Integer.parseInt(kernelParamTextField
            .getText()));
      } else if ("HPlolynomial".equalsIgnoreCase(kernelBox
          .getSelectedItem().toString())) {
        k = new DoubleHPolynomial(Integer.parseInt(kernelParamTextField
            .getText()));
      }

      LaSVM<double[]> svm = new LaSVM<double[]>(k);
      svm.setC(Double.parseDouble(regularizationField.getText()));
      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 ("smo".equalsIgnoreCase(classifierBox.getSelectedItem()
        .toString())) {
      Kernel<double[]> k = new DoubleLinear();
      if ("GaussianL2".equalsIgnoreCase(kernelBox.getSelectedItem()
          .toString())) {
        k = new DoubleGaussL2(Double.parseDouble(kernelParamTextField
            .getText()));
      } else if ("TriangleL2".equalsIgnoreCase(kernelBox
          .getSelectedItem().toString())) {
        k = new DoubleTriangleL2(
            Double.parseDouble(kernelParamTextField.getText()));
      } else if ("Polynomial".equalsIgnoreCase(kernelBox
          .getSelectedItem().toString())) {
        k = new DoublePolynomial(Integer.parseInt(kernelParamTextField
            .getText()));
      } else if ("HPlolynomial".equalsIgnoreCase(kernelBox
          .getSelectedItem().toString())) {
        k = new DoubleHPolynomial(Integer.parseInt(kernelParamTextField
            .getText()));
      }

      SMOSVM<double[]> svm = new SMOSVM<double[]>(k);
      svm.setC(Double.parseDouble(regularizationField.getText()));
      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 ("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);
//        // }
View Full Code Here

   
    //generate the list
    List<TrainingSample<double[]>> list = mcg.generateList(100);
   
    //build classifier based on GaussL2 lasvm with c=10
    DoubleGaussL2 k =new DoubleGaussL2(2);
    LaSVM<double[]> svm = new LaSVM<double[]>(k);
    svm.setC(10);
    OneAgainstAll<double[]> mcsvm = new OneAgainstAll<double[]>(svm);
   
   
View Full Code Here

TOP

Related Classes of fr.lip6.jkernelmachines.kernel.extra.bag.UnormalizedListKernel

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.