Package org.fnlp.ml.classifier.linear

Examples of org.fnlp.ml.classifier.linear.Linear


    public static void main(String[] args) throws Exception {

      ARClassifier tc = new ARClassifier();
      tc.train();
      Linear cl =Linear.loadFrom(modelFile);
      int i = 0;int j = 0;double ij = 0.0;int kk = 0;int jj = 0;int nn = 0;int n = 0;
      InstanceSet test = new InstanceSet(cl.getPipe(),cl.getAlphabetFactory());
      SimpleFileReader sfr = new SimpleFileReader("../tmp/ar-train.txt",true);
     
      ArrayList<Instance> list1 = new ArrayList<Instance>();
      while (sfr.hasNext())
      {
        list1.add(sfr.next());
      }
      List<String>[] str1 = new List[list1.size()];
      String[] str2 = new String[list1.size()];
      Iterator it = list1.iterator();
      while(it.hasNext()){
        Instance in = (Instance) it.next();
        str1[i] = (List<String>) in.getData();
        str2[i] = (String) in.getTarget();
        i++;
      }
      for(int k = 0;k<str2.length;k++)
      {
        if(str2[k].equals("1"))
          kk++;
      }
      String ss =null;
        test.loadThruPipes(new ListReader(str1));
       
        for(int ii=0;ii<str1.length;ii++){
          ss = cl.getStringLabel(test.getInstance(ii));
          if(ss.equals("1"))
            j++;
       
          if(ss.equals("1")&&ss.equals(str2[ii]))
            jj++;
View Full Code Here


      Generator gen = new SFGenerator();
      ZeroOneLoss l = new ZeroOneLoss();
      Inferencer ms = new LinearMax(gen, factory.getLabelSize());
      Update update = new LinearMaxPAUpdate(l);
      OnlineTrainer trainer = new OnlineTrainer(ms, update,l, factory.getFeatureSize(), 50,0.005f);
      Linear pclassifier = trainer.train(instset,instset);
      pipe.removeTargetPipe();
      pclassifier.setPipe(pipe);
      factory.setStopIncrement(true);
      pclassifier.saveTo(modelFile);
    }
View Full Code Here

  }

  public void optimizeTag(String file) throws IOException,
  ClassNotFoundException {
    ModelIO.loadFrom(file);
    Linear cl=ModelIO.cl;
    TempletGroup templates=ModelIO.templets;   
    ModelIO.saveTo(file+".bak", templates, cl);
    removeZero(cl);
    ModelIO.saveTo(file,templates,cl);
    System.out.print("Done");
View Full Code Here

    ModelIO.saveTo(file,templates,cl);
    System.out.print("Done");
  }

  public void optimizeDep(String file) throws LoadModelException, IOException {
    Linear cl= Linear.loadFrom(file)
    cl.saveTo(file+".bak");
    removeZero(cl);
    cl.saveTo(file);
    System.out.print("Done");
  }
View Full Code Here


    OnlineTrainer trainer = new OnlineTrainer(inference, update, loss,
        features.size(), 50,0.1f);

    Linear cl = trainer.train(trainSet);


    // test data没有标注
    Pipe tpipe = featurePipe;
    // 测试集
    InstanceSet testSet = new InstanceSet(tpipe);

    testSet.loadThruPipes(new SequenceReader(testfile, false, "utf8"));
    System.out.println("测试样本个数: " + testSet.size()); //
    String[][] labelsSet = new String[testSet.size()][];
    for (int i = 0; i < testSet.size(); i++) {
      Instance carrier = testSet.get(i);
      int[] pred = (int[]) cl.classify(carrier).getLabel(0);
      labelsSet[i] = labels.lookupString(pred);
    }
   
    String s = SimpleFormatter.format(testSet, labelsSet);
    System.out.println(s);
View Full Code Here

   
    Inferencer msolver = new LinearMax(featureGen, al.size() );
    OnlineTrainer trainer = new OnlineTrainer(msolver, update, loss, af.size(), round,
        c);

    Linear classify = trainer.train(train, test);
    String modelFile = path+".m.gz";
    classify.saveTo(modelFile);

    long end = System.currentTimeMillis();
    System.out.println("Total Time: " + (end - start));
    System.out.println("End!");
    (new File(modelFile)).deleteOnExit();
