当前位置: 首页>>代码示例>>Java>>正文


Java DoubleMath.log2方法代码示例

本文整理汇总了Java中com.google.common.math.DoubleMath.log2方法的典型用法代码示例。如果您正苦于以下问题:Java DoubleMath.log2方法的具体用法?Java DoubleMath.log2怎么用?Java DoubleMath.log2使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在com.google.common.math.DoubleMath的用法示例。


在下文中一共展示了DoubleMath.log2方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: getEntropyOfRules

import com.google.common.math.DoubleMath; //导入方法依赖的package包/类
/**
 * Return the entropy (unnormalised) of the given rules
 *
 * @param otherCfg
 * @return
 */
public double getEntropyOfRules(final ContextFreeGrammar otherCfg) {
	double sum = 0;

	for (final Entry<Integer, Multiset<NodeConsequent>> entry : otherCfg.grammar
			.entrySet()) {
		final int fromNode = entry.getKey();
		for (final Multiset.Entry<NodeConsequent> toNodes : entry
				.getValue().entrySet()) {
			sum += toNodes.getCount()
					* DoubleMath.log2(getMLProbability(fromNode,
							toNodes.getElement()));
		}
	}

	return sum;
}
 
开发者ID:mast-group,项目名称:codemining-treelm,代码行数:23,代码来源:AbstractContextFreeGrammar.java

示例2: getLogProbOfSentence

import com.google.common.math.DoubleMath; //导入方法依赖的package包/类
public double getLogProbOfSentence(final List<String> sentence) {
	double logProb = 0;
	for (int i = 0; i < sentence.size(); ++i) {
		final NGram<String> ngram = NGram.constructNgramAt(i, sentence,
				nGramSize);
		if (ngram.size() > 1) {
			final double prob = getProbabilityFor(ngram);
			if (AbstractNGramLM.DEBUG_PROBS) {
				LOGGER.info(AbstractNGramLM.getProbString(
						trie.substituteWordsToUNK(ngram), prob));
			}
			checkArgument(prob > 0);
			checkArgument(!Double.isInfinite(prob));
			logProb += DoubleMath.log2(prob);
		}
	}
	return logProb;
}
 
开发者ID:mast-group,项目名称:codemining-sequencelm,代码行数:19,代码来源:AbstractNGramLM.java

示例3: getLogProbOfSentence

import com.google.common.math.DoubleMath; //导入方法依赖的package包/类
private double getLogProbOfSentence(final List<FullToken> sentence,
		final String className) {
	final ICache<String> cache = createCache(className);
	double logProb = 0;

	final List<String> stringToks = Lists.newArrayList();
	for (int i = 0; i < sentence.size(); i++) {
		stringToks.add(sentence.get(i).token);
	}

	for (int i = 0; i < sentence.size(); ++i) {
		final NGram<String> ngram = NGram.constructNgramAt(i, stringToks,
				baseNgram.getN());
		if (ngram.size() > 1) {
			final double prob = getProbabilityFor(ngram, cache);

			checkArgument(prob > 0);
			checkArgument(!Double.isInfinite(prob));
			logProb += DoubleMath.log2(prob);
		}
	}
	return logProb;
}
 
开发者ID:mast-group,项目名称:codemining-sequencelm,代码行数:24,代码来源:SimpleCachedNGramLM.java

示例4: calculateScores

import com.google.common.math.DoubleMath; //导入方法依赖的package包/类
@Override
public SortedSet<Renaming> calculateScores(
		final Multiset<NGram<String>> ngrams,
		final Set<String> alternatives, final Scope scope) {
	final SortedSet<Renaming> scoreMap = Sets.newTreeSet();

	for (final String identifierName : alternatives) {
		double score = 0;
		for (final Entry<NGram<String>> ngram : ngrams.entrySet()) {
			try {
				final NGram<String> identNGram = NGram.substituteTokenWith(
						ngram.getElement(), WILDCARD_TOKEN, identifierName);
				final double ngramScore = scoreNgram(identNGram);
				score += DoubleMath.log2(ngramScore) * ngram.getCount();
			} catch (final Throwable e) {
				LOGGER.warning(ExceptionUtils.getFullStackTrace(e));
			}
		}
		scoreMap.add(new Renaming(identifierName, (addScopePriors(
				identifierName, scope) - score) / ngrams.size(), ngrams
				.size() / ngramLM.getN(), scope));
	}

	return scoreMap;
}
 
