<commit_before>package edu.jhu.hlt.optimize.functions;

import edu.jhu.hlt.optimize.function.Regularizer;
import edu.jhu.hlt.optimize.function.ValueGradient;
import edu.jhu.prim.vector.IntDoubleDenseVector;
import edu.jhu.prim.vector.IntDoubleVector;

/**
 * Gaussian prior (L2 regularizer) on the parameters with mean zero and a
 * specified variance.
 * 
 * @author mgormley
 * 
 */
public class L2 implements Regularizer {

    private double lambda;
    private int numParams;
    
    public L2(double lambda) {
        this.lambda = lambda;
    }
    
    /**
     * Builds a Gaussian prior (L2 regularizer) on the parameters.
     * 
     * @param variance The covariance matrix of the Gaussian will be variance*I.
     * @param numParams The number of parameters.
     */
    public L2(double lambda, int numParams) {
        this.lambda = lambda;
        this.numParams = numParams;
    }
    
    /**
     * Gets the negated sum of squares times 1/2 \lambda.
     */
    @Override
    public double getValue(IntDoubleVector params) {
        double sum = params.dot(params);
        sum *= 1./2. * lambda;
        return - sum;
    }

    /**
     * Gets the negative parameter value times lambda.
     */
    // TODO: Why do Sutton & McCallum include the sum of the parameters here and not just the value for each term of the gradient.
    @Override
    public IntDoubleVector getGradient(IntDoubleVector params) {
        IntDoubleDenseVector gradient = new IntDoubleDenseVector(numParams);
        for (int j=0; j<numParams; j++) {
            gradient.set(j, - params.get(j) * lambda);
        }
        return gradient;
    }

    @Override
    public ValueGradient getValueGradient(IntDoubleVector point) {
        return new ValueGradient(getValue(point), getGradient(point));
    }

    @Override
    public int getNumDimensions() {
        return numParams;
    }

    public void setNumDimensions(int numParams) {
        this.numParams = numParams ;
    }

    public double getVariance() {
        return variance;
    }

}<commit_msg>Misc changes: AnnoSentenceCollection - added a way to get all text from the set of AnnoSentences; ConcreteUtils - updated construction of new communication based on deprication of OptimizeFactory.create; L2 - removed reference to removed variance variable<commit_after>package edu.jhu.hlt.optimize.functions;

import edu.jhu.hlt.optimize.function.Regularizer;
import edu.jhu.hlt.optimize.function.ValueGradient;
import edu.jhu.prim.vector.IntDoubleDenseVector;
import edu.jhu.prim.vector.IntDoubleVector;

/**
 * Gaussian prior (L2 regularizer) on the parameters with mean zero and a
 * specified variance.
 * 
 * @author mgormley
 * 
 */
public class L2 implements Regularizer {

    private double lambda;
    private int numParams;
    
    public L2(double lambda) {
        this.lambda = lambda;
    }
    
    /**
     * Builds a Gaussian prior (L2 regularizer) on the parameters.
     * 
     * @param variance The covariance matrix of the Gaussian will be variance*I.
     * @param numParams The number of parameters.
     */
    public L2(double lambda, int numParams) {
        this.lambda = lambda;
        this.numParams = numParams;
    }
    
    /**
     * Gets the negated sum of squares times 1/2 \lambda.
     */
    @Override
    public double getValue(IntDoubleVector params) {
        double sum = params.dot(params);
        sum *= 1./2. * lambda;
        return - sum;
    }

    /**
     * Gets the negative parameter value times lambda.
     */
    // TODO: Why do Sutton & McCallum include the sum of the parameters here and not just the value for each term of the gradient.
    @Override
    public IntDoubleVector getGradient(IntDoubleVector params) {
        IntDoubleDenseVector gradient = new IntDoubleDenseVector(numParams);
        for (int j=0; j<numParams; j++) {
            gradient.set(j, - params.get(j) * lambda);
        }
        return gradient;
    }

    @Override
    public ValueGradient getValueGradient(IntDoubleVector point) {
        return new ValueGradient(getValue(point), getGradient(point));
    }

    @Override
    public int getNumDimensions() {
        return numParams;
    }

    public void setNumDimensions(int numParams) {
        this.numParams = numParams ;
    }
}
{'fraction_non_alphanumeric': 0.08626719515038471, 'fraction_numerical': 0.00489624621123805, 'mean_word_length': 2.6202531645569622, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5446270', 'n_tokens_mistral': 1255, 'n_tokens_neox': 1210, 'n_words': 375}