use of org.ejml.simple.SimpleMatrix in project CoreNLP by stanfordnlp.
the class SentimentCostAndGradient method scaleAndRegularize.
private static double scaleAndRegularize(TwoDimensionalMap<String, String, SimpleMatrix> derivatives, TwoDimensionalMap<String, String, SimpleMatrix> currentMatrices, double scale, double regCost, boolean dropBiasColumn) {
// the regularization cost
double cost = 0.0;
for (TwoDimensionalMap.Entry<String, String, SimpleMatrix> entry : currentMatrices) {
SimpleMatrix D = derivatives.get(entry.getFirstKey(), entry.getSecondKey());
SimpleMatrix regMatrix = entry.getValue();
if (dropBiasColumn) {
regMatrix = new SimpleMatrix(regMatrix);
regMatrix.insertIntoThis(0, regMatrix.numCols() - 1, new SimpleMatrix(regMatrix.numRows(), 1));
}
D = D.scale(scale).plus(regMatrix.scale(regCost));
derivatives.put(entry.getFirstKey(), entry.getSecondKey(), D);
cost += regMatrix.elementMult(regMatrix).elementSum() * regCost / 2.0;
}
return cost;
}
use of org.ejml.simple.SimpleMatrix in project CoreNLP by stanfordnlp.
the class SentimentCostAndGradient method computeTensorDeltaDown.
private static SimpleMatrix computeTensorDeltaDown(SimpleMatrix deltaFull, SimpleMatrix leftVector, SimpleMatrix rightVector, SimpleMatrix W, SimpleTensor Wt) {
SimpleMatrix WTDelta = W.transpose().mult(deltaFull);
SimpleMatrix WTDeltaNoBias = WTDelta.extractMatrix(0, deltaFull.numRows() * 2, 0, 1);
int size = deltaFull.getNumElements();
SimpleMatrix deltaTensor = new SimpleMatrix(size * 2, 1);
SimpleMatrix fullVector = NeuralUtils.concatenate(leftVector, rightVector);
for (int slice = 0; slice < size; ++slice) {
SimpleMatrix scaledFullVector = fullVector.scale(deltaFull.get(slice));
deltaTensor = deltaTensor.plus(Wt.getSlice(slice).plus(Wt.getSlice(slice).transpose()).mult(scaledFullVector));
}
return deltaTensor.plus(WTDeltaNoBias);
}
use of org.ejml.simple.SimpleMatrix in project CoreNLP by stanfordnlp.
the class SentimentModel method randomTransformMatrix.
SimpleMatrix randomTransformMatrix() {
SimpleMatrix binary = new SimpleMatrix(numHid, numHid * 2 + 1);
// bias column values are initialized zero
binary.insertIntoThis(0, 0, randomTransformBlock());
binary.insertIntoThis(0, numHid, randomTransformBlock());
return binary.scale(op.trainOptions.scalingForInit);
}
use of org.ejml.simple.SimpleMatrix in project CoreNLP by stanfordnlp.
the class SentimentModel method randomClassificationMatrix.
/**
* Returns matrices of the right size for either binary or unary (terminal) classification
*/
SimpleMatrix randomClassificationMatrix() {
SimpleMatrix score = new SimpleMatrix(numClasses, numHid + 1);
double range = 1.0 / (Math.sqrt((double) numHid));
score.insertIntoThis(0, 0, SimpleMatrix.random(numClasses, numHid, -range, range, rand));
// bias column goes from 0 to 1 initially
score.insertIntoThis(0, numHid, SimpleMatrix.random(numClasses, 1, 0.0, 1.0, rand));
return score.scale(op.trainOptions.scalingForInit);
}
use of org.ejml.simple.SimpleMatrix in project CoreNLP by stanfordnlp.
the class SentimentModel method readWordVectors.
void readWordVectors() {
Embedding embedding = new Embedding(op.wordVectors, op.numHid);
this.wordVectors = Generics.newTreeMap();
// for (String word : rawWordVectors.keySet()) {
for (String word : embedding.keySet()) {
// TODO: factor out unknown word vector code from DVParser
wordVectors.put(word, embedding.get(word));
}
String unkWord = op.unkWord;
SimpleMatrix unknownWordVector = wordVectors.get(unkWord);
wordVectors.put(UNKNOWN_WORD, unknownWordVector);
if (unknownWordVector == null) {
throw new RuntimeException("Unknown word vector not specified in the word vector file");
}
}
Aggregations