{"record":{"id":"43a26fb7fef61a97","repo":"stanfordnlp/CoreNLP","slug":"sentimentcostandgradient-tree-not-correctly-binar","errorCode":null,"errorMessage":"SentimentCostAndGradient: Tree not correctly binarized:...","messagePattern":"SentimentCostAndGradient: Tree not correctly binarized:\\.\\.\\.","errorType":"exception","errorClass":"ForwardPropagationException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/sentiment/SentimentCostAndGradient.java","lineNumber":540,"sourceCode":"      if (model.op.useTensors) {\n        SimpleTensor tensor = model.getBinaryTensor(leftCategory, rightCategory);\n        SimpleMatrix tensorIn = NeuralUtils.concatenate(leftVector, rightVector);\n        SimpleMatrix tensorOut = tensor.bilinearProducts(tensorIn);\n        nodeVector = NeuralUtils.elementwiseApplyTanh(W.mult(childrenVector).plus(tensorOut));\n      } else {\n        nodeVector = NeuralUtils.elementwiseApplyTanh(W.mult(childrenVector));\n      }\n    } else {\n      StringBuilder error = new StringBuilder();\n      error.append(\"SentimentCostAndGradient: Tree not correctly binarized:\\n   \");\n      error.append(tree);\n      error.append(\"\\nToo many top level constituents present: \");\n      error.append(\"(\" + tree.value());\n      for (Tree child : tree.children()) {\n        error.append(\" (\" + child.value() + \" ...)\");\n      }\n      error.append(\")\");\n      throw new ForwardPropagationException(error.toString());\n    }\n\n    SimpleMatrix predictions = NeuralUtils.softmax(classification.mult(NeuralUtils.concatenateWithBias(nodeVector)));\n\n    int index = getPredictedClass(predictions);\n    if (!(tree.label() instanceof CoreLabel)) {\n      log.info(\"SentimentCostAndGradient: warning: No CoreLabels in nodes: \" + tree);\n      throw new AssertionError(\"Expected CoreLabels in the nodes\");\n    }\n    CoreLabel label = (CoreLabel) tree.label();\n    label.set(RNNCoreAnnotations.Predictions.class, predictions);\n    label.set(RNNCoreAnnotations.PredictedClass.class, index);\n    label.set(RNNCoreAnnotations.NodeVector.class, nodeVector);\n  } // end forwardPropagateTree\n\n}\n","sourceCodeStart":522,"sourceCodeEnd":557,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/sentiment/SentimentCostAndGradient.java#L522-L557","documentation":"After classifying a node, the code checks tree structure/labels (e.g. binarization sanity around top-level constituents and CoreLabel types) and throws ForwardPropagationException with a descriptive error buffer when the tree violates model assumptions, here: not correctly binarized / too many top-level constituents.","triggerScenarios":"Calling forwardPropagate on a tree with more than two children at the root or multiple top-level constituents, i.e. a tree that was never binarized for the sentiment model.","commonSituations":"Feeding raw parser output or non-binarized treebank trees directly into SentimentTraining/ExternalEvaluate pipelines.","solutions":["Binarize trees before passing them to the sentiment model (use the same preprocessing as ReadSentimentDataset/convertTrees)","Check the root has exactly one or two children as the model expects","Validate tree structure programmatically before training"],"exampleFix":"// before\n(ROOT (S (NP ...) (VP ...) (PP ...))) // 3+ branches\n// after\n(ROOT (S (NP ...) (VP (VP ...) (PP ...)))) // binarized","handlingStrategy":"validation","validationCode":"if (root.children().length > 2) throw new IllegalStateException(\"Tree not correctly binarized: root has \" + root.children().length + \" children\");","typeGuard":null,"tryCatchPattern":"try { forwardPropagate(tree); } catch (ForwardPropagationException e) { if (e.getMessage().contains(\"not correctly binarized\")) { binarizeAndRetry(tree); } else throw e; }","preventionTips":["Binarize all trees at dataset load time","Assert binary branching across the whole tree before training","Keep parser output out of the training pipeline until binarized"],"tags":["neural-network","tree-structure"],"backgroundTag":"internal-invariant-violation","analyzedSha":"1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a","analyzedAt":"2026-09-10T02:24:07.274Z","contentChangedAt":"2026-09-10T02:24:07.274Z","schemaVersion":2},"datasetVersion":"2026-09-17T15:17:12.973Z"}