TensorTonicTensorTonic
Problems
Study PlansProjectsInterviewPricingFeedback
Study Plan
Gradient-Check a Scalar Neuron
Neural NetworksCalculus
Medium

Compare a tanh neuron's analytic weight and bias gradients with forward finite differences.

y=tanh⁡(∑i=1nwixi+b)y = \tanh\left(\sum_{i=1}^{n} w_i x_i + b\right)y=tanh(i=1∑n​wi​xi​+b) ∂y∂wi=(1−y2)xi\frac{\partial y}{\partial w_i} = (1-y^2)x_i∂wi​∂y​=(1−y2)xi​ ∂y∂b=1−y2\frac{\partial y}{\partial b} = 1-y^2∂b∂y​=1−y2 ginum=y(wi+h)−y(wi)hg_i^{\mathrm{num}} = \frac{y(w_i+h)-y(w_i)}{h}ginum​=hy(wi​+h)−y(wi​)​ gbnum=y(b+h)−y(b)hg_b^{\mathrm{num}} = \frac{y(b+h)-y(b)}{h}gbnum​=hy(b+h)−y(b)​

Here n is the vector length, x is inputs, w is weights, i indexes their entries, b is bias, y is the output, and h is the positive perturbation step. Each numerical gradient changes only the indicated parameter; every other value remains fixed.

Return a tuple of five PyTorch float64 tensors on the input device: analytic weight gradients, numerical weight gradients, analytic bias gradient, numerical bias gradient, and maximum absolute disagreement across all weight and bias gradients. The first two are vectors matching weights; the last three are scalars. With no weights, the maximum disagreement is the bias error.

Loading visualization...

Examples

Input: inputs = [1, -2], weights = [0.5, -0.25], bias = 0.1, h = 1e-6

Output: ([0.359201, -0.718403], [0.359201, -0.718404], 0.359201, 0.359201, 0.000001)

Explanation: Perturbing each parameter separately gives gradients close to the analytic values.

Input: inputs = [3, -4], weights = [2, -2], bias = 4, h = 1e-6

Output: ([0.0, 0.0], [0.0, 0.0], 0.0, 0.0, 0.0)

Input: inputs = [], weights = [], bias = -0.5, h = 1e-6

Output: ([], [], 0.786448, 0.786448, 0.0)

Hint 1

Tensor.to can create float64 working tensors.

Hint 2

Tensor.clone keeps separate perturbations independent.

Hint 3

torch.abs measures disagreement; an empty weight vector leaves only the bias error.

Requirements

  • Use PyTorch float64 for the calculations and every returned tensor.
  • Compute gradients manually, without automatic differentiation.
  • Use forward differences, not central differences.

Constraints

  • inputs and weights are finite one-dimensional floating-point tensors of equal length, which may be zero.
  • bias is a finite zero-dimensional floating-point tensor; all tensors share a device.
  • h is a finite positive real number.
Try Similar Problems
Backpropagate Through a Scalar NeuronMediumTopologically Sort a Computation DAGMediumAccumulate Shared-Path GradientsMediumTrace a Reachable Expression GraphMediumBuild a Scalar Expression GraphMediumCreate a Multiplication Value NodeEasyRun Reverse-Mode AutodiffHardCreate an Addition Value NodeEasyChain Rule Through a 3-Layer NetworkMediumBackpropagation Through a Computation GraphHardJacobian of a Neural Network LayerHardBasic AutogradEasy

Accepts: array

Accepts: array

Accepts: number

Accepts: number

You must run your code first.

Gradient-Check a Scalar Neuron

Neural NetworksCalculus
Medium

Compare a tanh neuron's analytic weight and bias gradients with forward finite differences.

y=tanh⁡(∑i=1nwixi+b)y = \tanh\left(\sum_{i=1}^{n} w_i x_i + b\right)y=tanh(i=1∑n​wi​xi​+b) ∂y∂wi=(1−y2)xi\frac{\partial y}{\partial w_i} = (1-y^2)x_i∂wi​∂y​=(1−y2)xi​ ∂y∂b=1−y2\frac{\partial y}{\partial b} = 1-y^2∂b∂y​=1−y2 ginum=y(wi+h)−y(wi)hg_i^{\mathrm{num}} = \frac{y(w_i+h)-y(w_i)}{h}ginum​=hy(wi​+h)−y(wi​)​ gbnum=y(b+h)−y(b)hg_b^{\mathrm{num}} = \frac{y(b+h)-y(b)}{h}gbnum​=hy(b+h)−y(b)​

Here n is the vector length, x is inputs, w is weights, i indexes their entries, b is bias, y is the output, and h is the positive perturbation step. Each numerical gradient changes only the indicated parameter; every other value remains fixed.

Return a tuple of five PyTorch float64 tensors on the input device: analytic weight gradients, numerical weight gradients, analytic bias gradient, numerical bias gradient, and maximum absolute disagreement across all weight and bias gradients. The first two are vectors matching weights; the last three are scalars. With no weights, the maximum disagreement is the bias error.

Loading visualization...

Examples

Input: inputs = [1, -2], weights = [0.5, -0.25], bias = 0.1, h = 1e-6

Output: ([0.359201, -0.718403], [0.359201, -0.718404], 0.359201, 0.359201, 0.000001)

Explanation: Perturbing each parameter separately gives gradients close to the analytic values.

Input: inputs = [3, -4], weights = [2, -2], bias = 4, h = 1e-6

Output: ([0.0, 0.0], [0.0, 0.0], 0.0, 0.0, 0.0)

Input: inputs = [], weights = [], bias = -0.5, h = 1e-6

Output: ([], [], 0.786448, 0.786448, 0.0)

Hint 1

Tensor.to can create float64 working tensors.

Hint 2

Tensor.clone keeps separate perturbations independent.

Hint 3

torch.abs measures disagreement; an empty weight vector leaves only the bias error.

Requirements

  • Use PyTorch float64 for the calculations and every returned tensor.
  • Compute gradients manually, without automatic differentiation.
  • Use forward differences, not central differences.

Constraints

  • inputs and weights are finite one-dimensional floating-point tensors of equal length, which may be zero.
  • bias is a finite zero-dimensional floating-point tensor; all tensors share a device.
  • h is a finite positive real number.
Try Similar Problems
Backpropagate Through a Scalar NeuronMediumTopologically Sort a Computation DAGMediumAccumulate Shared-Path GradientsMediumTrace a Reachable Expression GraphMediumBuild a Scalar Expression GraphMediumCreate a Multiplication Value NodeEasyRun Reverse-Mode AutodiffHardCreate an Addition Value NodeEasyChain Rule Through a 3-Layer NetworkMediumBackpropagation Through a Computation GraphHardJacobian of a Neural Network LayerHardBasic AutogradEasy

Accepts: array

Accepts: array

Accepts: number

Accepts: number

You must run your code first.