use of org.ejml.simple.SimpleMatrix in project CoreNLP by stanfordnlp.
the class NeuralUtils method vectorToParams.
/**
* Given a sequence of Iterators over SimpleMatrix, fill in all of
* the matrices with the entries in the theta vector. Errors are
* thrown if the theta vector does not exactly fill the matrices.
*/
@SafeVarargs
public static void vectorToParams(double[] theta, Iterator<SimpleMatrix>... matrices) {
int index = 0;
for (Iterator<SimpleMatrix> matrixIterator : matrices) {
while (matrixIterator.hasNext()) {
SimpleMatrix matrix = matrixIterator.next();
int numElements = matrix.getNumElements();
for (int i = 0; i < numElements; ++i) {
matrix.set(i, theta[index]);
++index;
}
}
}
if (index != theta.length) {
throw new AssertionError("Did not entirely use the theta vector");
}
}
use of org.ejml.simple.SimpleMatrix in project CoreNLP by stanfordnlp.
the class SimpleTensor method bilinearProducts.
/**
* Returns a column vector where each entry is the nth bilinear
* product of the nth slices of the two tensors.
*/
public SimpleMatrix bilinearProducts(SimpleMatrix in) {
if (in.numCols() != 1) {
throw new AssertionError("Expected a column vector");
}
if (in.numRows() != numCols) {
throw new AssertionError("Number of rows in the input does not match number of columns in tensor");
}
if (numRows != numCols) {
throw new AssertionError("Can only perform this operation on a SimpleTensor with square slices");
}
SimpleMatrix inT = in.transpose();
SimpleMatrix out = new SimpleMatrix(numSlices, 1);
for (int slice = 0; slice < numSlices; ++slice) {
double result = inT.mult(slices[slice]).mult(in).get(0);
out.set(slice, result);
}
return out;
}
use of org.ejml.simple.SimpleMatrix in project CoreNLP by stanfordnlp.
the class DVParserCostAndGradient method backpropDerivative.
public void backpropDerivative(Tree tree, List<String> words, IdentityHashMap<Tree, SimpleMatrix> nodeVectors, TwoDimensionalMap<String, String, SimpleMatrix> binaryW_dfs, Map<String, SimpleMatrix> unaryW_dfs, TwoDimensionalMap<String, String, SimpleMatrix> binaryScoreDerivatives, Map<String, SimpleMatrix> unaryScoreDerivatives, Map<String, SimpleMatrix> wordVectorDerivatives) {
SimpleMatrix delta = new SimpleMatrix(op.lexOptions.numHid, 1);
backpropDerivative(tree, words, nodeVectors, binaryW_dfs, unaryW_dfs, binaryScoreDerivatives, unaryScoreDerivatives, wordVectorDerivatives, delta);
}
use of org.ejml.simple.SimpleMatrix in project CoreNLP by stanfordnlp.
the class DVParserCostAndGradient method calculate.
// fill value & derivative
public void calculate(double[] theta) {
dvModel.vectorToParams(theta);
double localValue = 0.0;
double[] localDerivative = new double[theta.length];
TwoDimensionalMap<String, String, SimpleMatrix> binaryW_dfsG, binaryW_dfsB;
binaryW_dfsG = TwoDimensionalMap.treeMap();
binaryW_dfsB = TwoDimensionalMap.treeMap();
TwoDimensionalMap<String, String, SimpleMatrix> binaryScoreDerivativesG, binaryScoreDerivativesB;
binaryScoreDerivativesG = TwoDimensionalMap.treeMap();
binaryScoreDerivativesB = TwoDimensionalMap.treeMap();
Map<String, SimpleMatrix> unaryW_dfsG, unaryW_dfsB;
unaryW_dfsG = new TreeMap<>();
unaryW_dfsB = new TreeMap<>();
Map<String, SimpleMatrix> unaryScoreDerivativesG, unaryScoreDerivativesB;
unaryScoreDerivativesG = new TreeMap<>();
unaryScoreDerivativesB = new TreeMap<>();
Map<String, SimpleMatrix> wordVectorDerivativesG = new TreeMap<>();
Map<String, SimpleMatrix> wordVectorDerivativesB = new TreeMap<>();
for (TwoDimensionalMap.Entry<String, String, SimpleMatrix> entry : dvModel.binaryTransform) {
int numRows = entry.getValue().numRows();
int numCols = entry.getValue().numCols();
binaryW_dfsG.put(entry.getFirstKey(), entry.getSecondKey(), new SimpleMatrix(numRows, numCols));
binaryW_dfsB.put(entry.getFirstKey(), entry.getSecondKey(), new SimpleMatrix(numRows, numCols));
binaryScoreDerivativesG.put(entry.getFirstKey(), entry.getSecondKey(), new SimpleMatrix(1, numRows));
binaryScoreDerivativesB.put(entry.getFirstKey(), entry.getSecondKey(), new SimpleMatrix(1, numRows));
}
for (Map.Entry<String, SimpleMatrix> entry : dvModel.unaryTransform.entrySet()) {
int numRows = entry.getValue().numRows();
int numCols = entry.getValue().numCols();
unaryW_dfsG.put(entry.getKey(), new SimpleMatrix(numRows, numCols));
unaryW_dfsB.put(entry.getKey(), new SimpleMatrix(numRows, numCols));
unaryScoreDerivativesG.put(entry.getKey(), new SimpleMatrix(1, numRows));
unaryScoreDerivativesB.put(entry.getKey(), new SimpleMatrix(1, numRows));
}
if (op.trainOptions.trainWordVectors) {
for (Map.Entry<String, SimpleMatrix> entry : dvModel.wordVectors.entrySet()) {
int numRows = entry.getValue().numRows();
int numCols = entry.getValue().numCols();
wordVectorDerivativesG.put(entry.getKey(), new SimpleMatrix(numRows, numCols));
wordVectorDerivativesB.put(entry.getKey(), new SimpleMatrix(numRows, numCols));
}
}
// Some optimization methods prints out a line without an end, so our
// debugging statements are misaligned
Timing scoreTiming = new Timing();
scoreTiming.doing("Scoring trees");
int treeNum = 0;
MulticoreWrapper<Tree, Pair<DeepTree, DeepTree>> wrapper = new MulticoreWrapper<>(op.trainOptions.trainingThreads, new ScoringProcessor());
for (Tree tree : trainingBatch) {
wrapper.put(tree);
}
wrapper.join();
scoreTiming.done();
while (wrapper.peek()) {
Pair<DeepTree, DeepTree> result = wrapper.poll();
DeepTree goldTree = result.first;
DeepTree bestTree = result.second;
StringBuilder treeDebugLine = new StringBuilder();
Formatter formatter = new Formatter(treeDebugLine);
boolean isDone = (Math.abs(bestTree.getScore() - goldTree.getScore()) <= 0.00001 || goldTree.getScore() > bestTree.getScore());
String done = isDone ? "done" : "";
formatter.format("Tree %6d Highest tree: %12.4f Correct tree: %12.4f %s", treeNum, bestTree.getScore(), goldTree.getScore(), done);
log.info(treeDebugLine.toString());
if (!isDone) {
// if the gold tree is better than the best hypothesis tree by
// a large enough margin, then the score difference will be 0
// and we ignore the tree
double valueDelta = bestTree.getScore() - goldTree.getScore();
//double valueDelta = Math.max(0.0, - scoreGold + bestScore);
localValue += valueDelta;
// get the context words for this tree - should be the same
// for either goldTree or bestTree
List<String> words = getContextWords(goldTree.getTree());
// The derivatives affected by this tree are only based on the
// nodes present in this tree, eg not all matrix derivatives
// will be affected by this tree
backpropDerivative(goldTree.getTree(), words, goldTree.getVectors(), binaryW_dfsG, unaryW_dfsG, binaryScoreDerivativesG, unaryScoreDerivativesG, wordVectorDerivativesG);
backpropDerivative(bestTree.getTree(), words, bestTree.getVectors(), binaryW_dfsB, unaryW_dfsB, binaryScoreDerivativesB, unaryScoreDerivativesB, wordVectorDerivativesB);
}
++treeNum;
}
double[] localDerivativeGood;
double[] localDerivativeB;
if (op.trainOptions.trainWordVectors) {
localDerivativeGood = NeuralUtils.paramsToVector(theta.length, binaryW_dfsG.valueIterator(), unaryW_dfsG.values().iterator(), binaryScoreDerivativesG.valueIterator(), unaryScoreDerivativesG.values().iterator(), wordVectorDerivativesG.values().iterator());
localDerivativeB = NeuralUtils.paramsToVector(theta.length, binaryW_dfsB.valueIterator(), unaryW_dfsB.values().iterator(), binaryScoreDerivativesB.valueIterator(), unaryScoreDerivativesB.values().iterator(), wordVectorDerivativesB.values().iterator());
} else {
localDerivativeGood = NeuralUtils.paramsToVector(theta.length, binaryW_dfsG.valueIterator(), unaryW_dfsG.values().iterator(), binaryScoreDerivativesG.valueIterator(), unaryScoreDerivativesG.values().iterator());
localDerivativeB = NeuralUtils.paramsToVector(theta.length, binaryW_dfsB.valueIterator(), unaryW_dfsB.values().iterator(), binaryScoreDerivativesB.valueIterator(), unaryScoreDerivativesB.values().iterator());
}
// correct - highest
for (int i = 0; i < localDerivativeGood.length; i++) {
localDerivative[i] = localDerivativeB[i] - localDerivativeGood[i];
}
// TODO: this is where we would combine multiple costs if we had parallelized the calculation
value = localValue;
derivative = localDerivative;
// normalizing by training batch size
value = (1.0 / trainingBatch.size()) * value;
ArrayMath.multiplyInPlace(derivative, (1.0 / trainingBatch.size()));
// add regularization to cost:
double[] currentParams = dvModel.paramsToVector();
double regCost = 0;
for (double currentParam : currentParams) {
regCost += currentParam * currentParam;
}
regCost = op.trainOptions.regCost * 0.5 * regCost;
value += regCost;
// add regularization to gradient
ArrayMath.multiplyInPlace(currentParams, op.trainOptions.regCost);
ArrayMath.pairwiseAddInPlace(derivative, currentParams);
}
use of org.ejml.simple.SimpleMatrix in project CoreNLP by stanfordnlp.
the class DumpMatrices method main.
public static void main(String[] args) throws IOException {
String modelPath = null;
String outputDir = null;
for (int argIndex = 0; argIndex < args.length; ) {
if (args[argIndex].equalsIgnoreCase("-model")) {
modelPath = args[argIndex + 1];
argIndex += 2;
} else if (args[argIndex].equalsIgnoreCase("-output")) {
outputDir = args[argIndex + 1];
argIndex += 2;
} else {
log.info("Unknown argument " + args[argIndex]);
help();
}
}
if (outputDir == null || modelPath == null) {
help();
}
File outputFile = new File(outputDir);
FileSystem.checkNotExistsOrFail(outputFile);
FileSystem.mkdirOrFail(outputFile);
LexicalizedParser parser = LexicalizedParser.loadModel(modelPath);
DVModel model = DVParser.getModelFromLexicalizedParser(parser);
String binaryWDir = outputDir + File.separator + "binaryW";
FileSystem.mkdirOrFail(binaryWDir);
for (TwoDimensionalMap.Entry<String, String, SimpleMatrix> entry : model.binaryTransform) {
String filename = binaryWDir + File.separator + entry.getFirstKey() + "_" + entry.getSecondKey() + ".txt";
dumpMatrix(filename, entry.getValue());
}
String binaryScoreDir = outputDir + File.separator + "binaryScore";
FileSystem.mkdirOrFail(binaryScoreDir);
for (TwoDimensionalMap.Entry<String, String, SimpleMatrix> entry : model.binaryScore) {
String filename = binaryScoreDir + File.separator + entry.getFirstKey() + "_" + entry.getSecondKey() + ".txt";
dumpMatrix(filename, entry.getValue());
}
String unaryWDir = outputDir + File.separator + "unaryW";
FileSystem.mkdirOrFail(unaryWDir);
for (Map.Entry<String, SimpleMatrix> entry : model.unaryTransform.entrySet()) {
String filename = unaryWDir + File.separator + entry.getKey() + ".txt";
dumpMatrix(filename, entry.getValue());
}
String unaryScoreDir = outputDir + File.separator + "unaryScore";
FileSystem.mkdirOrFail(unaryScoreDir);
for (Map.Entry<String, SimpleMatrix> entry : model.unaryScore.entrySet()) {
String filename = unaryScoreDir + File.separator + entry.getKey() + ".txt";
dumpMatrix(filename, entry.getValue());
}
String embeddingFile = outputDir + File.separator + "embeddings.txt";
FileWriter fout = new FileWriter(embeddingFile);
BufferedWriter bout = new BufferedWriter(fout);
for (Map.Entry<String, SimpleMatrix> entry : model.wordVectors.entrySet()) {
bout.write(entry.getKey());
SimpleMatrix vector = entry.getValue();
for (int i = 0; i < vector.numRows(); ++i) {
bout.write(" " + vector.get(i, 0));
}
bout.write("\n");
}
bout.close();
fout.close();
}
Aggregations