use of org.ejml.simple.SimpleMatrix in project CoreNLP by stanfordnlp.
the class AverageDVModels method averageUnaryMatrices.
public static Map<String, SimpleMatrix> averageUnaryMatrices(List<Map<String, SimpleMatrix>> maps) {
Map<String, SimpleMatrix> averages = Generics.newTreeMap();
for (String name : getUnaryMatrixNames(maps)) {
int count = 0;
SimpleMatrix matrix = null;
for (Map<String, SimpleMatrix> map : maps) {
if (!map.containsKey(name)) {
continue;
}
SimpleMatrix original = map.get(name);
++count;
if (matrix == null) {
matrix = original;
} else {
matrix = matrix.plus(original);
}
}
matrix = matrix.divide(count);
averages.put(name, matrix);
}
return averages;
}
use of org.ejml.simple.SimpleMatrix in project CoreNLP by stanfordnlp.
the class NeuralUtils method paramsToVector.
/**
* Given a sequence of iterators over the matrices, builds a vector
* out of those matrices in the order given. Asks for an expected
* total size as a time savings. AssertionError thrown if the
* vector sizes do not exactly match.
*/
@SafeVarargs
public static double[] paramsToVector(int totalSize, Iterator<SimpleMatrix>... matrices) {
double[] theta = new double[totalSize];
int index = 0;
for (Iterator<SimpleMatrix> matrixIterator : matrices) {
while (matrixIterator.hasNext()) {
SimpleMatrix matrix = matrixIterator.next();
int numElements = matrix.getNumElements();
//System.out.println(Integer.toString(numElements)); // to know what matrices are
for (int i = 0; i < numElements; ++i) {
theta[index] = matrix.get(i);
++index;
}
}
}
if (index != totalSize) {
throw new AssertionError("Did not entirely fill the theta vector: expected " + totalSize + " used " + index);
}
return theta;
}
use of org.ejml.simple.SimpleMatrix in project CoreNLP by stanfordnlp.
the class NeuralUtils method softmax.
/**
* Applies softmax to all of the elements of the matrix. The return
* matrix will have all of its elements sum to 1. If your matrix is
* not already a vector, be sure this is what you actually want.
*/
public static SimpleMatrix softmax(SimpleMatrix input) {
SimpleMatrix output = new SimpleMatrix(input);
for (int i = 0; i < output.numRows(); ++i) {
for (int j = 0; j < output.numCols(); ++j) {
output.set(i, j, Math.exp(output.get(i, j)));
}
}
double sum = output.elementSum();
// will be safe, since exp should never return 0
return output.scale(1.0 / sum);
}
use of org.ejml.simple.SimpleMatrix in project CoreNLP by stanfordnlp.
the class NeuralUtils method oneHot.
public static SimpleMatrix oneHot(int index, int size) {
SimpleMatrix m = new SimpleMatrix(size, 1);
m.set(index, 1);
return m;
}
use of org.ejml.simple.SimpleMatrix in project CoreNLP by stanfordnlp.
the class NeuralUtils method paramsToVector.
/**
* Given a sequence of iterators over the matrices, builds a vector
* out of those matrices in the order given. The vector is scaled
* according to the <code>scale</code> parameter. Asks for an
* expected total size as a time savings. AssertionError thrown if
* the vector sizes do not exactly match.
*/
@SafeVarargs
public static double[] paramsToVector(double scale, int totalSize, Iterator<SimpleMatrix>... matrices) {
double[] theta = new double[totalSize];
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) {
theta[index] = matrix.get(i) * scale;
++index;
}
}
}
if (index != totalSize) {
throw new AssertionError("Did not entirely fill the theta vector: expected " + totalSize + " used " + index);
}
return theta;
}
Aggregations