/** * Samples from the distribution over values 0 through d.length given by d. * Assumes that the distribution sums to 1.0. * * @param d the distribution to sample from * @return a value from 0 to d.length */ public static int sampleFromDistribution(double[] d) { return sampleFromDistribution(d, rand); }
/** * Samples a single position in the sequence. * Does not modify the sequence passed in. * returns the score of the new label for the position to sample * @param sequence the sequence to start with * @param pos the position to sample. * @param temperature the temperature to control annealing */ private Pair<Integer, Double> samplePositionHelper(SequenceModel model, int[] sequence, int pos, double temperature) { double[] distribution = model.scoresOf(sequence, pos); if (temperature!=1.0) { if (temperature==0.0) { // set the max to 1.0 int argmax = ArrayMath.argmax(distribution); Arrays.fill(distribution, Double.NEGATIVE_INFINITY); distribution[argmax] = 0.0; } else { // take all to a power // use the temperature to increase/decrease the entropy of the sampling distribution ArrayMath.multiplyInPlace(distribution, 1.0/temperature); } } ArrayMath.logNormalize(distribution); ArrayMath.expInPlace(distribution); int newTag = ArrayMath.sampleFromDistribution(distribution, random); double newProb = distribution[newTag]; return new Pair<>(newTag, newProb); }
/** * Samples each label in turn from left to right. * * @return an array containing the int tags of the best sequence */ @Override public int[] bestSequence(SequenceModel ts) { // Also allocate space for rightWindow, just in case sequence model uses // it, even though this implementation doesn't. Probably it shouldn't, // or the left-to-right sampling is invalid, but our test classes do. int[] sample = new int[ts.length()+ts.leftWindow()+ts.rightWindow()]; for (int pos = ts.leftWindow(); pos < sample.length - ts.rightWindow(); pos++) { double[] scores = ts.scoresOf(sample, pos); for (int i = 0; i < scores.length; i++) { scores[i] = Math.exp(scores[i]); } ArrayMath.normalize(scores); int l = ArrayMath.sampleFromDistribution(scores); sample[pos] = ts.getPossibleValues(pos)[l]; } return sample; }
/** * Samples from the distribution over values 0 through d.length given by d. * Assumes that the distribution sums to 1.0. * * @param d the distribution to sample from * @return a value from 0 to d.length */ public static int sampleFromDistribution(double[] d) { return sampleFromDistribution(d, rand); }
/** * Samples from the distribution over values 0 through d.length given by d. * Assumes that the distribution sums to 1.0. * * @param d the distribution to sample from * @return a value from 0 to d.length */ public static int sampleFromDistribution(double[] d) { return sampleFromDistribution(d, rand); }
/** * Samples from the distribution over values 0 through d.length given by d. * Assumes that the distribution sums to 1.0. * * @param d the distribution to sample from * @return a value from 0 to d.length */ public static int sampleFromDistribution(double[] d) { return sampleFromDistribution(d, rand); }
/** * Samples from the distribution over values 0 through d.length given by d. * Assumes that the distribution sums to 1.0. * * @param d the distribution to sample from * @return a value from 0 to d.length */ public static int sampleFromDistribution(double[] d) { return sampleFromDistribution(d, rand); }
/** * Runs the Viterbi algorithm on the sequence model given by the TagScorer * in order to find the best sequence. * @return an array containing the int tags of the best sequence */ public int[] bestSequence(SequenceModel ts) { int[] sample = new int[ts.length()+ts.leftWindow()]; for (int pos = ts.leftWindow(); pos < sample.length; pos++) { double[] scores = ts.scoresOf(sample, pos); double total = 0.0; for (int i = 0; i < scores.length; i++) { scores[i] = Math.exp(scores[i]); } ArrayMath.normalize(scores); int l = ArrayMath.sampleFromDistribution(scores); sample[pos] = ts.getPossibleValues(pos)[l]; } return sample; }
/** * Samples a single position in the sequence. * Does not modify the sequence passed in. * returns the score of the new label for the position to sample * @param sequence the sequence to start with * @param pos the position to sample. * @param temperature the temperature to control annealing */ private Pair<Integer, Double> samplePositionHelper(SequenceModel model, int[] sequence, int pos, double temperature) { double[] distribution = model.scoresOf(sequence, pos); if (temperature!=1.0) { if (temperature==0.0) { // set the max to 1.0 int argmax = ArrayMath.argmax(distribution); Arrays.fill(distribution, Double.NEGATIVE_INFINITY); distribution[argmax] = 0.0; } else { // take all to a power // use the temperature to increase/decrease the entropy of the sampling distribution ArrayMath.multiplyInPlace(distribution, 1.0/temperature); } } ArrayMath.logNormalize(distribution); ArrayMath.expInPlace(distribution); int newTag = ArrayMath.sampleFromDistribution(distribution, random); double newProb = distribution[newTag]; return new Pair<>(newTag, newProb); }
/** * Samples a single position in the sequence. * Does not modify the sequence passed in. * returns the score of the new label for the position to sample * @param sequence the sequence to start with * @param pos the position to sample. * @param temperature the temperature to control annealing */ private Pair<Integer, Double> samplePositionHelper(SequenceModel model, int[] sequence, int pos, double temperature) { double[] distribution = model.scoresOf(sequence, pos); if (temperature!=1.0) { if (temperature==0.0) { // set the max to 1.0 int argmax = ArrayMath.argmax(distribution); Arrays.fill(distribution, Double.NEGATIVE_INFINITY); distribution[argmax] = 0.0; } else { // take all to a power // use the temperature to increase/decrease the entropy of the sampling distribution ArrayMath.multiplyInPlace(distribution, 1.0/temperature); } } ArrayMath.logNormalize(distribution); ArrayMath.expInPlace(distribution); int newTag = ArrayMath.sampleFromDistribution(distribution, random); double newProb = distribution[newTag]; return new Pair<Integer, Double>(newTag, newProb); }
ArrayMath.expInPlace(distribution); int oldTag = sequence[pos]; int newTag = ArrayMath.sampleFromDistribution(distribution, random);
/** * Samples each label in turn from left to right. * * @return an array containing the int tags of the best sequence */ @Override public int[] bestSequence(SequenceModel ts) { // Also allocate space for rightWindow, just in case sequence model uses // it, even though this implementation doesn't. Probably it shouldn't, // or the left-to-right sampling is invalid, but our test classes do. int[] sample = new int[ts.length()+ts.leftWindow()+ts.rightWindow()]; for (int pos = ts.leftWindow(); pos < sample.length - ts.rightWindow(); pos++) { double[] scores = ts.scoresOf(sample, pos); for (int i = 0; i < scores.length; i++) { scores[i] = Math.exp(scores[i]); } ArrayMath.normalize(scores); int l = ArrayMath.sampleFromDistribution(scores); sample[pos] = ts.getPossibleValues(pos)[l]; } return sample; }
/** * Samples each label in turn from left to right. * * @return an array containing the int tags of the best sequence */ @Override public int[] bestSequence(SequenceModel ts) { // Also allocate space for rightWindow, just in case sequence model uses // it, even though this implementation doesn't. Probably it shouldn't, // or the left-to-right sampling is invalid, but our test classes do. int[] sample = new int[ts.length()+ts.leftWindow()+ts.rightWindow()]; for (int pos = ts.leftWindow(); pos < sample.length - ts.rightWindow(); pos++) { double[] scores = ts.scoresOf(sample, pos); for (int i = 0; i < scores.length; i++) { scores[i] = Math.exp(scores[i]); } ArrayMath.normalize(scores); int l = ArrayMath.sampleFromDistribution(scores); sample[pos] = ts.getPossibleValues(pos)[l]; } return sample; }