Package com.etsy.conjecture.data

Examples of com.etsy.conjecture.data.StringKeyedVector


        }
    }

    @Override
    public StringKeyedVector getGradients(LabeledInstance<BinaryLabel> instance) {
        StringKeyedVector gradients = instance.getVector().copy();
        double inner = param.dot(instance.getVector());
        double label = instance.getLabel().getAsPlusMinus();
        double z = inner * label;
        if (z <= this.threshold) {
            gradients.mul(-label);
            return gradients;
        } else {
            return new StringKeyedVector();
        }       
    }
View Full Code Here


  private static final long serialVersionUID = 1L;
  private Map<String, Double> clusterCounts = new HashMap<String, Double>();

  public KMeans(String[] categories) {
    for(String s : categories) {
        param.put(s, new StringKeyedVector());
        clusterCounts.put(s, 0.0);
    }
  }
View Full Code Here

    Double current_count = clusterCounts.get(closest_center);
    clusterCounts.put(closest_center, current_count+1.0);
    // Get per center learning rate
    Double learning_rate = 1.0/clusterCounts.get(closest_center);
    // take gradient step
    StringKeyedVector center = param.get(closest_center);
    center.mul(1-learning_rate);
    instance.mul(learning_rate);
    center.add(instance);
    l1Projection(center);
    param.put(closest_center, center);
  }
View Full Code Here

    private StringKeyedVector unnormalizedGradients = new StringKeyedVector();
    private StringKeyedVector summedGradients = new StringKeyedVector();

    @Override
    public StringKeyedVector getUpdate(LabeledInstance instance) {
        StringKeyedVector gradients = model.getGradients(instance);
        StringKeyedVector updateVec = new StringKeyedVector();
        Iterator<Map.Entry<String, Double>> it = gradients.iterator();
        while (it.hasNext()) {
            Map.Entry<String,Double> pairs = (Map.Entry)it.next();
            String feature = pairs.getKey();
            double gradient = pairs.getValue();
            double featureLearningRate = updateAndGetFeatureLearningRate(feature, gradient);
            updateVec.setCoordinate(feature, gradient * -featureLearningRate);
       }
       return updateVec;
    }
View Full Code Here

        return param;
    }

    @Override
    public void teardown() {
        summedGradients = new StringKeyedVector();
        unnormalizedGradients = new StringKeyedVector();
    }
View Full Code Here

        return new BinaryLabel(Utilities.logistic(inner));
    }

    @Override
    public StringKeyedVector getGradients(LabeledInstance<BinaryLabel> instance) {
        StringKeyedVector gradients = instance.getVector().copy();
        double label = instance.getLabel().getAsPlusMinus();
        double prediction = param.dot(instance.getVector());
        double loss = Math.max(0, 1d - label * prediction);
        if (loss > 0) {
            double norm = instance.getVector().LPNorm(2d);
            double tau = loss / (norm * norm);
            gradients.mul(tau * label);
            return gradients;
        } else {
          return new StringKeyedVector();
        }
    }
View Full Code Here

public class ElasticNetOptimizer extends SGDOptimizer implements LazyVector.UpdateFunction {

    @Override
    public StringKeyedVector getUpdate(LabeledInstance instance) {
        StringKeyedVector gradients = model.getGradients(instance);
        double learningRate = getDecreasingLearningRate(model.epoch);
        gradients.mul(-learningRate);
        return gradients;
    }
View Full Code Here

    private StringKeyedVector n = new StringKeyedVector();

    @Override
    public StringKeyedVector getUpdate(LabeledInstance<L> instance) {
        FTRLRegularization(instance);
        StringKeyedVector gradients = model.getGradients(instance);
        Iterator<Map.Entry<String, Double>> it = gradients.iterator();
        while (it.hasNext()) {
            Map.Entry<String,Double> pairs = (Map.Entry)it.next();
            String feature = pairs.getKey();
            double gradient = pairs.getValue();
            double eta = getFeatureLearningRate(feature, gradient);
            double z_i = 0.0; // if first round, set z_i to 0.0
            if (z.containsKey(feature)) {
                z_i = z.getCoordinate(feature);
            }
            double update = (z_i + gradient) - eta * model.param.getCoordinate(feature);
            z.setCoordinate(feature, update);
            double n_i = 0.0; // if first round, set n_i to 0.0
            if (n.containsKey(feature)) {
                n_i = n.getCoordinate(feature);
            }
            n.setCoordinate(feature, n_i + gradient * gradient);
       }
       return new StringKeyedVector(); // Model updates happen in the FTRLRegularization step
    }
View Full Code Here

        return this;
    }

    @Override
    public void teardown() {
        z = new StringKeyedVector();
        n = new StringKeyedVector();
    }
View Full Code Here

        assertTrue(p.predict(getNegativeInstance().getVector()).getValue() < 0.5);
    }

    public void testInstanceNotModified(UpdateableLinearModel model) {
        BinaryLabeledInstance instance = getPositiveInstance();
        StringKeyedVector instanceCopy = instance.getVector().copy();
        model.update(instance);
        assertEquals(instance.getVector().getCoordinate("foo"), instanceCopy.getCoordinate("foo"), 0.0);
        assertEquals(instance.getVector().getCoordinate("bar"), instanceCopy.getCoordinate("bar"), 0.0);
    }
View Full Code Here

TOP

Related Classes of com.etsy.conjecture.data.StringKeyedVector

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.