Compute leaf gradients for a scalar expression that reuses intermediate values. Each leaf has id and value. Each operation has id, op, and parents, an ordered list of operand IDs. The supported operations are add and mul with two operands, and tanh with one. Operation records need not be in dependency order.
A parent may be used by several operations or more than once by the same operation. Every use contributes to its gradient.
∂a∂y=p∈P(a,y)∑cpHere y is the selected output, a is a contributing node, P is the set of paths from a to y, p is one such path, and c is the product of local derivatives along that path.
Return a Python dictionary mapping each reachable leaf ID to its accumulated 64-bit floating-point gradient in original leaf order. Exclude disconnected leaves. If the selected output is a leaf, return only that leaf with gradient 1.0.
Input: leaves = [{"id":"a","value":4}], operations = [{"id":"out","op":"add","parents":["a","a"]}], output_id = "out"
Output: {"a": 2.0}
Explanation: Both uses of a contribute a derivative of one.
Input: leaves = [{"id":"a","value":3}], operations = [{"id":"out","op":"mul","parents":["a","a"]}], output_id = "out"
Output: {"a": 6.0}
Input: leaves = [{"id":"x","value":2},{"id":"c","value":3}], operations = [{"id":"left","op":"mul","parents":["x","c"]},{"id":"right","op":"add","parents":["x","c"]},{"id":"out","op":"add","parents":["left","right"]}], output_id = "out"
Output: {"x": 4.0, "c": 3.0}
A dictionary can hold one gradient per node ID.
Iterate parent positions rather than a set so repeated operands stay distinct.
reversed can traverse a valid forward evaluation order backward.
Accepts: array
Accepts: array
Accepts: string
Compute leaf gradients for a scalar expression that reuses intermediate values. Each leaf has id and value. Each operation has id, op, and parents, an ordered list of operand IDs. The supported operations are add and mul with two operands, and tanh with one. Operation records need not be in dependency order.
A parent may be used by several operations or more than once by the same operation. Every use contributes to its gradient.
∂a∂y=p∈P(a,y)∑cpHere y is the selected output, a is a contributing node, P is the set of paths from a to y, p is one such path, and c is the product of local derivatives along that path.
Return a Python dictionary mapping each reachable leaf ID to its accumulated 64-bit floating-point gradient in original leaf order. Exclude disconnected leaves. If the selected output is a leaf, return only that leaf with gradient 1.0.
Input: leaves = [{"id":"a","value":4}], operations = [{"id":"out","op":"add","parents":["a","a"]}], output_id = "out"
Output: {"a": 2.0}
Explanation: Both uses of a contribute a derivative of one.
Input: leaves = [{"id":"a","value":3}], operations = [{"id":"out","op":"mul","parents":["a","a"]}], output_id = "out"
Output: {"a": 6.0}
Input: leaves = [{"id":"x","value":2},{"id":"c","value":3}], operations = [{"id":"left","op":"mul","parents":["x","c"]},{"id":"right","op":"add","parents":["x","c"]},{"id":"out","op":"add","parents":["left","right"]}], output_id = "out"
Output: {"x": 4.0, "c": 3.0}
A dictionary can hold one gradient per node ID.
Iterate parent positions rather than a set so repeated operands stay distinct.
reversed can traverse a valid forward evaluation order backward.
Accepts: array
Accepts: array
Accepts: string