Java Code Examples for org.apache.mahout.math.Vector#times()

The following examples show how to use org.apache.mahout.math.Vector#times() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: 1000021_CDbwEvaluator_t.java    From coming with MIT License 6 votes vote down vote up
private void setStDev(int cI) {
   List<VectorWritable> repPts = representativePoints.get(cI);
   //if (repPts == null) {
   //  System.out.println();
   //}
   int s0 = 0;
   Vector s1 = null;
   Vector s2 = null;
   for (VectorWritable vw : repPts) {
     s0++;
     Vector v = vw.get();
     s1 = s1 == null ? v.clone() : s1.plus(v);
     s2 = s2 == null ? v.times(v) : s2.plus(v.times(v));
   }
  if (s0 > 1) {
   Vector std = s2.times(s0).minus(s1.times(s1)).assign(new SquareRootFunction()).divide(s0);
   double d = std.zSum() / std.size();
   //System.out.println("stDev[" + cI + "]=" + d);
   stDevs.put(cI, d);
 }
}
 
Example 2
Source File: 1000021_CDbwEvaluator_s.java    From coming with MIT License 6 votes vote down vote up
private void setStDev(int cI) {
  List<VectorWritable> repPts = representativePoints.get(cI);
  //if (repPts == null) {
  //  System.out.println();
  //}
  int s0 = 0;
  Vector s1 = null;
  Vector s2 = null;
  for (VectorWritable vw : repPts) {
    s0++;
    Vector v = vw.get();
    s1 = s1 == null ? v.clone() : s1.plus(v);
    s2 = s2 == null ? v.times(v) : s2.plus(v.times(v));
  }
  Vector std = s2.times(s0).minus(s1.times(s1)).assign(new SquareRootFunction()).divide(s0);
  double d = std.zSum() / std.size();
  //System.out.println("stDev[" + cI + "]=" + d);
  stDevs.put(cI, d);
}
 
Example 3
Source File: CMLCRFElasticNet.java    From pyramid with Apache License 2.0 6 votes vote down vote up
/**
 * a special back track line search for sufficient decrease with elasticnet penalized model
 * reference:
 * An improved glmnet for l1-regularized logistic regression.
 * @param searchDirection
 * @return
 */
private void lineSearch(Vector searchDirection, Vector gradient){
    Vector localSearchDir;
    double initialStepLength = 1;
    double shrinkage = 0.5;
    double c = 1e-4;
    double stepLength = initialStepLength;
    Vector start = cmlcrf.getWeights().getAllWeights();
    double penalty = getPenalty();
    double value = getValue();
    double product = gradient.dot(searchDirection);

    localSearchDir = searchDirection;

    while(true){
        Vector step = localSearchDir.times(stepLength);
        Vector target = start.plus(step);
        cmlcrf.getWeights().setWeightVector(target);
        double targetPenalty = getPenalty();
        double targetValue = getValue();
        if (targetValue <= value + c*stepLength*(product + targetPenalty - penalty)){
            break;
        }
        stepLength *= shrinkage;
    }
}
 
Example 4
Source File: ElasticNetLogisticTrainer.java    From pyramid with Apache License 2.0 5 votes vote down vote up
/**
 * a special back track line search for sufficient decrease with elasticnet penalized model
 * reference:
 * An improved glmnet for l1-regularized logistic regression.
 * @param searchDirection
 * @return
 */
private void lineSearch(Vector searchDirection, Vector gradient){
    Vector localSearchDir;
    double initialStepLength = 1;
    double shrinkage = 0.5;
    double c = 1e-4;
    double stepLength = initialStepLength;
    Vector start = logisticRegression.getWeights().getAllWeights();
    double penalty = penalty();
    double value = loss(penalty);
    if (logger.isDebugEnabled()){
        logger.debug("start line search");
        logger.debug("initial loss = "+loss());
    }
    double product = gradient.dot(searchDirection);

    localSearchDir = searchDirection;

    while(true){
        Vector step = localSearchDir.times(stepLength);
        Vector target = start.plus(step);
        logisticRegression.getWeights().setWeightVector(target);
        double targetPenalty = penalty();
        double targetValue = loss(targetPenalty);
        if (targetValue <= value + c*stepLength*(product + targetPenalty - penalty)){
            if (logger.isDebugEnabled()){
                logger.debug("step size = "+stepLength);
                logger.debug("final loss = "+targetValue);
                logger.debug("line search done");
            }
            break;
        }
        stepLength *= shrinkage;
    }
}
 
Example 5
Source File: Step5.java    From recsys-offline with Apache License 2.0 5 votes vote down vote up
public void map(IntWritable key,VectorAndPrefsWritable vectorAndPref,Context context) throws IOException, InterruptedException{  
    Vector coo=vectorAndPref.getVector();  
    List<Long> userIds=vectorAndPref.getUserIDs();  
    List<Float> prefValues=vectorAndPref.getValues();  
    //System.out.println("alluserids:"+userIds);  
    for(int i=0;i<userIds.size();i++){  
        long userID=userIds.get(i);  
        float prefValue=prefValues.get(i);  
        Vector par=coo.times(prefValue);  
        context.write(new VarLongWritable(userID), new VectorWritable(par));  
        System.out.println(",userid:"+userID+",vector:"+par);  //  if the user id = 3 is the same as my paper then is right  
    }  
//  System.out.println();     
}
 
Example 6
Source File: GradientDescent.java    From pyramid with Apache License 2.0 4 votes vote down vote up
public void iterate(){
    Vector gradient = this.function.getGradient();
    Vector direction = gradient.times(-1);
    lineSearcher.moveAlongDirection(direction);
    terminator.add(function.getValue());
}