TensorTonicTensorTonic
Problems
Study PlansProjectsInterviewPricingFeedback
Study Plan
Apply Local Vector-Jacobian Rules
Calculus
Easy

Compute the input-gradient contributions for one saved scalar operation: add and mul take two inputs, while tanh takes one. The supplied output is its saved forward result.

add contributions=[g,g]\text{add contributions} = [g,g]add contributions=[g,g] mul contributions=[gb,ga]\text{mul contributions} = [gb,ga]mul contributions=[gb,ga] tanh contribution=[g(1−t2)]\text{tanh contribution} = [g(1-t^2)]tanh contribution=[g(1−t2)]

Here a and b are the first and second inputs, g is upstream_gradient, and t is the saved tanh output. Return a Python list of 64-bit floating-point contributions, one per input in input order.

Loading visualization...

Examples

Input: operation = "add", inputs = [2.0, -3.0], output = -1.0, upstream_gradient = 4.0

Output: [4.0, 4.0]

Explanation: Both local derivatives of addition are one.

Input: operation = "mul", inputs = [-2.0, 3.0], output = -6.0, upstream_gradient = 0.5

Output: [1.5, -1.0]

Input: operation = "tanh", inputs = [1.0], output = 0.7615941559557649, upstream_gradient = 2.0

Output: [0.839949]

Hint 1

List indexing identifies the other operand in a multiplication.

Hint 2

The saved tanh output can be squared directly; no new activation call is needed.

Requirements

  • Use Python floating-point arithmetic (64-bit); NumPy is available.
  • Use the supplied saved output for the tanh derivative.
  • Leave inputs unchanged.

Constraints

  • operation is "add", "mul", or "tanh".
  • inputs contains two finite numbers for add or mul, or one for tanh.
  • output matches the operation applied to inputs; output and upstream_gradient are finite numbers.
Try Similar Problems
Topologically Sort a Computation DAGMediumRun Reverse-Mode AutodiffHardGradient-Check an Autodiff GraphHardMeasure Scalar Expression PartialsEasyFinite Difference Derivative in Python: Autograd Step 1EasyCreate a Multiplication Value NodeEasyCreate an Addition Value NodeEasyBackpropagate Through a Scalar NeuronMediumSecond-Order Partial DerivativesEasyBasic AutogradEasyDerivatives of Common Activation FunctionsEasyJacobian of a Vector-Valued PolynomialEasy

Accepts: string

Accepts: array

Accepts: number

Accepts: number

You must run your code first.

Apply Local Vector-Jacobian Rules

Calculus
Easy

Compute the input-gradient contributions for one saved scalar operation: add and mul take two inputs, while tanh takes one. The supplied output is its saved forward result.

add contributions=[g,g]\text{add contributions} = [g,g]add contributions=[g,g] mul contributions=[gb,ga]\text{mul contributions} = [gb,ga]mul contributions=[gb,ga] tanh contribution=[g(1−t2)]\text{tanh contribution} = [g(1-t^2)]tanh contribution=[g(1−t2)]

Here a and b are the first and second inputs, g is upstream_gradient, and t is the saved tanh output. Return a Python list of 64-bit floating-point contributions, one per input in input order.

Loading visualization...

Examples

Input: operation = "add", inputs = [2.0, -3.0], output = -1.0, upstream_gradient = 4.0

Output: [4.0, 4.0]

Explanation: Both local derivatives of addition are one.

Input: operation = "mul", inputs = [-2.0, 3.0], output = -6.0, upstream_gradient = 0.5

Output: [1.5, -1.0]

Input: operation = "tanh", inputs = [1.0], output = 0.7615941559557649, upstream_gradient = 2.0

Output: [0.839949]

Hint 1

List indexing identifies the other operand in a multiplication.

Hint 2

The saved tanh output can be squared directly; no new activation call is needed.

Requirements

  • Use Python floating-point arithmetic (64-bit); NumPy is available.
  • Use the supplied saved output for the tanh derivative.
  • Leave inputs unchanged.

Constraints

  • operation is "add", "mul", or "tanh".
  • inputs contains two finite numbers for add or mul, or one for tanh.
  • output matches the operation applied to inputs; output and upstream_gradient are finite numbers.
Try Similar Problems
Topologically Sort a Computation DAGMediumRun Reverse-Mode AutodiffHardGradient-Check an Autodiff GraphHardMeasure Scalar Expression PartialsEasyFinite Difference Derivative in Python: Autograd Step 1EasyCreate a Multiplication Value NodeEasyCreate an Addition Value NodeEasyBackpropagate Through a Scalar NeuronMediumSecond-Order Partial DerivativesEasyBasic AutogradEasyDerivatives of Common Activation FunctionsEasyJacobian of a Vector-Valued PolynomialEasy

Accepts: string

Accepts: array

Accepts: number

Accepts: number

You must run your code first.