Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
manning authored and Stanford NLP committed Sep 28, 2015
1 parent 323404a commit 6c3d560
Show file tree
Hide file tree
Showing 27 changed files with 1,943 additions and 303,750 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ task listDeps << {


dependencies { dependencies {
compile fileTree(dir: 'lib', include: '*.jar') compile fileTree(dir: 'lib', include: '*.jar')
testCompile fileTree(dir: 'liblocal', include: '*.jar')
} }


// Eclipse plugin setup // Eclipse plugin setup
Expand Down
4 changes: 4 additions & 0 deletions build.xml
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@
<include name="*.jar"/> <include name="*.jar"/>
<exclude name="javanlp*"/> <exclude name="javanlp*"/>
</fileset> </fileset>
<fileset dir="${basedir}/liblocal">
<include name="*.jar"/>
<exclude name="javanlp*"/>
</fileset>
</path> </path>
</target> </target>


Expand Down
14 changes: 0 additions & 14 deletions lib/README
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -128,20 +128,6 @@ Not needed by CoreNLP distributions. In core, used only by web apps (jsp pages
LAST UPDATE: 2013-06-05 LAST UPDATE: 2013-06-05
LAST UPDATE BY: Sonal Gupta LAST UPDATE BY: Sonal Gupta


-----------------------------------------------------------------------
commons-math3.jar
ORIGINAL JAR NAME: commons-math3-3.5.jar
VERSION: 3.5
RELEASE DATE: 3-14-2015
SOURCE AVAILABLE: yes
DESCRIPTION: self contained fast math routines
URL: http://commons.apache.org/lang/

USED BY: edu.stanford.nlp.loglinear

LAST UPDATE: 2015-9-25
LAST UPDATE BY: Keenon Werling

----------------------------------------------------------------------- -----------------------------------------------------------------------
commons-logging.jar commons-logging.jar


Expand Down
Binary file removed lib/antlr-runtime-3.1.2.jar
Binary file not shown.
Binary file removed lib/commons-math3.jar
Binary file not shown.
Binary file removed lib/hamcrest-core-1.3.jar
Binary file not shown.
Binary file removed lib/javaruntype-1.2.jar
Binary file not shown.
Binary file removed lib/junit-quickcheck-core-0.4-beta-3.jar
Binary file not shown.
Binary file removed lib/junit-quickcheck-generators-0.4-beta-3.jar
Binary file not shown.
Binary file removed lib/junit-theories-4.12.jar
Binary file not shown.
Binary file removed lib/ognl-3.0.5.jar
Binary file not shown.
35 changes: 17 additions & 18 deletions src/edu/stanford/nlp/loglinear/inference/TableFactor.java
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import edu.stanford.nlp.loglinear.model.ConcatVector; import edu.stanford.nlp.loglinear.model.ConcatVector;
import edu.stanford.nlp.loglinear.model.GraphicalModel; import edu.stanford.nlp.loglinear.model.GraphicalModel;
import edu.stanford.nlp.loglinear.model.NDArrayDoubles; import edu.stanford.nlp.loglinear.model.NDArrayDoubles;
import org.apache.commons.math3.util.FastMath;


import java.util.ArrayList; import java.util.ArrayList;
import java.util.Iterator; import java.util.Iterator;
Expand Down Expand Up @@ -173,7 +172,7 @@ public double[][] getSummedMarginals() {
while (true) { while (true) {
double v = getAssignmentLogValue(assignment); double v = getAssignmentLogValue(assignment);
for (int i = 0; i < neighborIndices.length; i++) { for (int i = 0; i < neighborIndices.length; i++) {
results[i][assignment[i]] += FastMath.exp(v - maxValues[i][assignment[i]]); results[i][assignment[i]] += Math.exp(v - maxValues[i][assignment[i]]);
} }
// This mutates the resultAssignment[] array, rather than creating a new one // This mutates the resultAssignment[] array, rather than creating a new one
if (secondFastPassByReferenceIterator.hasNext()) { if (secondFastPassByReferenceIterator.hasNext()) {
Expand All @@ -187,7 +186,7 @@ public double[][] getSummedMarginals() {
for (int i = 0; i < neighborIndices.length; i++) { for (int i = 0; i < neighborIndices.length; i++) {
double sum = 0.0; double sum = 0.0;
for (int j = 0; j < results[i].length; j++) { for (int j = 0; j < results[i].length; j++) {
results[i][j] = FastMath.exp(maxValues[i][j]) * results[i][j]; results[i][j] = Math.exp(maxValues[i][j]) * results[i][j];
sum += results[i][j]; sum += results[i][j];
} }
if (Double.isInfinite(sum)) { if (Double.isInfinite(sum)) {
Expand Down Expand Up @@ -296,7 +295,7 @@ public TableFactor sumOut(int variable) {
for (int j = 0; j < getDimensions()[1]; j++) { for (int j = 0; j < getDimensions()[1]; j++) {
int index = k + j; int index = k + j;
if (Double.isFinite(max[j])) { if (Double.isFinite(max[j])) {
marginalized.values[j] += FastMath.exp(values[index] - max[j]); marginalized.values[j] += Math.exp(values[index] - max[j]);
} }
} }
} }
Expand All @@ -305,7 +304,7 @@ public TableFactor sumOut(int variable) {


for (int j = 0; j < getDimensions()[1]; j++) { for (int j = 0; j < getDimensions()[1]; j++) {
if (Double.isFinite(max[j])) { if (Double.isFinite(max[j])) {
marginalized.values[j] = max[j] + FastMath.log(marginalized.values[j]); marginalized.values[j] = max[j] + Math.log(marginalized.values[j]);
} }
else { else {
marginalized.values[j] = max[j]; marginalized.values[j] = max[j];
Expand Down Expand Up @@ -344,7 +343,7 @@ public TableFactor sumOut(int variable) {
for (int j = 0; j < getDimensions()[1]; j++) { for (int j = 0; j < getDimensions()[1]; j++) {
int index = k + j; int index = k + j;
if (Double.isFinite(max[i])) { if (Double.isFinite(max[i])) {
marginalized.values[i] += FastMath.exp(values[index] - max[i]); marginalized.values[i] += Math.exp(values[index] - max[i]);
} }
} }
} }
Expand All @@ -353,7 +352,7 @@ public TableFactor sumOut(int variable) {


for (int i = 0; i < getDimensions()[0]; i++) { for (int i = 0; i < getDimensions()[0]; i++) {
if (Double.isFinite(max[i])) { if (Double.isFinite(max[i])) {
marginalized.values[i] = max[i] + FastMath.log(marginalized.values[i]); marginalized.values[i] = max[i] + Math.log(marginalized.values[i]);
} }
else { else {
marginalized.values[i] = max[i]; marginalized.values[i] = max[i];
Expand All @@ -371,11 +370,11 @@ public TableFactor sumOut(int variable) {
TableFactor maxValues = maxOut(variable); TableFactor maxValues = maxOut(variable);


// Then we do the sum against an offset from the pivots // Then we do the sum against an offset from the pivots
TableFactor marginalized = marginalize(variable, 0, (marginalizedVariableValue, assignment) -> (a, b) -> a + FastMath.exp(b - maxValues.getAssignmentLogValue(assignment))); TableFactor marginalized = marginalize(variable, 0, (marginalizedVariableValue, assignment) -> (a, b) -> a + Math.exp(b - maxValues.getAssignmentLogValue(assignment)));


// Then we factor the max values back in, and // Then we factor the max values back in, and
for (int[] assignment : marginalized) { for (int[] assignment : marginalized) {
marginalized.setAssignmentLogValue(assignment, maxValues.getAssignmentLogValue(assignment) + FastMath.log(marginalized.getAssignmentLogValue(assignment))); marginalized.setAssignmentLogValue(assignment, maxValues.getAssignmentLogValue(assignment) + Math.log(marginalized.getAssignmentLogValue(assignment)));
} }


return marginalized; return marginalized;
Expand Down Expand Up @@ -510,14 +509,14 @@ public double valueSum() {


double sumExp = 0.0; double sumExp = 0.0;
for (int[] assignment : this) { for (int[] assignment : this) {
sumExp += FastMath.exp(getAssignmentLogValue(assignment) - max); sumExp += Math.exp(getAssignmentLogValue(assignment) - max);
} }


return sumExp * FastMath.exp(max); return sumExp * Math.exp(max);
} }


/** /**
* Just a pass through to the NDArray version, plus a FastMath.exp to ensure that to the outside world the TableFactor * Just a pass through to the NDArray version, plus a Math.exp to ensure that to the outside world the TableFactor
* doesn't look like it's in log-space * doesn't look like it's in log-space
* *
* @param assignment a list of variable settings, in the same order as the neighbors array of the factor * @param assignment a list of variable settings, in the same order as the neighbors array of the factor
Expand All @@ -527,19 +526,19 @@ public double valueSum() {
public double getAssignmentValue(int[] assignment) { public double getAssignmentValue(int[] assignment) {
double d = super.getAssignmentValue(assignment); double d = super.getAssignmentValue(assignment);
// if (d == null) d = Double.NEGATIVE_INFINITY; // if (d == null) d = Double.NEGATIVE_INFINITY;
return FastMath.exp(d); return Math.exp(d);
} }


/** /**
* Just a pass through to the NDArray version, plus a FastMath.log to ensure that to the outside world the TableFactor * Just a pass through to the NDArray version, plus a Math.log to ensure that to the outside world the TableFactor
* doesn't look like it's in log-space * doesn't look like it's in log-space
* *
* @param assignment a list of variable settings, in the same order as the neighbors array of the factor * @param assignment a list of variable settings, in the same order as the neighbors array of the factor
* @param value the value to put into the factor table * @param value the value to put into the factor table
*/ */
@Override @Override
public void setAssignmentValue(int[] assignment, double value) { public void setAssignmentValue(int[] assignment, double value) {
super.setAssignmentValue(assignment, FastMath.log(value)); super.setAssignmentValue(assignment, Math.log(value));
} }


//////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -652,9 +651,9 @@ private void normalizeLogArr(double[] arr) {
} }
double expSum = 0.0; double expSum = 0.0;
for (double d : arr) { for (double d : arr) {
expSum += FastMath.exp(d-max); expSum += Math.exp(d-max);
} }
double logSumExp = max + FastMath.log(expSum); double logSumExp = max + Math.log(expSum);


if (Double.isInfinite(logSumExp)) { if (Double.isInfinite(logSumExp)) {
// Just put in uniform probabilities if we are normalizing all 0s // Just put in uniform probabilities if we are normalizing all 0s
Expand All @@ -665,7 +664,7 @@ private void normalizeLogArr(double[] arr) {
else { else {
// Normalize in log-scale before exponentiation, to help with stability // Normalize in log-scale before exponentiation, to help with stability
for (int i = 0; i < arr.length; i++) { for (int i = 0; i < arr.length; i++) {
arr[i] = FastMath.exp(arr[i] - logSumExp); arr[i] = Math.exp(arr[i] - logSumExp);
} }
} }
} }
Expand Down
Loading

0 comments on commit 6c3d560

Please sign in to comment.