Package joshua.discriminative.semiring_parsingv2.pmodule

Examples of joshua.discriminative.semiring_parsingv2.pmodule.ExpectationSemiringPM


    SymbolTable p_symbol = new BuildinSymbol(null);
    int ngramStateID =0;
   
    double sumEntropy = 0;
   
    DefaultInsideSemiringParser ds = new EntropyOnHG(1.0);
    DiskHyperGraph dhg_test = new DiskHyperGraph(p_symbol, ngramStateID, true, null); //
    dhg_test.initRead(f_dev_items, f_dev_rules,null);
    for(int sent_id=0; sent_id < num_sents; sent_id ++){
      System.out.println("#Process sentence " + sent_id);
      HyperGraph hg_test = dhg_test.readHyperGraph();   
      ds.setHyperGraph(hg_test);
      ds.insideEstimationOverHG();
      ExpectationSemiring<LogSemiring,ScalarPM> goalSemiring = (ExpectationSemiring<LogSemiring,ScalarPM>) ds.getGoalK();
      //goal_semiring.printInfor();
      goalSemiring.getR().getValue().multiLogNumber(-goalSemiring.getP().getLogValue());//normalize
      goalSemiring.printInfor();
      double entropy = goalSemiring.getP().getLogValue() - goalSemiring.getR().getValue().convertToRealValue();
      System.out.println("entropy is " + entropy);
View Full Code Here



public class ScalarBO implements BilinearOperator<ScalarPM, ScalarPM, ScalarPM>{

  public ScalarPM bilinearMulti(ScalarPM r, ScalarPM s) {
    SignedValue res = SignedValue.multi(r.getValue(), s.getValue() );
    return new ScalarPM(res);
  }
View Full Code Here

 
  SignedValue entropy;
  SignedValue risk;
 
  public RiskAndEntropyPM(){
    this.value = new SignedValue();
    this.entropy = new SignedValue();
    this.risk = new SignedValue();
  }
View Full Code Here

    this.entropy = entropy;
    this.risk = risk;
  }
 
  public RiskAndEntropyPM duplicate() {
    SignedValue v = this.value.duplicate();
    SignedValue e = this.entropy.duplicate();
    SignedValue r = this.risk.duplicate();
    return new RiskAndEntropyPM(v,e,r);
  }
View Full Code Here

    //goalX.printInfor();
 
   
    //=== entropy
    //--normalize
    SignedValue entropyFactor = goalK.getR().getEntropy().duplicate();
    entropyFactor.multiLogNumber(-logZ);
   
    this.entropy = logZ - entropyFactor.convertToRealValue();//logZ - \bar{r}/Z
    //System.out.print("Entropy is" + entropy);
   
    //=== risk
    //--normalize
    SignedValue riskFactor = goalK.getR().getRisk().duplicate();
    riskFactor.multiLogNumber(-logZ);
    this.risk =  riskFactor.convertToRealValue();
    //System.out.print("Risk is" + risk);
   
    this.functionValue = this.risk - this.temperature*this.entropy;
   
   
    //=== gradients
    //System.out.print("Gradients are: ");
    HashMap<Integer, Double>  gradient = new HashMap<Integer, Double>();
    for(Integer featID : goalX.getT().getValue().getIds()){
      //delta(r)*Z/Z^2=delta(r)/Z
      //--normalize
      SignedValue resT = goalX.getT().getValue().getValueAt(featID).duplicate();
      resT.multiLogNumber(-logZ);
       
      //-delta(Z)*r/Z^2
      SignedValue resRS = SignedValue.multi(
          goalX.getS().getValue().getValueAt(featID),
          goalK.getR().getValue()
        );
      resRS.negate();
      resRS.multiLogNumber(-2*logZ);
     
      //-T*delta(Z)/Z
      SignedValue resS = goalX.getS().getValue().getValueAt(featID).duplicate();
      resS.multiLogNumber(Math.log(this.getTemperature()));
      resS.negate();     
      resS.multiLogNumber(-logZ);
     
      //add them together
      resT.add(resRS);
      resT.add(resS);
      double finalVal = resT.convertToRealValue();
View Full Code Here

public class ScalarPM implements PModule<LogSemiring, ScalarPM>{
 
  SignedValue value;
 
  public ScalarPM(){
    this.value = new SignedValue();
    this.value.setToZero();
  }
View Full Code Here

  public ScalarPM(SignedValue v_){
    this.value = v_;
  }
 
  public ScalarPM duplicate() {
    SignedValue v = this.value.duplicate();
    return new ScalarPM(v);
  }
View Full Code Here

  }*/
 

  public void add(SparseMap b) {
    for(Map.Entry<Integer, SignedValue> feature : b.tbl.entrySet()){
      SignedValue valB = feature.getValue();
      SignedValue valA = this.tbl.get(feature.getKey());
      if(valA!=null){
        valA.add(valB);
      }else{
        this.tbl.put(feature.getKey(), SignedValue.duplicate(valB));
      }
    }     
  }
View Full Code Here

  }

  public SparseMap duplicate() {
    HashMap<Integer, SignedValue> res = new HashMap<Integer, SignedValue>();
    for(Map.Entry<Integer, SignedValue> feature : this.tbl.entrySet()){
      SignedValue val = feature.getValue().duplicate();
      res.put(feature.getKey(), val);
    }
    return new SparseMap(res);
  }
View Full Code Here

    HashMap<Integer, SignedValue> gradientsMap = new HashMap<Integer, SignedValue>();    
    for(Map.Entry<Integer, Double> feature : features.entrySet()){
      int featID = feature.getKey();
     
      //P_e * \gamma * \Phi(e)
      SignedValue logGradient = SignedValue.createSignedValueFromRealNumber( scale*feature.getValue() );
      logGradient.multiLogNumber(logTransitionProb);
     
      gradientsMap.put(featID, logGradient);
    }
   
    return new SparseMap(gradientsMap);
View Full Code Here

TOP

Related Classes of joshua.discriminative.semiring_parsingv2.pmodule.ExpectationSemiringPM

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.