/** * Creates a zero initialized tensor */ public SimpleTensor(int numRows, int numCols, int numSlices) { slices = new SimpleMatrix[numSlices]; for (int i = 0; i < numSlices; ++i) { slices[i] = new SimpleMatrix(numRows, numCols); } this.numRows = numRows; this.numCols = numCols; this.numSlices = numSlices; }
/** <br>Will</br> overwrite an existing word vector */ public static void replaceWordVector(Map<String, SimpleMatrix> wordVectors, String source, String target) { if (!wordVectors.containsKey(source)) { return; } wordVectors.put(target, new SimpleMatrix(wordVectors.get(source))); }
/** * 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; }
/** 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))); }
public static SimpleMatrix oneHot(int index, int size) { SimpleMatrix m = new SimpleMatrix(size, 1); m.set(index, 1); return m; }
private SimpleMatrix encodeGenre(Document document) { return conll ? NeuralUtils.oneHot( genres.get(document.docInfo.get("DOC_ID").split("/")[0]), genres.size()) : new SimpleMatrix(1, 1); } }
public SimpleMatrix getDocumentEmbedding(Document document) { if (!conll) { return new SimpleMatrix(staticWordEmbeddings.getEmbeddingSize(), 1); } List<CoreLabel> words = new ArrayList<>(); Set<Integer> seenSentences = new HashSet<>(); for (Mention m : document.predictedMentionsByID.values()) { if (!seenSentences.contains(m.sentNum)) { seenSentences.add(m.sentNum); words.addAll(m.sentenceWords); } } return getAverageEmbedding(words); }
/** * Init a Map with 0 matrices for all the matrices in the original map. */ private static Map<String, SimpleMatrix> initDerivatives(Map<String, SimpleMatrix> map) { Map<String, SimpleMatrix> derivatives = Generics.newTreeMap(); for (Map.Entry<String, SimpleMatrix> entry : map.entrySet()) { int numRows = entry.getValue().numRows(); int numCols = entry.getValue().numCols(); derivatives.put(entry.getKey(), new SimpleMatrix(numRows, numCols)); } return derivatives; } }
private SimpleMatrix getMentionFeatures(Mention m, Document document, Map<Integer, List<Mention>> mentionsByHeadIndex) { return NeuralUtils.concatenate( NeuralUtils.oneHot(m.mentionType.ordinal(), 4), encodeDistance(m.endIndex - m.startIndex - 1), new SimpleMatrix(new double[][] { {m.mentionNum / (double) document.predictedMentionsByID.size()}, {mentionsByHeadIndex.get(m.headIndex).stream() .anyMatch(m2 -> m != m2 && m.insideIn(m2)) ? 1 : 0}}) ); }
private SimpleMatrix getAverageEmbedding(List<CoreLabel> words) { SimpleMatrix emb = new SimpleMatrix(staticWordEmbeddings.getEmbeddingSize(), 1); for (CoreLabel word : words) { emb = emb.plus(getStaticWordEmbedding(word.word())); } return emb.divide(Math.max(1, words.size())); }
/** * 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; }
private SimpleMatrix getAverageEmbeddings() { double[][] vec = new double[embeddings.getEmbeddingSize()][1]; SimpleMatrix totalVector = new SimpleMatrix(vec); for (IndexedWord w : sequence) { SimpleMatrix vector = embeddings.get(w.word().toLowerCase()); if (vector != null) { totalVector = totalVector.plus(vector); } } return totalVector.divide(this.sequence.size()); } }
/** * 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; }
/** * 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); }
/** * Creates a random context matrix. This will be numRows x * 2*numCols big. These can be appended to the end of either a * unary or binary transform matrix to get the transform matrix * which uses context words. */ private SimpleMatrix randomContextMatrix() { SimpleMatrix matrix = new SimpleMatrix(numRows, numCols * 2); matrix.insertIntoThis(0, 0, identity.scale(op.trainOptions.scalingForInit * 0.1)); matrix.insertIntoThis(0, numCols, identity.scale(op.trainOptions.scalingForInit * 0.1)); matrix = matrix.plus(SimpleMatrix.random(numRows,numCols * 2,-1.0/Math.sqrt((double)numCols * 100.0),1.0/Math.sqrt((double)numCols * 100.0),rand)); return matrix; }
/** * 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; }
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); }
/** * 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; }
/** * Init a TwoDimensionalMap with 0 matrices for all the matrices in the original map. */ private static TwoDimensionalMap<String, String, SimpleMatrix> initDerivatives(TwoDimensionalMap<String, String, SimpleMatrix> map) { TwoDimensionalMap<String, String, SimpleMatrix> derivatives = TwoDimensionalMap.treeMap(); for (TwoDimensionalMap.Entry<String, String, SimpleMatrix> entry : map) { int numRows = entry.getValue().numRows(); int numCols = entry.getValue().numCols(); derivatives.put(entry.getFirstKey(), entry.getSecondKey(), new SimpleMatrix(numRows, numCols)); } return derivatives; }
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); }