public void printMatrixNames(PrintStream out) { out.println("Binary matrices:"); for (TwoDimensionalMap.Entry<String, String, SimpleMatrix> binary : binaryTransform) { out.println(" " + binary.getFirstKey() + ":" + binary.getSecondKey()); } out.println("Unary matrices:"); for (String unary : unaryTransform.keySet()) { out.println(" " + unary); } }
public void printAllMatrices(PrintStream out) { for (TwoDimensionalMap.Entry<String, String, SimpleMatrix> binary : binaryTransform) { out.println("Binary transform " + binary.getFirstKey() + ":" + binary.getSecondKey()); out.println(binary.getValue()); } for (TwoDimensionalMap.Entry<String, String, SimpleMatrix> binary : binaryScore) { out.println("Binary score " + binary.getFirstKey() + ":" + binary.getSecondKey()); out.println(binary.getValue()); } for (Map.Entry<String, SimpleMatrix> unary : unaryTransform.entrySet()) { out.println("Unary transform " + unary.getKey()); out.println(unary.getValue()); } for (Map.Entry<String, SimpleMatrix> unary : unaryScore.entrySet()) { out.println("Unary score " + unary.getKey()); out.println(unary.getValue()); } }
if (!matrix.getFirstKey().equals("") || !matrix.getSecondKey().equals("")) { output.append(matrix.getFirstKey() + " " + matrix.getSecondKey() + ":\n"); if (!matrix.getFirstKey().equals("") || !matrix.getSecondKey().equals("")) { output.append(matrix.getFirstKey() + " " + matrix.getSecondKey() + ":\n"); if (!matrix.getFirstKey().equals("") || !matrix.getSecondKey().equals("")) { output.append(matrix.getFirstKey() + " " + matrix.getSecondKey() + ":\n");
FileSystem.mkdirOrFail(binaryWDir); for (TwoDimensionalMap.Entry<String, String, SimpleMatrix> entry : model.binaryTransform) { String filename = binaryWDir + File.separator + entry.getFirstKey() + "_" + entry.getSecondKey() + ".txt"; dumpMatrix(filename, entry.getValue()); FileSystem.mkdirOrFail(binaryScoreDir); for (TwoDimensionalMap.Entry<String, String, SimpleMatrix> entry : model.binaryScore) { String filename = binaryScoreDir + File.separator + entry.getFirstKey() + "_" + entry.getSecondKey() + ".txt"; dumpMatrix(filename, entry.getValue());
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 { log.info("Index " + index + " is element " + (index - curIndex) + " of binaryClassification \"" + entry.getFirstKey() + "," + entry.getSecondKey() + "\""); return; } else { log.info("Index " + index + " is element " + (index - curIndex) + " of binaryTensor \"" + entry.getFirstKey() + "," + entry.getSecondKey() + "\""); return; } else {
private static double scaleAndRegularize(TwoDimensionalMap<String, String, SimpleMatrix> derivatives, TwoDimensionalMap<String, String, SimpleMatrix> currentMatrices, double scale, double regCost, boolean dropBiasColumn) { double cost = 0.0; // the regularization cost for (TwoDimensionalMap.Entry<String, String, SimpleMatrix> entry : currentMatrices) { SimpleMatrix D = derivatives.get(entry.getFirstKey(), entry.getSecondKey()); SimpleMatrix regMatrix = entry.getValue(); if (dropBiasColumn) { regMatrix = new SimpleMatrix(regMatrix); regMatrix.insertIntoThis(0, regMatrix.numCols() - 1, new SimpleMatrix(regMatrix.numRows(), 1)); } D = D.scale(scale).plus(regMatrix.scale(regCost)); derivatives.put(entry.getFirstKey(), entry.getSecondKey(), D); cost += regMatrix.elementMult(regMatrix).elementSum() * regCost / 2.0; } return cost; }
/** * Add matrices from the second map to the first map, in place. */ public static void addMatrices(TwoDimensionalMap<String, String, SimpleMatrix> first, TwoDimensionalMap<String, String, SimpleMatrix> second) { for (TwoDimensionalMap.Entry<String, String, SimpleMatrix> entry : first) { if (second.contains(entry.getFirstKey(), entry.getSecondKey())) { first.put(entry.getFirstKey(), entry.getSecondKey(), entry.getValue().plus(second.get(entry.getFirstKey(), entry.getSecondKey()))); } } for (TwoDimensionalMap.Entry<String, String, SimpleMatrix> entry : second) { if (!first.contains(entry.getFirstKey(), entry.getSecondKey())) { first.put(entry.getFirstKey(), entry.getSecondKey(), entry.getValue()); } } }
/** * Add tensors from the second map to the first map, in place. */ public static void addTensors(TwoDimensionalMap<String, String, SimpleTensor> first, TwoDimensionalMap<String, String, SimpleTensor> second) { for (TwoDimensionalMap.Entry<String, String, SimpleTensor> entry : first) { if (second.contains(entry.getFirstKey(), entry.getSecondKey())) { first.put(entry.getFirstKey(), entry.getSecondKey(), entry.getValue().plus(second.get(entry.getFirstKey(), entry.getSecondKey()))); } } for (TwoDimensionalMap.Entry<String, String, SimpleTensor> entry : second) { if (!first.contains(entry.getFirstKey(), entry.getSecondKey())) { first.put(entry.getFirstKey(), entry.getSecondKey(), entry.getValue()); } } }
public Pair<K1, K2> next() { TwoDimensionalMap.Entry<K1, K2, Boolean> entry = backingIterator.next(); return Pair.makePair(entry.getFirstKey(), entry.getSecondKey()); }
private static double scaleAndRegularizeTensor(TwoDimensionalMap<String, String, SimpleTensor> derivatives, TwoDimensionalMap<String, String, SimpleTensor> currentMatrices, double scale, double regCost) { double cost = 0.0; // the regularization cost for (TwoDimensionalMap.Entry<String, String, SimpleTensor> entry : currentMatrices) { SimpleTensor D = derivatives.get(entry.getFirstKey(), entry.getSecondKey()); D = D.scale(scale).plus(entry.getValue().scale(regCost)); derivatives.put(entry.getFirstKey(), entry.getSecondKey(), D); cost += entry.getValue().elementMult(entry.getValue()).elementSum() * regCost / 2.0; } return cost; }
/** * Init a TwoDimensionalMap with 0 tensors for all the tensors in the original map. */ private static TwoDimensionalMap<String, String, SimpleTensor> initTensorDerivatives(TwoDimensionalMap<String, String, SimpleTensor> map) { TwoDimensionalMap<String, String, SimpleTensor> derivatives = TwoDimensionalMap.treeMap(); for (TwoDimensionalMap.Entry<String, String, SimpleTensor> entry : map) { int numRows = entry.getValue().numRows(); int numCols = entry.getValue().numCols(); int numSlices = entry.getValue().numSlices(); derivatives.put(entry.getFirstKey(), entry.getSecondKey(), new SimpleTensor(numRows, numCols, numSlices)); } return derivatives; }
public Pair<String, String> indexToBinaryScore(int pos) { pos -= (numBinaryMatrices * binaryTransformSize + numUnaryMatrices * unaryTransformSize); if (pos < numBinaryMatrices * binaryScoreSize && pos >= 0) { for (TwoDimensionalMap.Entry<String, String, SimpleMatrix> entry : binaryScore) { if (binaryScoreSize < pos) { pos -= binaryScoreSize; } else { return Pair.makePair(entry.getFirstKey(), entry.getSecondKey()); } } } return null; }
/** * Adds all of the entries in the <code>other</code> map, performing * <code>function</code> on them to transform the values */ public <V2> void addAll(TwoDimensionalMap<? extends K1, ? extends K2, ? extends V2> other, Function<V2, V> function) { for (TwoDimensionalMap.Entry<? extends K1, ? extends K2, ? extends V2> entry : other) { put(entry.getFirstKey(), entry.getSecondKey(), function.apply(entry.getValue())); } }
public static TwoDimensionalSet<String, String> getBinaryMatrixNames(List<TwoDimensionalMap<String, String, SimpleMatrix>> maps) { TwoDimensionalSet<String, String> matrixNames = new TwoDimensionalSet<>(); for (TwoDimensionalMap<String, String, SimpleMatrix> map : maps) { for (TwoDimensionalMap.Entry<String, String, SimpleMatrix> entry : map) { matrixNames.add(entry.getFirstKey(), entry.getSecondKey()); } } return matrixNames; }
public Pair<String, String> indexToBinaryTransform(int pos) { if (pos < numBinaryMatrices * binaryTransformSize) { for (TwoDimensionalMap.Entry<String, String, SimpleMatrix> entry : binaryTransform) { if (binaryTransformSize < pos) { pos -= binaryTransformSize; } else { return Pair.makePair(entry.getFirstKey(), entry.getSecondKey()); } } } return null; }
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; }
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; }
/** * Adds all the keys in the given TwoDimensionalMap. Returns true iff at least one key is added. */ public boolean addAllKeys(TwoDimensionalMap<? extends K1, ? extends K2, ?> map) { boolean result = false; for (TwoDimensionalMap.Entry<? extends K1, ? extends K2, ?> entry : map) { if (add(entry.getFirstKey(), entry.getSecondKey())) { result = true; } } return result; }
/** * 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; }
public void printMatrixStats(PrintStream out) { log.info("Model loaded with " + numUnaryMatrices + " unary and " + numBinaryMatrices + " binary"); for (TwoDimensionalMap.Entry<String, String, SimpleMatrix> binary : binaryTransform) { out.println("Binary transform " + binary.getFirstKey() + ":" + binary.getSecondKey()); double normf = binary.getValue().normF(); out.println(" Total norm " + (normf * normf)); normf = binary.getValue().extractMatrix(0, op.lexOptions.numHid, 0, op.lexOptions.numHid).normF(); out.println(" Left norm (" + binary.getFirstKey() + ") " + (normf * normf)); normf = binary.getValue().extractMatrix(0, op.lexOptions.numHid, op.lexOptions.numHid, op.lexOptions.numHid*2).normF(); out.println(" Right norm (" + binary.getSecondKey() + ") " + (normf * normf)); } }