TensorTonicTensorTonic
Problems
Study PlansProjectsInterviewPricingFeedback
Study Plan
Build a Scalar Expression Graph
Neural NetworksCalculus
Medium

Build a scalar expression graph from two ordered lists of dictionaries. Each leaf has id and data. Each operation has id, op, left, and right; left and right name previously created nodes, and op is "+" or "*".

z=x+yz = x + yz=x+y z=xyz = xyz=xy

Here x and y are the left and right parent values, and z is the result of the selected operation.

Each returned node has exactly id, data, grad, op, and parents. Its data is the supplied leaf value or the operation result as a Python float, and grad is 0.0. Leaves have an empty op string and an empty parents list. Operation nodes retain their symbol and a list of left and right parent IDs, in that order.

Return a tuple containing the list of fresh node dictionaries in creation order, followed by the final node ID as a string. Leaves come first in their input order, then operations; with no operations, the final node is the last leaf.

Loading visualization...

Examples

Input: leaves=[{'id':'a','data':2.0},{'id':'b','data':-3.0},{'id':'c','data':10.0},{'id':'f','data':-2.0}], operations=[{'id':'e','op':'*','left':'a','right':'b'},{'id':'d','op':'+','left':'e','right':'c'},{'id':'L','op':'*','left':'d','right':'f'}]

Output: ([{'id':'a','data':2.0,'grad':0.0,'op':'','parents':[]},{'id':'b','data':-3.0,'grad':0.0,'op':'','parents':[]},{'id':'c','data':10.0,'grad':0.0,'op':'','parents':[]},{'id':'f','data':-2.0,'grad':0.0,'op':'','parents':[]},{'id':'e','data':-6.0,'grad':0.0,'op':'*','parents':['a','b']},{'id':'d','data':4.0,'grad':0.0,'op':'+','parents':['e','c']},{'id':'L','data':-8.0,'grad':0.0,'op':'*','parents':['d','f']}],'L')

Explanation: Each operation reads previously created values and records its ordered parent IDs.

Input: leaves=[{'id':'x','data':1.5},{'id':'y','data':2.5}], operations=[{'id':'z','op':'+','left':'x','right':'y'}]

Output: ([{'id':'x','data':1.5,'grad':0.0,'op':'','parents':[]},{'id':'y','data':2.5,'grad':0.0,'op':'','parents':[]},{'id':'z','data':4.0,'grad':0.0,'op':'+','parents':['x','y']}],'z')

Input: leaves=[{'id':'only','data':-7.0}], operations=[]

Output: ([{'id':'only','data':-7.0,'grad':0.0,'op':'','parents':[]}],'only')

Hint 1

A dictionary keyed by node ID makes parent values easy to retrieve.

Hint 2

Appending nodes to a separate list preserves creation order.

Requirements

  • Use Python floating-point arithmetic (64-bit); NumPy is available.
  • Preserve repeated parent IDs when both operands name the same node.
  • Leave leaves and operations unchanged.

Constraints

  • leaves is a nonempty list of dictionaries containing exactly id and data.
  • operations is a possibly empty list of dictionaries containing exactly id, op, left, and right.
  • Node IDs are unique nonempty strings; both parents of each operation already exist.
  • Each op is "+" or "*". Leaf data and every computed result are finite.
Try Similar Problems
Trace a Reachable Expression GraphMediumCreate a Multiplication Value NodeEasyCreate an Addition Value NodeEasyBackpropagate Through a Scalar NeuronMediumTopologically Sort a Computation DAGMediumAccumulate Shared-Path GradientsMediumGradient-Check a Scalar NeuronMediumRun Reverse-Mode AutodiffHardChain Rule Through a 3-Layer NetworkMediumBackpropagation Through a Computation GraphHardJacobian of a Neural Network LayerHardBasic AutogradEasy

Accepts: array

Accepts: array

You must run your code first.

Build a Scalar Expression Graph

Neural NetworksCalculus
Medium

Build a scalar expression graph from two ordered lists of dictionaries. Each leaf has id and data. Each operation has id, op, left, and right; left and right name previously created nodes, and op is "+" or "*".

z=x+yz = x + yz=x+y z=xyz = xyz=xy

Here x and y are the left and right parent values, and z is the result of the selected operation.

Each returned node has exactly id, data, grad, op, and parents. Its data is the supplied leaf value or the operation result as a Python float, and grad is 0.0. Leaves have an empty op string and an empty parents list. Operation nodes retain their symbol and a list of left and right parent IDs, in that order.

Return a tuple containing the list of fresh node dictionaries in creation order, followed by the final node ID as a string. Leaves come first in their input order, then operations; with no operations, the final node is the last leaf.

Loading visualization...

Examples

Input: leaves=[{'id':'a','data':2.0},{'id':'b','data':-3.0},{'id':'c','data':10.0},{'id':'f','data':-2.0}], operations=[{'id':'e','op':'*','left':'a','right':'b'},{'id':'d','op':'+','left':'e','right':'c'},{'id':'L','op':'*','left':'d','right':'f'}]

Output: ([{'id':'a','data':2.0,'grad':0.0,'op':'','parents':[]},{'id':'b','data':-3.0,'grad':0.0,'op':'','parents':[]},{'id':'c','data':10.0,'grad':0.0,'op':'','parents':[]},{'id':'f','data':-2.0,'grad':0.0,'op':'','parents':[]},{'id':'e','data':-6.0,'grad':0.0,'op':'*','parents':['a','b']},{'id':'d','data':4.0,'grad':0.0,'op':'+','parents':['e','c']},{'id':'L','data':-8.0,'grad':0.0,'op':'*','parents':['d','f']}],'L')

Explanation: Each operation reads previously created values and records its ordered parent IDs.

Input: leaves=[{'id':'x','data':1.5},{'id':'y','data':2.5}], operations=[{'id':'z','op':'+','left':'x','right':'y'}]

Output: ([{'id':'x','data':1.5,'grad':0.0,'op':'','parents':[]},{'id':'y','data':2.5,'grad':0.0,'op':'','parents':[]},{'id':'z','data':4.0,'grad':0.0,'op':'+','parents':['x','y']}],'z')

Input: leaves=[{'id':'only','data':-7.0}], operations=[]

Output: ([{'id':'only','data':-7.0,'grad':0.0,'op':'','parents':[]}],'only')

Hint 1

A dictionary keyed by node ID makes parent values easy to retrieve.

Hint 2

Appending nodes to a separate list preserves creation order.

Requirements

  • Use Python floating-point arithmetic (64-bit); NumPy is available.
  • Preserve repeated parent IDs when both operands name the same node.
  • Leave leaves and operations unchanged.

Constraints

  • leaves is a nonempty list of dictionaries containing exactly id and data.
  • operations is a possibly empty list of dictionaries containing exactly id, op, left, and right.
  • Node IDs are unique nonempty strings; both parents of each operation already exist.
  • Each op is "+" or "*". Leaf data and every computed result are finite.
Try Similar Problems
Trace a Reachable Expression GraphMediumCreate a Multiplication Value NodeEasyCreate an Addition Value NodeEasyBackpropagate Through a Scalar NeuronMediumTopologically Sort a Computation DAGMediumAccumulate Shared-Path GradientsMediumGradient-Check a Scalar NeuronMediumRun Reverse-Mode AutodiffHardChain Rule Through a 3-Layer NetworkMediumBackpropagation Through a Computation GraphHardJacobian of a Neural Network LayerHardBasic AutogradEasy

Accepts: array

Accepts: array

You must run your code first.