本文整理汇总了Java中edu.stanford.nlp.math.ArrayMath.logNormalize方法的典型用法代码示例。如果您正苦于以下问题:Java ArrayMath.logNormalize方法的具体用法?Java ArrayMath.logNormalize怎么用?Java ArrayMath.logNormalize使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类edu.stanford.nlp.math.ArrayMath
的用法示例。
在下文中一共展示了ArrayMath.logNormalize方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: samplePositionHelper
import edu.stanford.nlp.math.ArrayMath; //导入方法依赖的package包/类
/**
* 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);
if (BisequenceEmpiricalNERPrior.DEBUG) {
if (BisequenceEmpiricalNERPrior.debugIndices.indexOf(pos) != -1) {
System.err.println("final model:");
for (int j = 0; j < distribution.length; j++)
System.err.println("\t" + distribution[j]);
System.err.println();
}
}
int newTag = ArrayMath.sampleFromDistribution(distribution, random);
double newProb = distribution[newTag];
return new Pair<Integer, Double>(newTag, newProb);
}
示例2: getConditionalDistribution
import edu.stanford.nlp.math.ArrayMath; //导入方法依赖的package包/类
public double[] getConditionalDistribution (int[] sequence, int position) {
double[] probs = scoresOf(sequence, position);
ArrayMath.logNormalize(probs);
probs = ArrayMath.exp(probs);
//System.out.println(this);
return probs;
}
示例3: conditionalLogProbsGivenPrevious
import edu.stanford.nlp.math.ArrayMath; //导入方法依赖的package包/类
/**
* Computes the probabilities of the tag at the end of the table given that
* the previous tag sequence in table is GIVEN. given is at the beginning,
* position in question is at the end
*
* @return the probabilities of the tag at the end of the table
*/
public double[] conditionalLogProbsGivenPrevious(int[] given) {
if (given.length != windowSize - 1) {
throw new IllegalArgumentException("conditionalLogProbsGivenPrevious requires given one less than clique size (" +
windowSize + ") but was " + Arrays.toString(given));
}
double[] result = new double[numClasses];
for (int i = 0; i < numClasses; i++) {
int index = indexOf(given, i);
result[i] = table[index];
}
ArrayMath.logNormalize(result);
return result;
}
示例4: getConditionalDistribution
import edu.stanford.nlp.math.ArrayMath; //导入方法依赖的package包/类
/**
* Computes the distribution over values of the element at position pos in the
* sequence, conditioned on the values of the elements in all other positions
* of the provided sequence.
*
* @param sequence
* the sequence containing the rest of the values to condition on
* @param position
* the position of the element to give a distribution for
* @return an array of type double, representing a probability distribution;
* sums to 1.0
*/
public double[] getConditionalDistribution(int[] sequence, int position) {
double[] result = scoresOf(sequence, position);
ArrayMath.logNormalize(result);
// System.out.println("marginal: " + ArrayMath.toString(marginal,
// nf));
// System.out.println("conditional: " + ArrayMath.toString(result,
// nf));
result = ArrayMath.exp(result);
// System.out.println("conditional: " + ArrayMath.toString(result,
// nf));
return result;
}
示例5: getExactScores
import edu.stanford.nlp.math.ArrayMath; //导入方法依赖的package包/类
private double[] getExactScores(History h) {
String[] tags = stringTagsAt(h.current - h.start + leftWindow());
double[] histories = getHistories(tags, h); // log score for each tag
ArrayMath.logNormalize(histories);
double[] scores = new double[tags.length];
for (int j = 0; j < tags.length; j++) {
// score the j-th tag
String tag = tags[j];
int tagindex = maxentTagger.tags.getIndex(tag);
scores[j] = histories[tagindex];
}
return scores;
}
示例6: getTopicDistribution
import edu.stanford.nlp.math.ArrayMath; //导入方法依赖的package包/类
/**
* @return A probability distribution, not in log-space.
*/
public double[] getTopicDistribution(int doc, int entityPosition) {
int[] mentionTokens = words[doc][entityPosition];
int[] mentionDeps = deps[doc][entityPosition];
int[] mentionInverseDeps = inverseDeps[doc][entityPosition];
int[] features = (includeEntityFeatures ? feats[doc][entityPosition] : null);
int numMentions = mentionTokens.length;
double[] probs = new double[numTopics];
for (int topic = 0; topic < numTopics; topic++) {
// Probability of topic in this specific document.
double probOfTopic = (thetasInDoc ? probOfTopicGivenDoc(topic, doc) : probOfTopic(topic));
// This assumes all mentions use the same core entity token.
double probOfWGivenTopic = (wCountsBySlot[topic].getCount(mentionTokens[0]) + wSmoothing) / (wCountsBySlot[topic].totalCount() + wSmoothingTimesNumW);
probs[topic] = Math.log(probOfTopic * probOfWGivenTopic);
// Entity features.
if( includeEntityFeatures ) {
for( int feat = 0; feat < numFeats; feat++ ) {
double sumprobs = 0.0;
int numOn = 0;
if( features[feat] == 1) {
sumprobs += (featCountsBySlot[topic].getCount(feat) + featSmoothing) / (featCountsBySlot[topic].totalCount() + featSmoothingTimesNumFeats);
numOn++;
}
if( numOn > 0 ) // same as interpolating with uniform lambda weights on each probability
probs[topic] += Math.log(sumprobs / numOn);
}
}
// Loop over the mentions for this entity.
for( int mention = 0; mention < numMentions; mention++ ) {
// double probOfDepGivenTopic = Math.log(depCountsBySlot[topic].getCount(mentionDeps[mention]) + depSmoothing) - Math.log(depCountsBySlot[topic].totalCount() + depSmoothingTimesNumDeps);
// Divide is faster than logarithm
double probOfDepGivenTopic = (depCountsBySlot[topic].getCount(mentionDeps[mention]) + depSmoothing) / (depCountsBySlot[topic].totalCount() + depSmoothingTimesNumDeps);
// Penalty if the nsubj is much higher than the dobj in this topic.
if( constrainInverseDeps && inverseDeps != null && currentIteration > 50 ) {
// Only nsubj and dobj have inverses, so things like prep_in are null (value -1 in the array).
if( mentionInverseDeps[mention] >= 0 ) {
double probOfInverseDepGivenTopic = (depCountsBySlot[topic].getCount(mentionInverseDeps[mention]) + depSmoothing) / (depCountsBySlot[topic].totalCount() + depSmoothingTimesNumDeps);
if( probOfInverseDepGivenTopic > probOfDepGivenTopic+0.03 ) {
// System.out.printf("Higher topic %d, %s: %.4f\t%.4f\n", topic, depIndex.get(mentionDeps[mention]), probOfInverseDepGivenTopic, probOfDepGivenTopic);
probOfDepGivenTopic = .0001;
}
}
}
probOfDepGivenTopic = Math.log(probOfDepGivenTopic);
// System.out.printf(" %s dep count %.1f total dep count %.1f, depsmoothing=%.1f, deptotalsmoothing=%.1f\n",
// dep, depCountsBySlot[topic].getCount(mentionDeps[mention]), depCountsBySlot[topic].totalCount(), depSmoothing, depSmoothingTimesNumDeps);
// System.out.printf(" %s word count %.1f total word count %.1f, wsmoothing=%.1f, wtotalsmoothing=%.1f\n",
// word, wCountsBySlot[topic].getCount(mentionTokens[mention]), wCountsBySlot[topic].totalCount(), wSmoothing, wSmoothingTimesNumW);
probs[topic] += probOfDepGivenTopic;
// DEBUGGING OUTPUT
// System.out.printf("P(slot=%d)= %.5f * P(%s|slot=%d)= %.5f(%.5f) * P(%s|slot=%d)= %.5f(%.5f) \t= %.5f(%.9f)\n",
// topic, probOfTopic, word, topic, Math.exp(probOfWGivenTopic), probOfWGivenTopic, dep, topic, Math.exp(probOfDepGivenTopic), probOfDepGivenTopic, probs[topic], Math.exp(probs[topic]));
}
}
ArrayMath.logNormalize(probs);
ArrayMath.expInPlace(probs);
// ArrayMath.normalize(probs);
// System.out.println("Returning probs: " + Arrays.toString(probs));
return probs;
}
示例7: scoresOf
import edu.stanford.nlp.math.ArrayMath; //导入方法依赖的package包/类
public double[] scoresOf(int[] tags, int pos) {
if (VERBOSE) {
int p = (100 * pos) / length();
if (p > percent) {
long secs2 = System.currentTimeMillis();
System.err.println(StringUtils.padLeft(p, 3) + "% " + ((secs2 - secs == 0) ? 0 : (num * 1000 / (secs2 - secs))) + " hits per sec, position=" + pos + ", legal=" + ((tot == 0) ? 100 : ((100 * hit) / tot)));
// + "% [hit=" + hit + ", tot=" + tot + "]");
percent = p;
num = 0;
secs = secs2;
}
tot++;
}
String[] answers = new String[1 + leftWindow() + rightWindow()];
String[] pre = new String[leftWindow()];
for (int i = 0; i < 1 + leftWindow() + rightWindow(); i++) {
int absPos = pos - leftWindow() + i;
if (absPos < 0) {
continue;
}
answers[i] = tagIndex.get(tags[absPos]);
CoreLabel li = lineInfos.get(absPos);
li.set(CoreAnnotations.AnswerAnnotation.class, answers[i]);
if (i < leftWindow()) {
pre[i] = answers[i];
}
}
double[] scores = new double[tagIndex.size()];
//System.out.println("Considering: "+Arrays.asList(pre));
if (!legalTags.contains(Arrays.asList(pre)) && classifier.flags.useObservedSequencesOnly) {
// System.out.println("Rejecting: " + Arrays.asList(pre));
// System.out.println(legalTags);
Arrays.fill(scores, -1000);// Double.NEGATIVE_INFINITY;
return scores;
}
num++;
hit++;
Counter<String> c = classifier.scoresOf(lineInfos, pos);
//System.out.println("Pos "+pos+" hist "+Arrays.asList(pre)+" result "+c);
//System.out.println(c);
//if (false && flags.justify) {
// System.out.println("Considering position " + pos + ", word is " + ((CoreLabel) lineInfos.get(pos)).word());
// //System.out.println("Datum is "+d.asFeatures());
// System.out.println("History: " + Arrays.asList(pre));
//}
for (String s : c.keySet()) {
int t = tagIndex.indexOf(s);
if (t > -1) {
int[] tA = getPossibleValues(pos);
for (int j = 0; j < tA.length; j++) {
if (tA[j] == t) {
scores[j] = c.getCount(s);
//if (false && flags.justify) {
// System.out.println("Label " + s + " got score " + scores[j]);
//}
}
}
}
}
// normalize?
if (classifier.normalize()) {
ArrayMath.logNormalize(scores);
}
return scores;
}
示例8: calculateProbs
import edu.stanford.nlp.math.ArrayMath; //导入方法依赖的package包/类
/** calculateProbs puts log probs of taggings in the probabilities array.
*
* @param probabilities Array with indices sent size, k best size, numTags
*/
protected void calculateProbs(double[][][] probabilities) {
ArrayUtils.fill(probabilities, Double.NEGATIVE_INFINITY);
for (int hyp = 0; hyp < kBestSize; hyp++) {
// put the whole thing in pairs, give its beginning and end
pairs.setSize(size);
for (int i = 0; i < size; i++) {
pairs.setWord(i,sent.get(i));
pairs.setTag(i,finalTags[i]);
//pairs.add(new WordTag(sent.get(i),finalTags[i]));
// TODO: if kBestSize > 1, use KBestSequenceFinder and save
// k-best hypotheses into finalTags:
//pairs.setTag(i,finalTags[i]);
}
int start = endSizePairs;
int end = endSizePairs + size - 1;
endSizePairs = endSizePairs + size;
// iterate over the sentence
for (int current = 0; current < size; current++) {
History h = new History(start, end, current + start, pairs, maxentTagger.extractors);
String[] tags = stringTagsAt(h.current - h.start + leftWindow());
double[] probs = getHistories(tags, h);
ArrayMath.logNormalize(probs);
// System.err.println("word: " + pairs.getWord(current));
// System.err.println("tags: " + Arrays.asList(tags));
// System.err.println("probs: " + ArrayMath.toString(probs));
for (int j = 0; j < tags.length; j++) {
// score the j-th tag
String tag = tags[j];
boolean approximate = maxentTagger.defaultScore > 0.0;
int tagindex = approximate ? maxentTagger.tags.getIndex(tag) : j;
// System.err.println("Mapped from j="+ j + " " + tag + " to " + tagindex);
probabilities[current][hyp][tagindex] = probs[j];
}
} // for current
} // for hyp
// clean up the stuff in PairsHolder (added by cdm in Aug 2008)
revert(0);
}
示例9: calculateProbs
import edu.stanford.nlp.math.ArrayMath; //导入方法依赖的package包/类
/** calculateProbs puts log probs of taggings in the probabilities array.
*
* @param probabilities Array with indices sent size, k best size, numTags
*/
protected void calculateProbs(double[][][] probabilities) {
ArrayUtils.fill(probabilities, Double.NEGATIVE_INFINITY);
for (int hyp = 0; hyp < kBestSize; hyp++) {
// put the whole thing in pairs, give its beginning and end
pairs.setSize(size);
for (int i = 0; i < size; i++) {
pairs.setWord(i,sent.get(i));
pairs.setTag(i,finalTags[i]);
//pairs.add(new WordTag(sent.get(i),finalTags[i]));
// TODO: if kBestSize > 1, use KBestSequenceFinder and save
// k-best hypotheses into finalTags:
//pairs.setTag(i,finalTags[i]);
}
int start = endSizePairs;
int end = endSizePairs + size - 1;
endSizePairs = endSizePairs + size;
// iterate over the sentence
for (int current = 0; current < size; current++) {
History h = new History(start, end, current + start, pairs, maxentTagger.extractors);
String[] tags = stringTagsAt(h.current - h.start + leftWindow());
double[] probs = getHistories(tags, h);
ArrayMath.logNormalize(probs);
// System.err.println("word: " + pairs.getWord(current));
// System.err.println("tags: " + Arrays.asList(tags));
// System.err.println("probs: " + ArrayMath.toString(probs));
for (int j = 0; j < tags.length; j++) {
// score the j-th tag
String tag = tags[j];
boolean approximate = maxentTagger.hasApproximateScoring();
int tagindex = approximate ? maxentTagger.tags.getIndex(tag) : j;
// System.err.println("Mapped from j="+ j + " " + tag + " to " + tagindex);
probabilities[current][hyp][tagindex] = probs[j];
}
} // for current
} // for hyp
// clean up the stuff in PairsHolder (added by cdm in Aug 2008)
revert(0);
}