private SimpleMatrix GetAdjustment(ScalePoint point, int level, int x, int y, out double dp) { dp = 0.0; if (point.Level <= 0 || point.Level >= (spaces.Length - 1)) { throw (new ArgumentException("point.Level is not within [bottom-1;top-1] range")); } ImageMap below = spaces[level - 1]; ImageMap current = spaces[level]; ImageMap above = spaces[level + 1]; SimpleMatrix H = new SimpleMatrix(3, 3); H[0, 0] = below[x, y] - 2 * current[x, y] + above[x, y]; H[0, 1] = H[1, 0] = 0.25 * (above[x, y + 1] - above[x, y - 1] - (below[x, y + 1] - below[x, y - 1])); H[0, 2] = H[2, 0] = 0.25 * (above[x + 1, y] - above[x - 1, y] - (below[x + 1, y] - below[x - 1, y])); H[1, 1] = current[x, y - 1] - 2 * current[x, y] + current[x, y + 1]; H[1, 2] = H[2, 1] = 0.25 * (current[x + 1, y + 1] - current[x - 1, y + 1] - (current[x + 1, y - 1] - current[x - 1, y - 1])); H[2, 2] = current[x - 1, y] - 2 * current[x, y] + current[x + 1, y]; SimpleMatrix d = new SimpleMatrix(3, 1); d[0, 0] = 0.5 * (above[x, y] - below[x, y]); d[1, 0] = 0.5 * (current[x, y + 1] - current[x, y - 1]); d[2, 0] = 0.5 * (current[x + 1, y] - current[x - 1, y]); SimpleMatrix b = (SimpleMatrix)d.Clone(); b.Negate(); H.SolveLinear(b); dp = b.Dot(d); return(b); }
private void ForwardPropagateTree(Tree tree, IList <string> words, IdentityHashMap <Tree, SimpleMatrix> nodeVectors, IdentityHashMap <Tree, double> scores) { if (tree.IsLeaf()) { return; } if (tree.IsPreTerminal()) { Tree wordNode = tree.Children()[0]; string word = wordNode.Label().Value(); SimpleMatrix wordVector = dvModel.GetWordVector(word); wordVector = NeuralUtils.ElementwiseApplyTanh(wordVector); nodeVectors[tree] = wordVector; return; } foreach (Tree child in tree.Children()) { ForwardPropagateTree(child, words, nodeVectors, scores); } // at this point, nodeVectors contains the vectors for all of // the children of tree SimpleMatrix childVec; if (tree.Children().Length == 2) { childVec = NeuralUtils.ConcatenateWithBias(nodeVectors[tree.Children()[0]], nodeVectors[tree.Children()[1]]); } else { childVec = NeuralUtils.ConcatenateWithBias(nodeVectors[tree.Children()[0]]); } if (op.trainOptions.useContextWords) { childVec = ConcatenateContextWords(childVec, tree.GetSpan(), words); } SimpleMatrix W = dvModel.GetWForNode(tree); if (W == null) { string error = "Could not find W for tree " + tree; if (op.testOptions.verbose) { log.Info(error); } throw new NoSuchParseException(error); } SimpleMatrix currentVector = W.Mult(childVec); currentVector = NeuralUtils.ElementwiseApplyTanh(currentVector); nodeVectors[tree] = currentVector; SimpleMatrix scoreW = dvModel.GetScoreWForNode(tree); if (scoreW == null) { string error = "Could not find scoreW for tree " + tree; if (op.testOptions.verbose) { log.Info(error); } throw new NoSuchParseException(error); } double score = scoreW.Dot(currentVector); //score = NeuralUtils.sigmoid(score); scores[tree] = score; }