本文整理汇总了Java中edu.jhu.prim.util.math.FastMath类的典型用法代码示例。如果您正苦于以下问题:Java FastMath类的具体用法?Java FastMath怎么用?Java FastMath使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
FastMath类属于edu.jhu.prim.util.math包,在下文中一共展示了FastMath类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: klDivergence
import edu.jhu.prim.util.math.FastMath; //导入依赖的package包/类
/**
* Gets the KL divergence between two multinomial distributions p and q.
*
* @param p Array representing a multinomial distribution, p.
* @param q Array representing a multinomial distribution, q.
* @return KL(p || q)
*/
public static double klDivergence(double[] p, double[] q) {
if (p.length != q.length) {
throw new IllegalStateException("The length of p and q must be the same.");
}
double delta = 1e-8;
if (!Multinomials.isMultinomial(p, delta)) {
throw new IllegalStateException("p is not a multinomial");
}
if (!Multinomials.isMultinomial(q, delta)) {
throw new IllegalStateException("q is not a multinomial");
}
double kl = 0.0;
for (int i=0; i<p.length; i++) {
if (p[i] == 0.0 || q[i] == 0.0) {
continue;
}
kl += p[i] * FastMath.log(p[i] / q[i]);
}
return kl;
}
示例2: safeLogSubtract
import edu.jhu.prim.util.math.FastMath; //导入依赖的package包/类
private double safeLogSubtract(double partition, double beliefTrue) {
double outMsgFalse;
if (partition < beliefTrue) {
// This will happen very frequently if the log-add table is used
// instead of "exact" log-add.
if (log.isTraceEnabled()) {
log.trace(String.format("Partition function less than belief: partition=%.20f belief=%.20f", partition, beliefTrue));
}
// To get around the floating point error, we truncate the
// subtraction to log(0).
outMsgFalse = Double.NEGATIVE_INFINITY;
unsafeLogSubtracts++;
} else {
outMsgFalse = FastMath.logSubtractExact(partition, beliefTrue);
}
logSubtractCount++;
return outMsgFalse;
}
示例3: updateCell
import edu.jhu.prim.util.math.FastMath; //导入依赖的package包/类
public void updateCell(int nt, double score, int mid, Rule r) {
assert(!isClosed);
if (bps[nt] == null) {
// If the non-terminal hasn't been added yet, include it in the set of non terminals.
nts.add(nt);
}
if (computeInside) {
// Compute the inside score.
scores[nt] = FastMath.logAdd(scores[nt], score);
// Add a dummy backpointer, so that the above non-null check still works.
bps[nt] = BackPointer.NON_NULL_BACKPOINTER;
} else {
// Compute the viterbi score.
if (score > scores[nt]) {
scores[nt] = score;
bps[nt] = new BackPointer(r, mid);
}
}
}
示例4: updateCell
import edu.jhu.prim.util.math.FastMath; //导入依赖的package包/类
public final void updateCell(int s, int t, int d, int ic, double score, int r) {
int idx = getIndex(s, t, d, ic);
if (this.type == DepParseType.VITERBI) {
if (score > scores[idx]) {
scores[idx] = score;
bps[idx] = r;
}
} else {
scores[idx] = FastMath.logAdd(scores[idx], score);
// Don't update the backpointer.
// Commented out for speed.
// log.debug(String.format("Cell: s=%d (r=%d) t=%d d=%s ic=%s score=%10.2f exp(score)=%.2f",
// s, r, t,
// d == ProjectiveDependencyParser.RIGHT ? "R" : "L",
// ic == ProjectiveDependencyParser.COMPLETE ? "C" : "I",
// scores[s][t][d][ic],
// FastMath.exp(scores[s][t][d][ic])));
}
}
示例5: testSimple
import edu.jhu.prim.util.math.FastMath; //导入依赖的package包/类
@Test
public void testSimple() {
Tensor t1 = TensorUtils.getVectorFromValues(s, 2, 3, 5);
Tensor expOut = TensorUtils.getVectorFromValues(s, FastMath.log(2.), FastMath.log(3.), FastMath.log(5.));
Tensor expT1Adj = TensorUtils.getVectorFromValues(s,
2.2 / 2.,
2.2 / 3.,
2.2 / 5.);
Tensor1Factory fact = new Tensor1Factory() {
public Module<Tensor> getModule(Module<Tensor> m1) {
return new Log(m1);
}
};
AbstractModuleTest.evalTensor1(t1, expT1Adj, fact, expOut, 2.2);
}
示例6: testSimple
import edu.jhu.prim.util.math.FastMath; //导入依赖的package包/类
@Test
public void testSimple() {
Tensor t1 = TensorUtils.getVectorFromValues(s, 2, 3, 5);
Tensor expOut = TensorUtils.getVectorFromValues(s, FastMath.exp(2.), FastMath.exp(3.), FastMath.exp(5.));
Tensor expT1Adj = TensorUtils.getVectorFromValues(s,
2.2 * FastMath.exp(2.),
2.2 * FastMath.exp(3.),
2.2 * FastMath.exp(5.));
Tensor1Factory fact = new Tensor1Factory() {
public Module<Tensor> getModule(Module<Tensor> m1) {
return new Exp(m1);
}
};
AbstractModuleTest.evalTensor1(t1, expT1Adj, fact, expOut, 2.2);
}
示例7: testInfOnLinearChainGraph
import edu.jhu.prim.util.math.FastMath; //导入依赖的package包/类
public static void testInfOnLinearChainGraph(FactorGraph fg,
FgInferencer bp) {
bp.run();
VarTensor marg;
double[] goldMarg;
marg = bp.getMarginals(fg.getVar(0));
goldMarg = new double[] { 0.079, 0.920 };
JUnitUtils.assertArrayEquals(goldMarg, marg.getValues(), 1e-2);
marg = bp.getLogMarginals(fg.getVar(0));
goldMarg = DoubleArrays.getLog(goldMarg);
JUnitUtils.assertArrayEquals(goldMarg, marg.getValues(), 1e-2);
marg = bp.getMarginals(fg.getFactor(3));
goldMarg = new double[] { 0.013146806000337095, 0.06607112759143771, 0.1774818810045508, 0.7433001854036744 };
JUnitUtils.assertArrayEquals(goldMarg, marg.getValues(), 1e-4);
marg = bp.getLogMarginals(fg.getFactor(3));
goldMarg = DoubleArrays.getLog(goldMarg);
JUnitUtils.assertArrayEquals(goldMarg, marg.getValues(), 1e-4);
double goldPartition = 0.5932;
assertEquals(goldPartition, bp.getPartition(), 1e-2);
assertEquals(FastMath.log(goldPartition), bp.getLogPartition(), 1e-2);
}
示例8: updateFactorGraphFromModel
import edu.jhu.prim.util.math.FastMath; //导入依赖的package包/类
/** Assumes that theta is in the real semiring. */
private static void updateFactorGraphFromModel(IntDoubleVector theta, FactorGraph fg) {
// Update factors from the model in params array.
int i=0;
for (Factor f : fg.getFactors()) {
if (isGlobalFactor(f)) {
System.out.println("Skipping factor: " + f);
continue;
}
ExplicitFactor factor = (ExplicitFactor) f;
for (int c=0; c<factor.size(); c++) {
double logValue = FastMath.log(theta.get(i++));
// IMPORTANT NOTE: we set the factor to be the log of the score.
factor.setValue(c, logValue);
}
}
}
示例9: addFeat
import edu.jhu.prim.util.math.FastMath; //导入依赖的package包/类
private static void addFeat(FeatureVector feats, int mod, long feat) {
int hash = MurmurHash.hash32(feat);
if (mod > 0) {
hash = FastMath.mod(hash, mod);
}
feats.add(hash, 1.0);
// Enable this for debugging of feature creation.
// if (feats instanceof LongFeatureVector) {
// ((LongFeatureVector)feats).addLong(feat, 1.0);
// }
}
示例10: getValIdx
import edu.jhu.prim.util.math.FastMath; //导入依赖的package包/类
public int getValIdx(AnnoSentence sent, int pidx, int cidx) {
String val = getVal(sent, pidx, cidx);
if (val == null) {
return -1;
} else {
String data = val;
if (valueHashMod > 0) {
int idx = FastMath.mod(MurmurHash3.murmurhash3_x86_32(data, 0, data.length(), 123456789), valueHashMod);
return valAlphabet.lookupIndex(idx);
} else {
return valAlphabet.lookupIndex(val);
}
}
}
示例11: addFeat
import edu.jhu.prim.util.math.FastMath; //导入依赖的package包/类
public static void addFeat(FeatureVector feats, int mod, long feat, double value) {
int hash = MurmurHash.hash32(feat);
if (mod > 0) {
hash = FastMath.mod(hash, mod);
}
feats.add(hash, value);
}
示例12: testInsideOutsideSpeed
import edu.jhu.prim.util.math.FastMath; //导入依赖的package包/类
/**
* Output:
* SEED=123456789101112
* 100 trials: Tokens per second: 2700.2700270027003
* 1000 trials: Tokens per second: 5395.6834532374105
*
* After switching to setTailNodes() with Hypernode[] storage.
* 100 trials: Tokens per second: 2929.6875
* 1000 trials: Tokens per second: 6651.884700665189
* 1000 trials (with LogPosNegSemiring): Tokens per second: 4687.5
*/
@Test
public void testInsideOutsideSpeed() {
FastMath.useLogAddTable = true;
int trials = 1000;
int n = 30;
// Just create one tree.
double[] root = Multinomials.randomMultinomial(n);
double[][] child = new double[n][];
for (int i=0; i<n; i++) {
child[i] = Multinomials.randomMultinomial(n);
}
Timer timer = new Timer();
timer.start();
for (int t=0; t<trials; t++) {
HyperDepParser.insideOutsideSingleRoot(root, child);
}
timer.stop();
System.out.println("Total time: " + timer.totMs());
int numSents = trials;
int numTokens = n * numSents;
System.out.println("Sentences per second: " + numSents / timer.totSec());
System.out.println("Tokens per second: " + numTokens / timer.totSec());
FastMath.useLogAddTable = false;
}
示例13: testInsideLogViterbiSpeed
import edu.jhu.prim.util.math.FastMath; //导入依赖的package包/类
/**
* Output:
* SEED=123456789101112
* 1000 trials: Tokens per second: 12494.793835901708
*/
@Test
public void testInsideLogViterbiSpeed() {
FastMath.useLogAddTable = true;
int trials = 1000;
int n = 30;
// Just create one tree.
double[] root = Multinomials.randomMultinomial(n);
double[][] child = new double[n][];
for (int i=0; i<n; i++) {
child[i] = Multinomials.randomMultinomial(n);
}
for (int round=0; round<2; round++) {
Semiring s = LogViterbiSemiring.getInstance();
Timer timer = new Timer();
timer.start();
for (int t=0; t<trials; t++) {
O1DpHypergraph graph = new O1DpHypergraph(root, child, s, true);
Scores scores = new Scores();
Hyperalgo.insideAlgorithm(graph, graph.getPotentials(), s, scores);
}
timer.stop();
System.out.println("Total time: " + timer.totMs());
int numSents = trials;
int numTokens = n * numSents;
System.out.println("Sentences per second: " + numSents / timer.totSec());
System.out.println("Tokens per second: " + numTokens / timer.totSec());
}
FastMath.useLogAddTable = false;
}
示例14: getFeatures
import edu.jhu.prim.util.math.FastMath; //导入依赖的package包/类
protected static void getFeatures(List<FeatTemplate> tpls, TemplateFeatureExtractor extStr, IntTemplateFeatureExtractor extInt,
LocalObservations local) {
for (FeatTemplate tpl : tpls) {
List<String> featsStr = new ArrayList<>();
extStr.addFeatures(tpl, local, featsStr);
IntArrayList featsInt = new IntArrayList();
extInt.addFeatures(tpl, local, featsInt);
if (featsStr.size() != featsInt.size()) {
log.error("Mismatch in number of features extracted for template: {} str = {} int = {}", tpl.getName(), featsStr.size(), featsInt.size());
}
for (int i=0; i<Math.min(featsStr.size(), featsInt.size()); i++) {
int hash;
if (useIntExtr) {
hash = featsInt.get(i);
} else {
hash = MurmurHash.hash32(featsStr.get(i));
}
hash = FastMath.mod(hash, featureHashMod);
// Add to the set of strings which are colliding on this hash.
Set<String> values = collisions[hash];
if (values == null) {
values = new HashSet<String>();
collisions[hash] = values;
}
values.add(featsStr.get(i));
// Increment the token count of collisions on this hash.
collisionTokenCount[hash] += 1;
}
}
}
示例15: addFakeBrownClusters
import edu.jhu.prim.util.math.FastMath; //导入依赖的package包/类
public static void addFakeBrownClusters(AnnoSentence sent) {
ArrayList<String> clusters = new ArrayList<String>();
for (int i=0; i<sent.size(); i++) {
clusters.add(FastMath.mod(i*7, 2) + "10101" + FastMath.mod(i*39, 2));
}
sent.setClusters(clusters);
}