/** Increase the size of the weights[] parameters to match (a new, larger) input Alphabet size */ public void growWeightsDimensionToInputAlphabet () { int vs = inputAlphabet.size(); if (vs == this.defaultFeatureIndex) // Doesn't need to grow return; assert (vs > this.defaultFeatureIndex); setTrainable (false); for (int i = 0; i < weights.length; i++) { DenseVector newWeights = new DenseVector (vs+1); newWeights.arrayCopyFrom (0, weights[i]); newWeights.setValue (vs, weights[i].value (defaultFeatureIndex)); newWeights.setValue (defaultFeatureIndex, 0); weights[i] = newWeights; } this.defaultFeatureIndex = vs; cachedCostStale = true; cachedGradientStale = true; }
public void setParameters (Matrix m) { assert (m instanceof DenseVector && ((DenseVector)m).singleSize() == numParameters); cachedCostStale = cachedGradientStale = true; DenseVector parameters = (DenseVector)m; int pi = 0; for (int i = 0; i < numStates(); i++) { State s = (State) getState (i); s.initialCost = -parameters.value (pi++); s.finalCost = -parameters.value (pi++); } for (int i = 0; i < weights.length; i++) pi = parameters.arrayCopyTo (pi, weights[i]); }
private DenseVector[] constExpCopy (DenseVector[] stuff) { DenseVector[] values = new DenseVector [stuff.length]; for (int i = 0; i < values.length; i++) { values [i] = (DenseVector) stuff[i].cloneMatrix (); } return values; }
public void testTimesEquals () { double[] d1 = new double[] {1, 2, 3, 4, 5}; DenseVector m1 = new DenseVector (d1); DenseVector m2 = new DenseVector (d1); m2.elementwiseTimesEquals (m1); m2.print(); }
public void incrementCount (double count) { //System.out.println ("incrementCount "+(gatheringConstraints?"constraints":"expectations")+" dest#="+source.index+" count="+count); assert (crf.trainable); int weightsIndex = source.weightsIndices[index]; if (crf.gatheringConstraints) { crf.constraints[weightsIndex].plusEquals (input, count); crf.constraints[weightsIndex].columnPlusEquals (crf.defaultFeatureIndex, count); } else { crf.expectations[weightsIndex].plusEquals (input, count); crf.expectations[weightsIndex].columnPlusEquals (crf.defaultFeatureIndex, count); } } // Serialization
for (int i = 0; i < numStates(); i++) { State s = (State) getState (i); cachedGradient.setValue (gi++, (Double.isInfinite(s.initialCost) ? 0.0 : (s.initialExpectation cachedGradient.setValue (gi++, (Double.isInfinite (s.finalCost) ? 0.0 : s.finalExpectation for (int j = 0; j < weights[i].singleSize(); j++) { cachedGradient.setValue (gi++, (Double.isInfinite (weights[i].singleValue(j)) ? 0.0 : (expectations[i].singleValue(j) + (hyperbolicPriorSlope * Maths.tanh (weights[i].singleValue(j)) - constraints[i].singleValue(j)))); if (printGradient) System.out.println ("CRF gradient["+crf.getWeightsName(i)+"]["+(j!=defaultFeatureIndex?inputAlphabet.lookupObject(j):"<DEFAULT_FEATURE>")+"]="+cachedGradient.value(gi-1)); for (int j = 0; j < weights[i].singleSize(); j++) { cachedGradient.setValue (gi++, (Double.isInfinite (weights[i].singleValue(j)) ? 0.0 : (expectations[i].singleValue(j) + weights[i].singleValue(j) / gaussianPriorVariance - constraints[i].singleValue(j)))); if (printGradient) System.out.println ("CRF gradient["+crf.getWeightsName(i)+"]["+(j!=defaultFeatureIndex?inputAlphabet.lookupObject(j):"<DEFAULT_FEATURE>")+"]="+cachedGradient.value(gi-1));
cachedGradient.setValue (gi++, (Double.isInfinite(s.initialCost) ? 0.0 : (s.initialExpectation +s.initialConstraint + " (ctr) + " +initialPrior+ "(reg) = " +(cachedGradient.value(gi-1))); cachedGradient.setValue (gi++, (Double.isInfinite (s.finalCost) ? 0.0 : s.finalExpectation +s.finalConstraint + " (ctr) + " +finalPrior+ "(reg) = " +(cachedGradient.value(gi-1))); cachedGradient.setValue (gi++, (Double.isInfinite (defaultWeights[i]) ? 0.0 : (defaultExpectations[i] System.out.println ("CRF gradient["+crf.getWeightsName(i)+"][<DEFAULT_FEATURE>] (gidx:"+(gi-1)+"="+cachedGradient.value(gi-1)); for (int j = 0; j < weights[i].numLocations(); j++) { cachedGradient.setValue (gi++, (Double.isInfinite (weights[i].valueAtLocation(j)) ? 0.0 : (expectations[i].valueAtLocation(j) System.out.println ("CRF gradient["+crf.getWeightsName(i)+"]["+inputAlphabet.lookupObject(j)+"]="+cachedGradient.value(gi-1)); cachedGradient.setValue (gi++, (Double.isInfinite (defaultWeights[i])
for (int i = 0; i < numStates(); i++) { State s = (State) getState (i); cachedGradient.setValue (gi++, (Double.isInfinite(s.initialCost) ? 0.0 : (s.initialExpectation cachedGradient.setValue (gi++, (Double.isInfinite (s.finalCost) ? 0.0 : s.finalExpectation cachedGradient.setValue (gi++, (Double.isInfinite (defaultWeights[i]) ? 0.0 : (defaultExpectations[i] System.out.println ("CRF gradient["+crf.getWeightsName(i)+"][<DEFAULT_FEATURE>]="+cachedGradient.value(gi-1)); for (int j = 0; j < weights[i].numLocations(); j++) { cachedGradient.setValue (gi++, (Double.isInfinite (weights[i].valueAtLocation(j)) ? 0.0 : (expectations[i].valueAtLocation(j) System.out.println ("CRF gradient["+crf.getWeightsName(i)+"]["+inputAlphabet.lookupObject(j)+"]="+cachedGradient.value(gi-1)); cachedGradient.setValue (gi++, (Double.isInfinite (defaultWeights[i]) ? 0.0 : (defaultExpectations[i] - defaultConstraints[i]))); if (printGradient) System.out.println ("CRF gradient["+crf.getWeightsName(i)+"][<DEFAULT_FEATURE>]="+cachedGradient.value(gi-1));
public void getParameters (double[] buffer) { if (buffer.length != getNumParameters ()) buffer = new double [getNumParameters()]; DenseVector parameters = new DenseVector (buffer, true); int pi = 0; for (int i = 0; i < numStates(); i++) { State s = (State) getState (i); parameters.setValue (pi++, -s.initialCost); parameters.setValue (pi++, -s.finalCost); } for (int i = 0; i < weights.length; i++) { parameters.setValue (pi++, defaultWeights[i]); int nl = weights[i].numLocations(); for (int j = 0; j < nl; j++) parameters.setValue (pi++, weights[i].valueAtLocation(j)); } parameters.arrayCopyTo (0, buffer); }
protected DenseVector getDenseVectorOf (int ri, Matrix2 matrix) { int dims[] = new int [2]; matrix.getDimensions(dims); DenseVector vec = new DenseVector (dims[1]); for (int i=0; i < dims[1]; i++) { vec.setValue (i, matrix.value(ri,i)); } return vec; }
public Quadratic (double a, double b, double c) { parameters = new DenseVector (1); this.a = a; this.b = b; this.c = c; }
public void setTrainable (boolean f) { if (f != trainable) { if (f) { constraints = new DenseVector[weights.length]; expectations = new DenseVector[weights.length]; for (int i = 0; i < weights.length; i++) { constraints[i] = new DenseVector (weights[i].singleSize()); expectations[i] = new DenseVector (weights[i].singleSize()); } } else constraints = expectations = null; for (int i = 0; i < numStates(); i++) ((State)getState(i)).setTrainable(f); trainable = f; } }
public Matrix getParameters (Matrix m) { assert (m instanceof DenseVector && ((Vector)m).singleSize() == numParameters); DenseVector parameters = (DenseVector)m; int pi = 0; for (int i = 0; i < numStates(); i++) { State s = (State) getState (i); parameters.setValue (pi++, -s.initialCost); parameters.setValue (pi++, -s.finalCost); } for (int i = 0; i < weights.length; i++) pi = parameters.arrayCopyFrom (pi, weights[i]); return parameters; }
public void setParameters (double [] buff) { assert (buff.length == getNumParameters()); cachedValueStale = cachedGradientStale = true; DenseVector parameters = new DenseVector (buff, true); int pi = 0; for (int i = 0; i < numStates(); i++) { State s = (State) getState (i); s.initialCost = -parameters.value (pi++); s.finalCost = -parameters.value (pi++); } for (int i = 0; i < weights.length; i++) { defaultWeights[i] = parameters.value (pi++); int nl = weights[i].numLocations(); for (int j = 0; j < nl; j++) weights[i].setValueAtLocation (j, parameters.value (pi++)); } someTrainingDone = true; }
/** Create a new CRF sharing Alphabet and other attributes, but possibly having a larger weights array. */ private CRF (CRF initialCRF) { logger.info ("new CRF. Old weight size = "+initialCRF.weights[0].singleSize()+ " New weight size = "+initialCRF.inputAlphabet.size()); this.inputAlphabet = initialCRF.inputAlphabet; this.outputAlphabet = initialCRF.outputAlphabet; this.states = new ArrayList (initialCRF.states.size()); this.inputPipe = initialCRF.inputPipe; this.outputPipe = initialCRF.outputPipe; this.defaultFeatureIndex = initialCRF.defaultFeatureIndex; for (int i = 0; i < initialCRF.states.size(); i++) { State s = (State) initialCRF.getState (i); String[] weightNames = new String[s.weightsIndices.length]; for (int j = 0; j < weightNames.length; j++) weightNames[j] = (String) initialCRF.weightAlphabet.lookupObject(s.weightsIndices[i]); addState (s.name, s.initialCost, s.finalCost, s.destinationNames, s.labels, weightNames); } assert (weights.length > 0); for (int i = 0; i < weights.length; i++) weights[i].arrayCopyFrom (0, initialCRF.getWeights ((String)weightAlphabet.lookupObject(i))); }
private static InstanceList[] splitInOrder (List instances, double[] proportions, InstanceList cloneMe) { double[] maxind = new double[proportions.length]; System.arraycopy (proportions, 0, maxind, 0, proportions.length); InstanceList[] ret = new InstanceList[proportions.length]; DenseVector.normalize(maxind); // Fill maxind[] with the highest instance index that should go in // each corresponding returned InstanceList. for (int i = 0; i < maxind.length; i++) { // xxx Is it dangerous to share the featureSelection that comes with cloning? ret[i] = cloneMe.cloneEmpty(); if (i > 0) maxind[i] += maxind[i-1]; } for (int i = 0; i < maxind.length; i++) maxind[i] = Math.rint (maxind[i] * instances.size()); int j = 0; // This gives a slight bias toward putting an extra instance in the last InstanceList. for (int i = 0; i < instances.size(); i++) { while (i >= maxind[j]) j++; ret[j].instances.add (instances.get(i)); } return ret; }
double max = DenseVector.max (scores); double sum = 0; for (int li = 0; li < numLabels; li++)
/** Copy values from an array into this vector. The array should have the * same size as the vector */ public final void arrayCopyFrom( double[] a ) { arrayCopyFrom(a,0); }
private boolean checkForNaN () { for (int i = 0; i < weights.length; i++) { assert (!weights[i].isNaN()); assert (constraints == null || !constraints[i].isNaN()); assert (expectations == null || !expectations[i].isNaN()); } for (int i = 0; i < numStates(); i++) { State s = (State) getState (i); assert (!Double.isNaN (s.initialExpectation)); assert (!Double.isNaN (s.initialConstraint)); assert (!Double.isNaN (s.initialCost)); assert (!Double.isNaN (s.finalExpectation)); assert (!Double.isNaN (s.finalConstraint)); assert (!Double.isNaN (s.finalCost)); } return true; }
public void testDenseSparseVector () { SparseVector svDense = new SparseVector (null, dbl3); double sdot = svDense.dotProduct (svDense); double ddot = d1.dotProduct (d1); assertEquals (sdot, ddot, 0.0001); svDense.plusEqualsSparse (s1); checkAnswer (svDense, new double[] { 2.0, 2.5, 3.0, 5.7, 3.5, 5.6, 0, 3, 0, 0, 0, 0, 0, 4, 0, 5, }); svDense.plusEqualsSparse (s1, 2.0); checkAnswer (svDense, new double[] { 2.0, 2.5, 3.0, 7.7, 3.5, 9.6, 0, 9, 0, 0, 0, 0, 0, 12, 0, 15, }); double[] dbl4 = new double [dbl3.length + 1]; for (int i = 0; i < dbl4.length; i++) dbl4[i] = 2.0; SparseVector sv4 = new SparseVector (null, dbl4); svDense.plusEqualsSparse (sv4); checkAnswer (svDense, new double[] { 4.0, 4.5, 5.0, 9.7, 5.5, 11.6, 2.0, 11.0, 2.0, 2.0, 2, 2, 2, 14, 2.0, 17, }); }