public boolean add(K1 k1, K2 k2) { return (backingMap.put(k1, k2, true) != null); }
public V put(K1 key1, K2 key2, K3 key3, V value) { TwoDimensionalMap<K2, K3, V> m = getTwoDimensionalMap(key1); return m.put(key2, key3, value); }
/** * 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())); } }
/** * Given single matrices and sets of options, create the * corresponding SentimentModel. Useful for creating a Java version * of a model trained in some other manner, such as using the * original Matlab code. */ static SentimentModel modelFromMatrices(SimpleMatrix W, SimpleMatrix Wcat, SimpleTensor Wt, Map<String, SimpleMatrix> wordVectors, RNNOptions op) { if (!op.combineClassification || !op.simplifiedModel) { throw new IllegalArgumentException("Can only create a model using this method if combineClassification and simplifiedModel are turned on"); } TwoDimensionalMap<String, String, SimpleMatrix> binaryTransform = TwoDimensionalMap.treeMap(); binaryTransform.put("", "", W); TwoDimensionalMap<String, String, SimpleTensor> binaryTensors = TwoDimensionalMap.treeMap(); binaryTensors.put("", "", Wt); TwoDimensionalMap<String, String, SimpleMatrix> binaryClassification = TwoDimensionalMap.treeMap(); Map<String, SimpleMatrix> unaryClassification = Generics.newTreeMap(); unaryClassification.put("", Wcat); return new SentimentModel(binaryTransform, binaryTensors, binaryClassification, unaryClassification, wordVectors, op); }
public static boolean entityIsAcronym(Document document, CorefCluster mentionCluster, CorefCluster potentialAntecedent) { int minId = Math.min(mentionCluster.clusterID, potentialAntecedent.clusterID); int maxId = Math.max(mentionCluster.clusterID, potentialAntecedent.clusterID); if(!document.acronymCache.contains(minId, maxId)) { boolean isAcronym = false; for(Mention m : mentionCluster.corefMentions){ if(m.isPronominal()) continue; for(Mention ant : potentialAntecedent.corefMentions){ if(isAcronym(m.originalSpan, ant.originalSpan)) isAcronym = true; } } document.acronymCache.put(minId, maxId, isAcronym); } return document.acronymCache.get(minId, maxId); }
public void mergeAcronymCache(CorefCluster to, CorefCluster from) { TwoDimensionalSet<Integer, Integer> replacements = TwoDimensionalSet.hashSet(); for (Integer first : acronymCache.firstKeySet()) { for (Integer second : acronymCache.get(first).keySet()) { if (acronymCache.get(first, second)) { Integer other = null; if (first == from.clusterID) { other = second; } else if (second == from.clusterID) { other = first; } if (other != null && other != to.clusterID) { int cid1 = Math.min(other, to.clusterID); int cid2 = Math.max(other, to.clusterID); replacements.add(cid1, cid2); } } } } for (Integer first : replacements.firstKeySet()) { for (Integer second : replacements.secondKeySet(first)) { acronymCache.put(first, second, true); } } }
public void addRandomBinaryMatrix(String leftBasic, String rightBasic) { if (binaryTransform.get(leftBasic, rightBasic) != null) { return; } ++numBinaryMatrices; // scoring matrix SimpleMatrix score = SimpleMatrix.random(1, numCols, -1.0/Math.sqrt((double)numCols),1.0/Math.sqrt((double)numCols),rand); binaryScore.put(leftBasic, rightBasic, score.scale(op.trainOptions.scalingForInit)); SimpleMatrix binary; if (op.trainOptions.useContextWords) { binary = new SimpleMatrix(numRows, numCols * 4 + 1); // leave room for bias term binary.insertIntoThis(0,numCols*2+1, randomContextMatrix()); } else { binary = new SimpleMatrix(numRows, numCols * 2 + 1); } SimpleMatrix left = randomTransformMatrix(); SimpleMatrix right = randomTransformMatrix(); binary.insertIntoThis(0, 0, left); binary.insertIntoThis(0, numCols, right); binaryTransform.put(leftBasic, rightBasic, binary.scale(op.trainOptions.scalingForInit)); }
/** * 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; }
/** * 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()); } } }
/** * 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()); } } }
/** * 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; }
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; }
public static TwoDimensionalMap<String, String, SimpleMatrix> averageBinaryMatrices(List<TwoDimensionalMap<String, String, SimpleMatrix>> maps) { TwoDimensionalMap<String, String, SimpleMatrix> averages = TwoDimensionalMap.treeMap(); for (Pair<String, String> binary : getBinaryMatrixNames(maps)) { int count = 0; SimpleMatrix matrix = null; for (TwoDimensionalMap<String, String, SimpleMatrix> map : maps) { if (!map.contains(binary.first(), binary.second())) { continue; } SimpleMatrix original = map.get(binary.first(), binary.second()); ++count; if (matrix == null) { matrix = original; } else { matrix = matrix.plus(original); } } matrix = matrix.divide(count); averages.put(binary.first(), binary.second(), matrix); } return averages; }
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; }
nodes.put(in.getIndex(), in.getCopyAnnotation(), word); graph.addVertex(word);
nodeMap.put(word.index(), word.copyCount(), word); graph.addVertex(word); if (in.isRoot) {
double[][] map = new double[1][1]; map[0][0] = 0.0; unaryBetas.put(label, childLabel, map);