View Full Code Here

   
    /**
     * 建立分类器
     */   
    OnlineTrainer trainer = new OnlineTrainer(af);
    Linear pclassifier = trainer.train(trainset);
    pp.removeTargetPipe();
    pclassifier.setPipe(pp);
    af.setStopIncrement(true);
   
    //将分类器保存到模型文件
    pclassifier.saveTo(modelFile)
    pclassifier = null;
   
    //从模型文件读入分类器
    Linear cl =Linear.loadFrom(modelFile);
   
    //性能评测
    Evaluation eval = new Evaluation(testset);
    eval.eval(cl,1);

    /**
     * 测试
     */
    System.out.println("类别 : 文本内容");
    System.out.println("===================");
    for(int i=0;i<testset.size();i++){
      Instance data = testset.getInstance(i);
     
      Integer gold = (Integer) data.getTarget();
      String pred_label = cl.getStringLabel(data);
      String gold_label = cl.getLabel(gold);
     
      if(pred_label.equals(gold_label))
        System.out.println(pred_label+" : "+testset.getInstance(i).getSource());
      else
        System.err.println(gold_label+"->"+pred_label+" : "+testset.getInstance(i).getSource());
    }
   
   
    /**
     * 分类器使用
     */
    String str = "韦德:不拿冠军就是失败 詹皇:没拿也不意味失败";
    System.out.println("============\n分类:"+ str);
    Pipe p = cl.getPipe();
    Instance inst = new Instance(str);
    try {
      //特征转换
      p.addThruPipe(inst);
    } catch (Exception e) {
      e.printStackTrace();
    }
    String res = cl.getStringLabel(inst);
    System.out.println("类别:"+ res);
    //清除模型文件
    (new File(modelFile)).deleteOnExit();
    System.exit(0);
  }
View Full Code Here

   
    /**
     * 建立分类器
     */   
    OnlineTrainer trainer3 = new OnlineTrainer(af3);
    Linear pclassifier = trainer3.train(trainset);
    pp.removeTargetPipe();
    pclassifier.setPipe(pp);
    af.setStopIncrement(true);
   
    //将分类器保存到模型文件
    pclassifier.saveTo(linearModelFile)
    pclassifier = null;
   
    //从模型文件读入分类器
    Linear cl =Linear.loadFrom(linearModelFile);
   
    //性能评测
    Evaluation eval = new Evaluation(testset);
    eval.eval(cl,1);
    /**
     * 测试
     */
    System.out.println("类别 : 文本内容");
    System.out.println("===================");
    count=0;
    for(int i=0;i<testset.size();i++){
      Instance data = testset.getInstance(i);
     
      Integer gold = (Integer) data.getTarget();
      String pred_label = cl.getStringLabel(data);
      String gold_label = cl.getLabel(gold);
     
      if(pred_label.equals(gold_label)){
        //System.out.println(pred_label+" : "+testsetliner.getInstance(i).getSource());
        count++;
      }
View Full Code Here

   
    /**
     * 建立分类器
     */   
    OnlineTrainer trainer = new OnlineTrainer(af);
    Linear pclassifier = trainer.train(trainset);
    pp.removeTargetPipe();
    pclassifier.setPipe(pp);
    af.setStopIncrement(true);
   
    //将分类器保存到模型文件
    pclassifier.saveTo(modelFile)
    pclassifier = null;
   
    //从模型文件读入分类器
    Linear cl =Linear.loadFrom(modelFile);
   
    //性能评测
    Evaluation eval = new Evaluation(testset);
    eval.eval(cl,1);

View Full Code Here

    LinearMax solver = new LinearMax(generator, ysize);
    ZeroOneLoss loss = new ZeroOneLoss();
    Update update = new LinearMaxPAUpdate(loss);
    OnlineTrainer trainer = new OnlineTrainer(solver, update, loss,
        fsize, maxite, c);
    Linear models = trainer.train(instset, null);
    instset = null;
    solver = null;
    loss = null;
    trainer = null;
    System.out.println();
    factory.setStopIncrement(true);
    models.saveTo(modelfile);

  }
View Full Code Here

TOP

Related Classes of org.fnlp.ml.classifier.linear.Linear

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.