use of org.ejml.simple.SimpleMatrix in project CoreNLP by stanfordnlp.
the class ConvertMatlabModel method copyWordVector.
/** Will not overwrite an existing word vector if it is already there */
public static void copyWordVector(Map<String, SimpleMatrix> wordVectors, String source, String target) {
if (wordVectors.containsKey(target) || !wordVectors.containsKey(source)) {
return;
}
log.info("Using wordVector " + source + " for " + target);
wordVectors.put(target, new SimpleMatrix(wordVectors.get(source)));
}
use of org.ejml.simple.SimpleMatrix in project CoreNLP by stanfordnlp.
the class SentimentCostAndGradient method getTensorGradient.
private static SimpleTensor getTensorGradient(SimpleMatrix deltaFull, SimpleMatrix leftVector, SimpleMatrix rightVector) {
int size = deltaFull.getNumElements();
SimpleTensor Wt_df = new SimpleTensor(size * 2, size * 2, size);
// TODO: combine this concatenation with computeTensorDeltaDown?
SimpleMatrix fullVector = NeuralUtils.concatenate(leftVector, rightVector);
for (int slice = 0; slice < size; ++slice) {
Wt_df.setSlice(slice, fullVector.scale(deltaFull.get(slice)).mult(fullVector.transpose()));
}
return Wt_df;
}
use of org.ejml.simple.SimpleMatrix in project CoreNLP by stanfordnlp.
the class AverageDVModels method main.
/**
* Command line arguments for this program:
* <br>
* -output: the model file to output
* -input: a list of model files to input
*/
public static void main(String[] args) {
String outputModelFilename = null;
List<String> inputModelFilenames = Generics.newArrayList();
for (int argIndex = 0; argIndex < args.length; ) {
if (args[argIndex].equalsIgnoreCase("-output")) {
outputModelFilename = args[argIndex + 1];
argIndex += 2;
} else if (args[argIndex].equalsIgnoreCase("-input")) {
for (++argIndex; argIndex < args.length && !args[argIndex].startsWith("-"); ++argIndex) {
inputModelFilenames.addAll(Arrays.asList(args[argIndex].split(",")));
}
} else {
throw new RuntimeException("Unknown argument " + args[argIndex]);
}
}
if (outputModelFilename == null) {
log.info("Need to specify output model name with -output");
System.exit(2);
}
if (inputModelFilenames.size() == 0) {
log.info("Need to specify input model names with -input");
System.exit(2);
}
log.info("Averaging " + inputModelFilenames);
log.info("Outputting result to " + outputModelFilename);
LexicalizedParser lexparser = null;
List<DVModel> models = Generics.newArrayList();
for (String filename : inputModelFilenames) {
LexicalizedParser parser = LexicalizedParser.loadModel(filename);
if (lexparser == null) {
lexparser = parser;
}
models.add(DVParser.getModelFromLexicalizedParser(parser));
}
List<TwoDimensionalMap<String, String, SimpleMatrix>> binaryTransformMaps = CollectionUtils.transformAsList(models, model -> model.binaryTransform);
List<TwoDimensionalMap<String, String, SimpleMatrix>> binaryScoreMaps = CollectionUtils.transformAsList(models, model -> model.binaryScore);
List<Map<String, SimpleMatrix>> unaryTransformMaps = CollectionUtils.transformAsList(models, model -> model.unaryTransform);
List<Map<String, SimpleMatrix>> unaryScoreMaps = CollectionUtils.transformAsList(models, model -> model.unaryScore);
List<Map<String, SimpleMatrix>> wordMaps = CollectionUtils.transformAsList(models, model -> model.wordVectors);
TwoDimensionalMap<String, String, SimpleMatrix> binaryTransformAverages = averageBinaryMatrices(binaryTransformMaps);
TwoDimensionalMap<String, String, SimpleMatrix> binaryScoreAverages = averageBinaryMatrices(binaryScoreMaps);
Map<String, SimpleMatrix> unaryTransformAverages = averageUnaryMatrices(unaryTransformMaps);
Map<String, SimpleMatrix> unaryScoreAverages = averageUnaryMatrices(unaryScoreMaps);
Map<String, SimpleMatrix> wordAverages = averageUnaryMatrices(wordMaps);
DVModel newModel = new DVModel(binaryTransformAverages, unaryTransformAverages, binaryScoreAverages, unaryScoreAverages, wordAverages, lexparser.getOp());
DVParser newParser = new DVParser(newModel, lexparser);
newParser.saveModel(outputModelFilename);
}
use of org.ejml.simple.SimpleMatrix in project CoreNLP by stanfordnlp.
the class AverageDVModels method averageBinaryMatrices.
public static TwoDimensionalMap<String, String, SimpleMatrix> averageBinaryMatrices(List<TwoDimensionalMap<String, String, SimpleMatrix>> maps) {
TwoDimensionalMap<String, String, SimpleMatrix> averages = TwoDimensionalMap.treeMap();
for (Pair<String, String> binary : getBinaryMatrixNames(maps)) {
int count = 0;
SimpleMatrix matrix = null;
for (TwoDimensionalMap<String, String, SimpleMatrix> map : maps) {
if (!map.contains(binary.first(), binary.second())) {
continue;
}
SimpleMatrix original = map.get(binary.first(), binary.second());
++count;
if (matrix == null) {
matrix = original;
} else {
matrix = matrix.plus(original);
}
}
matrix = matrix.divide(count);
averages.put(binary.first(), binary.second(), matrix);
}
return averages;
}
use of org.ejml.simple.SimpleMatrix in project CoreNLP by stanfordnlp.
the class DVModel method addRandomBinaryMatrix.
public void addRandomBinaryMatrix(String leftBasic, String rightBasic) {
if (binaryTransform.get(leftBasic, rightBasic) != null) {
return;
}
++numBinaryMatrices;
// scoring matrix
SimpleMatrix score = SimpleMatrix.random(1, numCols, -1.0 / Math.sqrt((double) numCols), 1.0 / Math.sqrt((double) numCols), rand);
binaryScore.put(leftBasic, rightBasic, score.scale(op.trainOptions.scalingForInit));
SimpleMatrix binary;
if (op.trainOptions.useContextWords) {
binary = new SimpleMatrix(numRows, numCols * 4 + 1);
// leave room for bias term
binary.insertIntoThis(0, numCols * 2 + 1, randomContextMatrix());
} else {
binary = new SimpleMatrix(numRows, numCols * 2 + 1);
}
SimpleMatrix left = randomTransformMatrix();
SimpleMatrix right = randomTransformMatrix();
binary.insertIntoThis(0, 0, left);
binary.insertIntoThis(0, numCols, right);
binaryTransform.put(leftBasic, rightBasic, binary.scale(op.trainOptions.scalingForInit));
}
Aggregations