@Override public void setWeights(double[][] weights) { super.setWeights(weights); }
@Override public void setWeights(double[][] weights) { super.setWeights(weights); if (weightSquare == null) { weightSquare = new double[weights.length][]; for (int i = 0; i < weights.length; i++) weightSquare[i] = new double[weights[i].length]; } for (int i = 0; i < weights.length; i++) { for (int j=0; j < weights[i].length; j++) { double w = weights[i][j]; weightSquare[i][j] = w * w; } } }
/** * Computes value of function for specified value of x (scaled by xScale) * only over samples indexed by batch. * NOTE: This function does not do regularization (regularization is done by the minimizer). * * @param x - unscaled weights * @param xScale - how much to scale x by when performing calculations * @param batch - indices of which samples to compute function over * @return value of function at specified x (scaled by xScale) for samples */ @Override public double valueAt(double[] x, double xScale, int[] batch) { double prob = 0.0; // the log prob of the sequence given the model, which is the negation of value at this point // int[][] wis = getWeightIndices(); to2D(x, xScale, weights); setWeights(weights); // iterate over all the documents for (int ind : batch) { prob += valueForADoc(ind); } if (Double.isNaN(prob)) { // shouldn't be the case throw new RuntimeException("Got NaN for prob in CRFLogConditionalObjectiveFunction.calculate()"); } value = -prob; return value; }
setWeights(weights);
setWeights(weights);
@Override public void calculateStochastic(double[] x, double [] v, int[] batch) { to2D(x, weights); setWeights(weights);
setWeights(weights);
@Override public void setWeights(double[][] weights) { super.setWeights(weights); }
public void setWeights(double[][] weights) { super.setWeights(weights); }
@Override public void setWeights(double[][] weights) { super.setWeights(weights); if (weightSquare == null) { weightSquare = new double[weights.length][]; for (int i = 0; i < weights.length; i++) weightSquare[i] = new double[weights[i].length]; } for (int i = 0; i < weights.length; i++) { for (int j=0; j < weights[i].length; j++) { double w = weights[i][j]; weightSquare[i][j] = w * w; } } }
@Override public void setWeights(double[][] weights) { super.setWeights(weights); if (weightSquare == null) { weightSquare = new double[weights.length][]; for (int i = 0; i < weights.length; i++) weightSquare[i] = new double[weights[i].length]; } double w = 0; for (int i = 0; i < weights.length; i++) { for (int j=0; j < weights[i].length; j++) { w = weights[i][j]; weightSquare[i][j] = w * w; } } }
/** * Computes value of function for specified value of x (scaled by xScale) * only over samples indexed by batch. * NOTE: This function does not do regularization (regularization is done by the minimizer). * * @param x - unscaled weights * @param xScale - how much to scale x by when performing calculations * @param batch - indices of which samples to compute function over * @return value of function at specified x (scaled by xScale) for samples */ @Override public double valueAt(double[] x, double xScale, int[] batch) { double prob = 0.0; // the log prob of the sequence given the model, which is the negation of value at this point // int[][] wis = getWeightIndices(); to2D(x, xScale, weights); setWeights(weights); // iterate over all the documents for (int ind : batch) { prob += valueForADoc(ind); } if (Double.isNaN(prob)) { // shouldn't be the case throw new RuntimeException("Got NaN for prob in CRFLogConditionalObjectiveFunction.calculate()"); } value = -prob; return value; }
/** * Computes value of function for specified value of x (scaled by xScale) * only over samples indexed by batch. * NOTE: This function does not do regularization (regularization is done by the minimizer). * * @param x - unscaled weights * @param xScale - how much to scale x by when performing calculations * @param batch - indices of which samples to compute function over * @return value of function at specified x (scaled by xScale) for samples */ @Override public double valueAt(double[] x, double xScale, int[] batch) { double prob = 0.0; // the log prob of the sequence given the model, which is the negation of value at this point // int[][] wis = getWeightIndices(); to2D(x, xScale, weights); setWeights(weights); // iterate over all the documents for (int ind : batch) { prob += valueForADoc(ind); } if (Double.isNaN(prob)) { // shouldn't be the case throw new RuntimeException("Got NaN for prob in CRFLogConditionalObjectiveFunction.calculate()"); } value = -prob; return value; }
setWeights(weights);
double prob = 0.0; // the log prob of the sequence given the model, which is the negation of value at this point to2D(x, weights); setWeights(weights);
/** * Performs stochastic gradient update based * on samples indexed by batch, but does not apply regularization. * * @param x - unscaled weights * @param batch - indices of which samples to compute function over */ @Override public void calculateStochasticGradient(double[] x, int[] batch) { if (derivative == null) { derivative = new double[domainDimension()]; } // int[][] wis = getWeightIndices(); // was: double[][] weights = to2D(x, 1.0); // but 1.0 should be the same as omitting 2nd parameter.... to2D(x, weights); setWeights(weights); // iterate over all the documents List<Integer> docIDs = new ArrayList<Integer>(batch.length); for (int m=0; m < batch.length; m++) docIDs.add(batch[m]); multiThreadGradient(docIDs, true); int index = 0; for (int i = 0; i < E.length; i++) { for (int j = 0; j < E[i].length; j++) { // real gradient should be empirical-expected; // but since we minimize -L(\theta), the gradient is -(empirical-expected) derivative[index++] = (E[i][j]-Ehat[i][j]); } } }
setWeights(weights);
@Override public void calculateStochastic(double[] x, double [] v, int[] batch) { to2D(x, weights); setWeights(weights);