/** 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); }
/** 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))); }
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; }
/** 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; }