开发者ID:mast-group,项目名称:naturalize,代码行数:26,代码来源:AbstractIdentifierRenamings.java

示例5: calculateScores

import com.google.common.math.DoubleMath; //导入方法依赖的package包/类
/**
 * Predict the max-likelihood tokens given the ngrams.
 * 
 * @param ngrams
 * @param alternatives
 * @return
 */
@Override
public SortedSet<Renaming> calculateScores(
		final Multiset<NGram<String>> ngrams,
		final Set<String> alternatives, final Scope scope) {
	final SortedSet<Renaming> suggestions = Sets.newTreeSet();

	for (final String alternative : alternatives) {
		double score = 0;
		for (final NGram<String> ngram : ngrams) {
			score += DoubleMath.log2(getNgramLM().getProbabilityFor(
					NGram.substituteTokenWith(ngram, WILDCARD_TOKEN,
							alternative)));
		}
		suggestions.add(new Renaming(alternative, -score, 1, null));
	}
	return suggestions;
}
 
开发者ID:mast-group,项目名称:naturalize,代码行数:25,代码来源:FormattingRenamings.java

示例6: bestGraphemes

import com.google.common.math.DoubleMath; //导入方法依赖的package包/类
public List<Alignment> bestGraphemes(Word x, int bestPathCount) {
  PathXTable t = new PathXTable(x.unigramCount() + 1, bestPathCount);
  t.offer(0, t.make(0, -1, -1));

  for (int xx = 1; xx < x.unigramCount() + 1; xx++) {
    for (int i = 1; (i <= opts.getMaxXGram()) && (xx - i >= 0); i++) {
      String xGram = x.gram(xx - i, i);
      double margX = margs.probX(xGram);

      double score = DoubleMath.log2(margX) * i;
      t.extendPath(xx, xx - i, PathXTable.Entry.sample(score, i));
    }
  }

  return createAlignments(x, t, bestPathCount);
}
 
开发者ID:steveash,项目名称:jg2p,代码行数:17,代码来源:AlignerInferencer.java

示例7: entropy

import com.google.common.math.DoubleMath; //导入方法依赖的package包/类
public double entropy() {
    if (falseCount == 0 && trueCount == 0) {
        return Double.NaN;
    } else if (falseCount == 0 || trueCount == 0) {
        return 0;
    }

    int totalCount = falseCount + trueCount;

    double falseProb = (double) falseCount / totalCount;
    double trueProb = 1.0 - falseProb;

    double falseEntropy = falseProb * DoubleMath.log2(falseProb);
    double trueEntropy = trueProb * DoubleMath.log2(trueProb);

    return -(falseEntropy + trueEntropy);
}
 
开发者ID:ksean,项目名称:mnist-machine-learning,代码行数:18,代码来源:BooleanStat.java

示例8: log2Round

import com.google.common.math.DoubleMath; //导入方法依赖的package包/类
@Benchmark int log2Round(int reps) {
  int tmp = 0;
  for (int i = 0; i < reps; i++) {
    int j = i & ARRAY_MASK;
    tmp += DoubleMath.log2(positiveDoubles[j], mode);
  }
  return tmp;
}
 
开发者ID:sander120786,项目名称:guava-libraries,代码行数:9,代码来源:DoubleMathRoundingBenchmark.java

示例9: computeLog2PosteriorProbabilityOfRule

import com.google.common.math.DoubleMath; //导入方法依赖的package包/类
@Override
public double computeLog2PosteriorProbabilityOfRule(
		final TreeNode<TSGNode> tree, final boolean remove) {
	checkNotNull(tree);

	double nRulesCommonRoot = grammar.countTreesWithRoot(tree.getData());
	double nRulesInGrammar = grammar.countTreeOccurences(tree);

	if (nRulesInGrammar > nRulesCommonRoot) { // Concurrency has bitten
		// us... Sorry no
		// guarantees, but it's
		// the
		// most probable that we just removed it...
		nRulesInGrammar = nRulesCommonRoot;
	}

	final double log2prior = getLog2PriorForTree(tree);
	checkArgument(
			!Double.isInfinite(log2prior) && !Double.isNaN(log2prior),
			"Prior is %s", log2prior);

	if (nRulesInGrammar > 0 && remove) {
		nRulesInGrammar--;
		nRulesCommonRoot--;
	}

	final double log2Probability = StatsUtil.log2SumOfExponentials(
			DoubleMath.log2(nRulesInGrammar),
			DoubleMath.log2(concentrationParameter) + log2prior)
			- DoubleMath.log2(nRulesCommonRoot + concentrationParameter);

	checkArgument(
			!Double.isNaN(log2Probability)
					&& !Double.isInfinite(log2Probability),
			"Posterior probability is %s", log2Probability);
	checkArgument(log2Probability <= 0);
	return log2Probability;
}
 
