use of org.ejml.simple.SimpleMatrix in project CoreNLP by stanfordnlp.
the class NeuralUtils method concatenate.
/**
* Concatenates several column vectors into one large column vector
*/
public static SimpleMatrix concatenate(SimpleMatrix... vectors) {
int size = 0;
for (SimpleMatrix vector : vectors) {
size += vector.numRows();
}
SimpleMatrix result = new SimpleMatrix(size, 1);
int index = 0;
for (SimpleMatrix vector : vectors) {
result.insertIntoThis(index, 0, vector);
index += vector.numRows();
}
return result;
}
use of org.ejml.simple.SimpleMatrix in project CoreNLP by stanfordnlp.
the class NeuralUtils method concatenateWithBias.
/**
* Concatenates several column vectors into one large column
* vector, adds a 1.0 at the end as a bias term
*/
public static SimpleMatrix concatenateWithBias(SimpleMatrix... vectors) {
int size = 0;
for (SimpleMatrix vector : vectors) {
size += vector.numRows();
}
// one extra for the bias
size++;
SimpleMatrix result = new SimpleMatrix(size, 1);
int index = 0;
for (SimpleMatrix vector : vectors) {
result.insertIntoThis(index, 0, vector);
index += vector.numRows();
}
result.set(index, 0, 1.0);
return result;
}
use of org.ejml.simple.SimpleMatrix in project CoreNLP by stanfordnlp.
the class SentimentModel method initRandomWordVectors.
void initRandomWordVectors(List<Tree> trainingTrees) {
if (op.numHid == 0) {
throw new RuntimeException("Cannot create random word vectors for an unknown numHid");
}
Set<String> words = Generics.newHashSet();
words.add(UNKNOWN_WORD);
for (Tree tree : trainingTrees) {
List<Tree> leaves = tree.getLeaves();
for (Tree leaf : leaves) {
String word = leaf.label().value();
if (op.lowercaseWordVectors) {
word = word.toLowerCase();
}
words.add(word);
}
}
this.wordVectors = Generics.newTreeMap();
for (String word : words) {
SimpleMatrix vector = randomWordVector();
wordVectors.put(word, vector);
}
}
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;
}
Aggregations