public void set(double value) { for (int slice = 0; slice < numSlices; ++slice) { slices[slice].set(value); } }
private static SimpleMatrix encodeDistance(int d) { SimpleMatrix m = new SimpleMatrix(11, 1); if (d < 5) { m.set(d, 1); } else if (d < 8) { m.set(5, 1); } else if (d < 16) { m.set(6, 1); } else if (d < 32) { m.set(7, 1); } else if (d < 64) { m.set(8, 1); } else { m.set(9, 1); } m.set(10, Math.min(d, 64) / 64.0); return m; }
public static SimpleMatrix oneHot(int index, int size) { SimpleMatrix m = new SimpleMatrix(size, 1); m.set(index, 1); return m; }
/** * Returns a vector with random Gaussian values, mean 0, std 1 */ public static SimpleMatrix randomGaussian(int numRows, int numCols, Random rand) { SimpleMatrix result = new SimpleMatrix(numRows, numCols); for (int i = 0; i < numRows; ++i) { for (int j = 0; j < numCols; ++j) { result.set(i, j, rand.nextGaussian()); } } return result; }
/** * 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"); } }
/** * Applies ReLU to each of the entries in the matrix. Returns a new matrix. */ public static SimpleMatrix elementwiseApplyReLU(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.max(0, output.get(i, j))); } } return output; }
/** * Applies log to each of the entries in the matrix. Returns a new matrix. */ public static SimpleMatrix elementwiseApplyLog(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.log(output.get(i, j))); } } return output; }
public SimpleMatrix getPairFeatures(Pair<Integer, Integer> pair, Document document, Map<Integer, List<Mention>> mentionsByHeadIndex) { Mention m1 = document.predictedMentionsByID.get(pair.first); Mention m2 = document.predictedMentionsByID.get(pair.second); List<Integer> featureVals = pairwiseFeatures(document, m1, m2, dictionaries, conll); SimpleMatrix features = new SimpleMatrix(featureVals.size(), 1); for (int i = 0; i < featureVals.size(); i++) { features.set(i, featureVals.get(i)); } features = NeuralUtils.concatenate(features, encodeDistance(m2.sentNum - m1.sentNum), encodeDistance(m2.mentionNum - m1.mentionNum - 1), new SimpleMatrix(new double[][] {{ m1.sentNum == m2.sentNum && m1.endIndex > m2.startIndex ? 1 : 0}}), getMentionFeatures(m1, document, mentionsByHeadIndex), getMentionFeatures(m2, document, mentionsByHeadIndex), encodeGenre(document)); return features; }
/** * 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; }
/** * Applies tanh to each of the entries in the matrix. Returns a new matrix. */ public static SimpleMatrix elementwiseApplyTanh(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.tanh(output.get(i, j))); } } return output; }
/** * Applies the derivative of tanh to each of the elements in the vector. Returns a new matrix. */ public static SimpleMatrix elementwiseApplyTanhDerivative(SimpleMatrix input) { SimpleMatrix output = new SimpleMatrix(input.numRows(), input.numCols()); output.set(1.0); output = output.minus(input.elementMult(input)); return output; }
/** * 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; }
/** * 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); }
@SuppressWarnings("unchecked") public Matrix pseudoinverse(double lambda) { SimpleSVD<SimpleMatrix> simpleSVD = this.svd(); SimpleMatrix U = simpleSVD.getU(); SimpleMatrix S = simpleSVD.getW(); SimpleMatrix V = simpleSVD.getV(); int N = Math.min(this.numRows(),this.numCols()); double maxSingular = 0; for( int i = 0; i < N; ++i ) { if( S.get(i, i) > maxSingular ) { maxSingular = S.get(i, i); } } double tolerance = FastMath.DBL_EPSILON * Math.max(this.numRows(),this.numCols()) * maxSingular; for(int i=0;i<Math.min(S.numRows(), S.numCols());++i) { double a = S.get(i, i); if(a <= tolerance) { a = 0; } else { a = a/(a * a + lambda * lambda); } S.set(i, i, a); } return new Matrix(V.mult(S.transpose()).mult(U.transpose())); }
int goldClass = RNNCoreAnnotations.getGoldClass(tree); if (goldClass >= 0) { goldLabel.set(goldClass, 1.0);
public void set(double value) { for (int slice = 0; slice < numSlices; ++slice) { slices[slice].set(value); } }
public static SimpleMatrix oneHot(int index, int size) { SimpleMatrix m = new SimpleMatrix(size, 1); m.set(index, 1); return m; }
/** * Returns a vector with random Gaussian values, mean 0, std 1 */ public static SimpleMatrix randomGaussian(int numRows, int numCols, Random rand) { SimpleMatrix result = new SimpleMatrix(numRows, numCols); for (int i = 0; i < numRows; ++i) { for (int j = 0; j < numCols; ++j) { result.set(i, j, rand.nextGaussian()); } } return result; }