开发者ID:mast-group,项目名称:codemining-treelm,代码行数:39,代码来源:ClassicTsgPosteriorComputer.java

示例10: computeLog2PosteriorProbabilityOfRule

import com.google.common.math.DoubleMath; //导入方法依赖的package包/类
@Override
public double computeLog2PosteriorProbabilityOfRule(final TreeNode<TSGNode> tree, final boolean remove) {
    checkNotNull(tree);

    double nRulesCommonRoot = grammar.countTreesWithRoot(tree.getData());
    double nRulesInGrammar = grammar.countTreeOccurences(tree);

    if (nRulesInGrammar > nRulesCommonRoot) { // Concurrency has bitten
        // us... Sorry no
        // guarantees, but it's
        // the
        // most probable that we just removed it...
        nRulesInGrammar = nRulesCommonRoot;
    }

    final double log2prior = getLog2PriorForTree(tree);
    checkArgument(!Double.isInfinite(log2prior) && !Double.isNaN(log2prior), "Prior is %s", log2prior);

    if (nRulesInGrammar > 0 && remove) {
        nRulesInGrammar--;
        nRulesCommonRoot--;
    }

    double log2Probability = StatsUtil.log2SumOfExponentials(DoubleMath.log2(nRulesInGrammar),
            DoubleMath.log2(concentrationParameter) + log2prior)
            - DoubleMath.log2(nRulesCommonRoot + concentrationParameter);

    if (log2Probability > 0 && log2Probability < 1e15) {
        log2Probability = 0; // Ignore small numerical errors
    }
    checkArgument(!Double.isNaN(log2Probability) && !Double.isInfinite(log2Probability),
            "Posterior probability is %s", log2Probability);
    checkArgument(log2Probability <= 0, "Value is %s", log2Probability);
    return log2Probability;
}
 
开发者ID:mast-group,项目名称:codemining-treelm,代码行数:36,代码来源:BlockCollapsedGibbsSampler.java

示例11: getLog2ProbForCFG

import com.google.common.math.DoubleMath; //导入方法依赖的package包/类
/**
 * Return the log probability of the given PCFG rule.
 *
 * @param from
 * @param to
 * @return
 */
public double getLog2ProbForCFG(
		final AbstractContextFreeGrammar.CFGRule rule) {
	checkNotNull(rule);
	double mlProbability = cfg.getMLProbability(rule.root,
			rule.ruleConsequent);
	if (Double.compare(mlProbability, 0) == 0) {
		mlProbability = 10E-10; // An arbitrary small probability.
	}
	final double logProb = DoubleMath.log2(mlProbability);

	checkArgument(!Double.isNaN(logProb), "LogProb is %s", logProb);
	return logProb;
}
 
开发者ID:mast-group,项目名称:codemining-treelm,代码行数:21,代码来源:CFGPrior.java

示例12: computeLog2PosteriorProbabilityOfRule

import com.google.common.math.DoubleMath; //导入方法依赖的package包/类
@Override
public double computeLog2PosteriorProbabilityOfRule(
		final TreeNode<String> tree, final boolean remove) {
	double nRulesCommonRoot = grammar
			.countTreesWithRoot(tree.getData());
	double nRulesInGrammar = grammar.countTreeOccurences(tree);

	if (nRulesInGrammar > 0 && remove) {
		nRulesInGrammar--;
		nRulesCommonRoot--;
	}

	return DoubleMath.log2(nRulesInGrammar / nRulesCommonRoot);
}
 
开发者ID:mast-group,项目名称:codemining-treelm,代码行数:15,代码来源:TreeProbabilityComputerTest.java

