public int unaryScoreIndex(String child) { int pos = (binaryTransformSize + binaryScoreSize) * numBinaryMatrices + unaryTransformSize * numUnaryMatrices; for (Map.Entry<String, SimpleMatrix> unary : unaryScore.entrySet()) { if (unary.getKey().equals(child)) { return pos; } pos += unary.getValue().getNumElements(); } return -1; }
public int unaryTransformIndex(String child) { int pos = binaryTransformSize * numBinaryMatrices; for (Map.Entry<String, SimpleMatrix> unary : unaryTransform.entrySet()) { if (unary.getKey().equals(child)) { return pos; } pos += unary.getValue().getNumElements(); } return -1; }
public static void outputMatrix(BufferedWriter bout, SimpleMatrix matrix) throws IOException { for (int i = 0; i < matrix.getNumElements(); ++i) { bout.write(" " + matrix.get(i)); } bout.newLine(); }
/** * Returns true iff every element of matrix is 0 */ public static boolean isZero(SimpleMatrix matrix) { int size = matrix.getNumElements(); for (int i = 0; i < size; ++i) { if (matrix.get(i) != 0.0) { return false; } } return true; } }
private static int getEmbeddingSize(Map<String, SimpleMatrix> wordVectors){ if (!wordVectors.containsKey(UNKNOWN_WORD)){ // find if there's any other unk string String unkStr = ""; if (wordVectors.containsKey("UNK")) { unkStr = "UNK"; } if (wordVectors.containsKey("UUUNKKK")) { unkStr = "UUUNKKK"; } if (wordVectors.containsKey("UNKNOWN")) { unkStr = "UNKNOWN"; } if (wordVectors.containsKey("*UNKNOWN*")) { unkStr = "*UNKNOWN*"; } if (wordVectors.containsKey("<unk>")) { unkStr = "<unk>"; } // set UNKNOWN_WORD if ( ! unkStr.isEmpty()){ wordVectors.put(UNKNOWN_WORD, wordVectors.get(unkStr)); } else { throw new RuntimeException("! wordVectors used to initialize Embedding doesn't contain any recognized form of " + UNKNOWN_WORD); } } return wordVectors.get(UNKNOWN_WORD).getNumElements(); }
/** * 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"); } }
/** * 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; }
/** * 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; }
public int binaryTransformIndex(String leftChild, String rightChild) { int pos = 0; for (TwoDimensionalMap.Entry<String, String, SimpleMatrix> binary : binaryTransform) { if (binary.getFirstKey().equals(leftChild) && binary.getSecondKey().equals(rightChild)) { return pos; } pos += binary.getValue().getNumElements(); } return -1; }
public int binaryScoreIndex(String leftChild, String rightChild) { int pos = binaryTransformSize * numBinaryMatrices + unaryTransformSize * numUnaryMatrices; for (TwoDimensionalMap.Entry<String, String, SimpleMatrix> binary : binaryScore) { if (binary.getFirstKey().equals(leftChild) && binary.getSecondKey().equals(rightChild)) { return pos; } pos += binary.getValue().getNumElements(); } return -1; }
/** * Returns the index with the highest value in the {@code predictions} matrix. * Indexed from 0. */ private static int getPredictedClass(SimpleMatrix predictions) { int argmax = 0; for (int i = 1; i < predictions.getNumElements(); ++i) { if (predictions.get(i) > predictions.get(argmax)) { argmax = i; } } return argmax; }
public DVModel(TwoDimensionalMap<String, String, SimpleMatrix> binaryTransform, Map<String, SimpleMatrix> unaryTransform, TwoDimensionalMap<String, String, SimpleMatrix> binaryScore, Map<String, SimpleMatrix> unaryScore, Map<String, SimpleMatrix> wordVectors, Options op) { this.op = op; this.binaryTransform = binaryTransform; this.unaryTransform = unaryTransform; this.binaryScore = binaryScore; this.unaryScore = unaryScore; this.wordVectors = wordVectors; this.numBinaryMatrices = binaryTransform.size(); this.numUnaryMatrices = unaryTransform.size(); if (numBinaryMatrices > 0) { this.binaryTransformSize = binaryTransform.iterator().next().getValue().getNumElements(); this.binaryScoreSize = binaryScore.iterator().next().getValue().getNumElements(); } else { this.binaryTransformSize = 0; this.binaryScoreSize = 0; } if (numUnaryMatrices > 0) { this.unaryTransformSize = unaryTransform.values().iterator().next().getNumElements(); this.unaryScoreSize = unaryScore.values().iterator().next().getNumElements(); } else { this.unaryTransformSize = 0; this.unaryScoreSize = 0; } this.numRows = op.lexOptions.numHid; this.numCols = op.lexOptions.numHid; this.identity = SimpleMatrix.identity(numRows); this.rand = new Random(op.trainOptions.randomSeed); }
int nh = 0; for (SimpleMatrix wv : wordVectors.values()) { nh = wv.getNumElements();
/** * Outputs the scores from the tree. Counts the tree nodes the * same as setIndexLabels. */ private static int outputTreeScores(PrintStream out, Tree tree, int index) { if (tree.isLeaf()) { return index; } out.print(" " + index + ':'); SimpleMatrix vector = RNNCoreAnnotations.getPredictions(tree); for (int i = 0; i < vector.getNumElements(); ++i) { out.print(" " + NF.format(vector.get(i))); } out.println(); index++; for (Tree child : tree.children()) { index = outputTreeScores(out, child, index); } return index; }
/** * Outputs the vectors from the tree. Counts the tree nodes the * same as setIndexLabels. */ private static int outputTreeVectors(PrintStream out, Tree tree, int index) { if (tree.isLeaf()) { return index; } out.print(" " + index + ':'); SimpleMatrix vector = RNNCoreAnnotations.getNodeVector(tree); for (int i = 0; i < vector.getNumElements(); ++i) { out.print(" " + NF.format(vector.get(i))); } out.println(); index++; for (Tree child : tree.children()) { index = outputTreeVectors(out, child, index); } return index; }
public void printParamInformation(int index) { int curIndex = 0; for (TwoDimensionalMap.Entry<String, String, SimpleMatrix> entry : binaryTransform) { if (curIndex <= index && curIndex + entry.getValue().getNumElements() > index) { log.info("Index " + index + " is element " + (index - curIndex) + " of binaryTransform \"" + entry.getFirstKey() + "," + entry.getSecondKey() + "\""); return; } else { curIndex += entry.getValue().getNumElements(); if (curIndex <= index && curIndex + entry.getValue().getNumElements() > index) { log.info("Index " + index + " is element " + (index - curIndex) + " of binaryClassification \"" + entry.getFirstKey() + "," + entry.getSecondKey() + "\""); return; } else { curIndex += entry.getValue().getNumElements(); if (curIndex <= index && curIndex + entry.getValue().getNumElements() > index) { log.info("Index " + index + " is element " + (index - curIndex) + " of unaryClassification \"" + entry.getKey() + "\""); return; } else { curIndex += entry.getValue().getNumElements(); if (curIndex <= index && curIndex + entry.getValue().getNumElements() > index) { log.info("Index " + index + " is element " + (index - curIndex) + " of wordVector \"" + entry.getKey() + "\""); return; } else { curIndex += entry.getValue().getNumElements();
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; }
op.lexOptions.numHid = vector.getNumElements();
int size = 0; for (SimpleMatrix vector : wordVectors.values()) { size = vector.getNumElements(); break;
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); }