TensorTonicTensorTonic
Problems
Study PlansProjectsInterviewPricingFeedback
Study Plan
Accumulate Shared-Path Gradients
Neural NetworksCalculus
Medium

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.

∂y∂a=∑p∈P(a,y)cp\frac{\partial y}{\partial a} = \sum_{p\in P(a,y)}c_p∂a∂y​=p∈P(a,y)∑​cp​

Here 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.

Loading visualization...

Examples

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}

Hint 1

A dictionary can hold one gradient per node ID.

Hint 2

Iterate parent positions rather than a set so repeated operands stay distinct.

Hint 3

reversed can traverse a valid forward evaluation order backward.

Requirements

  • Use 64-bit floating-point arithmetic; NumPy is available.
  • Compute derivatives manually, without automatic differentiation.
  • Leave leaves and operations unchanged.

Constraints

  • leaves and operations are lists of dictionaries with exactly the fields described above.
  • Node IDs are unique nonempty strings, every parent ID exists, and the graph is acyclic.
  • output_id identifies a leaf or operation; leaf values and all calculated values are finite.
  • Parent IDs may repeat within one operation or appear in several operations.
Try Similar Problems
Backpropagate Through a Scalar NeuronMediumTopologically Sort a Computation DAGMediumTrace a Reachable Expression GraphMediumGradient-Check a Scalar NeuronMediumBuild a Scalar Expression GraphMediumDifferentiate Exponentials and PowersMediumCreate a Multiplication Value NodeEasyRun Reverse-Mode AutodiffHardChain Rule Through a 3-Layer NetworkMediumBackpropagation Through a Computation GraphHardJacobian of a Neural Network LayerHardBasic AutogradEasy

Accepts: array

Accepts: array

Accepts: string

You must run your code first.

Accumulate Shared-Path Gradients

Neural NetworksCalculus
Medium

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.

∂y∂a=∑p∈P(a,y)cp\frac{\partial y}{\partial a} = \sum_{p\in P(a,y)}c_p∂a∂y​=p∈P(a,y)∑​cp​

Here 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.

Loading visualization...

Examples

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}

Hint 1

A dictionary can hold one gradient per node ID.

Hint 2

Iterate parent positions rather than a set so repeated operands stay distinct.

Hint 3

reversed can traverse a valid forward evaluation order backward.

Requirements

  • Use 64-bit floating-point arithmetic; NumPy is available.
  • Compute derivatives manually, without automatic differentiation.
  • Leave leaves and operations unchanged.

Constraints

  • leaves and operations are lists of dictionaries with exactly the fields described above.
  • Node IDs are unique nonempty strings, every parent ID exists, and the graph is acyclic.
  • output_id identifies a leaf or operation; leaf values and all calculated values are finite.
  • Parent IDs may repeat within one operation or appear in several operations.
Try Similar Problems
Backpropagate Through a Scalar NeuronMediumTopologically Sort a Computation DAGMediumTrace a Reachable Expression GraphMediumGradient-Check a Scalar NeuronMediumBuild a Scalar Expression GraphMediumDifferentiate Exponentials and PowersMediumCreate a Multiplication Value NodeEasyRun Reverse-Mode AutodiffHardChain Rule Through a 3-Layer NetworkMediumBackpropagation Through a Computation GraphHardJacobian of a Neural Network LayerHardBasic AutogradEasy

Accepts: array

Accepts: array

Accepts: string

You must run your code first.