示例13: computeValue

import com.google.common.math.DoubleMath; //导入方法依赖的package包/类
/**
 * @param decayValue
 * @param lamdbaValue
 * @return
 */
private double computeValue(final double decayValue, double lamdbaValue) {

	double penalty = 0;
	if (lamdbaValue < 0) {
		lamdbaValue = 10E-10;
		penalty = lamdbaValue * lamdbaValue;
	} else if (lamdbaValue > 1) {
		lamdbaValue = 1 - 10E-10;
		penalty = (lamdbaValue - 1) * (lamdbaValue - 1);
	}

	double sum = 0;
	for (final Entry<LPair> p : elems.entrySet()) {
		double decaySums = 0;
		for (final DecayFactor f : p.getElement().cacheProb) {
			decaySums += f.getForAlpha(decayValue);
		}

		checkArgument(p.getElement().ngramProb > 0
				&& p.getElement().ngramProb <= 1.);
		double baseProb = lamdbaValue * decaySums + (1. - lamdbaValue)
				* p.getElement().ngramProb;
		sum += p.getCount()
				* DoubleMath.log2(p.getElement().importance * baseProb
						+ (1. - p.getElement().importance)
						* p.getElement().ngramProb);
	}
	final double value = sum / elems.size() - penalty;
	checkArgument(!Double.isInfinite(value) && !Double.isNaN(value),
			"Value Should not be NaN or Inf but is " + value + " with sum="
					+ sum);

	return value;
}
 
开发者ID:mast-group,项目名称:codemining-sequencelm,代码行数:40,代码来源:ParameterOptimizer.java

示例14: getLogProbOfSentence

import com.google.common.math.DoubleMath; //导入方法依赖的package包/类
private double getLogProbOfSentence(final List<FullToken> sentence,
		final String className) {
	final ICache<String> cache = createCache(className);
	double logProb = 0;

	final List<String> tokenValues = Lists.newArrayList();
	final List<String> tokenTypes = Lists.newArrayList();
	for (int i = 0; i < sentence.size(); i++) {
		tokenValues.add(sentence.get(i).token);
		tokenTypes.add(sentence.get(i).tokenType);
	}

	for (int i = 0; i < sentence.size(); ++i) {
		final NGram<String> tokTypeNgram = NGram.constructNgramAt(i,
				tokenTypes, typeNgram.getN());
		final NGram<String> valueNgram = NGram.constructNgramAt(i,
				tokenValues, baseNgram.getN());

		final double probOfIdentifier = typeNgram
				.getProbabilityFor(constructIdentNgram(tokTypeNgram));

		if (valueNgram.size() > 1) {
			final double prob = probOfIdentifier
					* getProbabilityFor(valueNgram, cache, probOfIdentifier)
					+ (1. - probOfIdentifier)
					* baseNgram.getProbabilityFor(valueNgram);
			checkArgument(prob > 0);
			checkArgument(!Double.isInfinite(prob));
			logProb += DoubleMath.log2(prob);
		}

		if (tokTypeNgram.get(tokTypeNgram.size() - 1).equals(
				identiferNameType)) {
			cache.pushElement(valueNgram.get(valueNgram.size() - 1));
		}
	}
	return logProb;
}
 
开发者ID:mast-group,项目名称:codemining-sequencelm,代码行数:39,代码来源:IdentifierOnlyCachedNGramLM.java

示例15: getLogProbOfNotRenaming

import com.google.common.math.DoubleMath; //导入方法依赖的package包/类
/**
 * Get the probability of a specific renaming happening (taking at least
 * one of the suggestions).
 * 
 * @param suggestions
 * @return
 */
public double getLogProbOfNotRenaming() {
	double logProbNotRename = 0;
	for (final Suggestion sg : suggestions) {
		final double pNotRename = sg.getProbNotRename();
		checkArgument(pNotRename <= 1, pNotRename
				+ " should be in the probability range");
		checkArgument(pNotRename >= 0, pNotRename
				+ " should be in the probability range");
		logProbNotRename += DoubleMath.log2(pNotRename);
	}
	return logProbNotRename;
}
 
开发者ID:mast-group,项目名称:naturalize,代码行数:20,代码来源:SnippetScorer.java


注:本文中的com.google.common.math.DoubleMath.log2方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。