TensorTonicTensorTonic
Problems
Study PlansProjectsInterviewPricingFeedback
Study Plan
Calculate Squared-Error Loss Gradients
Loss Functions
Medium

Compute the summed squared-error loss and its gradient with respect to predictions, using aligned target values.

L=∑i(pi−yi)2L=\sum_i(p_i-y_i)^2L=i∑​(pi​−yi​)2 gi=2(pi−yi)g_i=2(p_i-y_i)gi​=2(pi​−yi​)

Here p is predictions, y is targets, i indexes every tensor entry, L is the total loss, and g is its gradient with respect to predictions. Do not average over the entries. Empty tensors have zero loss and an empty gradient.

Return a tuple containing a zero-dimensional loss tensor and a gradient tensor with the same shape as predictions. Both are PyTorch tensors that preserve the common input dtype and device.

Loading visualization...

Examples

Input: predictions = [1,-1], targets = [1,1]

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

Explanation: The second prediction differs from its target by -2, contributing a loss of 4 and a gradient of -4.

Input: predictions = [0.5], targets = [0.5]

Output: (0.0, [0.0])

Input: predictions = [], targets = []

Output: (0.0, [])

Hint 1

torch.sum reduces all tensor entries when no dimension is supplied.

Hint 2

Elementwise operations retain the shape of predictions.

Requirements

  • Use PyTorch without modifying predictions or targets.
  • Preserve the shared float16, float32, or float64 dtype and device; examples and custom Run inputs use float64.

Constraints

  • predictions and targets have identical shapes, including scalar or empty shapes.
  • All tensors contain finite values and share one floating-point dtype and device; noncontiguous tensors are valid.
  • The loss and gradients are finite.
Try Similar Problems
Train a Tiny Micrograd MLPHardLogistic Regression Training LoopMediumImplement Dice LossMediumImplement Contrastive Loss (Siamese)MediumImplement Triplet LossMediumImplement Cross-Entropy Loss in PythonMediumImplement Symmetric CLIP Contrastive LossMediumCompare Sequence and Token Length NormalizationMediumHigher-Order Derivatives & Loss CurvatureMediumLoss FunctionsMediumBradley-Terry Reward Model LossMediumREINFORCE with BaselineMedium

Accepts: array, number

Accepts: array, number

You must run your code first.

Calculate Squared-Error Loss Gradients

Loss Functions
Medium

Compute the summed squared-error loss and its gradient with respect to predictions, using aligned target values.

L=∑i(pi−yi)2L=\sum_i(p_i-y_i)^2L=i∑​(pi​−yi​)2 gi=2(pi−yi)g_i=2(p_i-y_i)gi​=2(pi​−yi​)

Here p is predictions, y is targets, i indexes every tensor entry, L is the total loss, and g is its gradient with respect to predictions. Do not average over the entries. Empty tensors have zero loss and an empty gradient.

Return a tuple containing a zero-dimensional loss tensor and a gradient tensor with the same shape as predictions. Both are PyTorch tensors that preserve the common input dtype and device.

Loading visualization...

Examples

Input: predictions = [1,-1], targets = [1,1]

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

Explanation: The second prediction differs from its target by -2, contributing a loss of 4 and a gradient of -4.

Input: predictions = [0.5], targets = [0.5]

Output: (0.0, [0.0])

Input: predictions = [], targets = []

Output: (0.0, [])

Hint 1

torch.sum reduces all tensor entries when no dimension is supplied.

Hint 2

Elementwise operations retain the shape of predictions.

Requirements

  • Use PyTorch without modifying predictions or targets.
  • Preserve the shared float16, float32, or float64 dtype and device; examples and custom Run inputs use float64.

Constraints

  • predictions and targets have identical shapes, including scalar or empty shapes.
  • All tensors contain finite values and share one floating-point dtype and device; noncontiguous tensors are valid.
  • The loss and gradients are finite.
Try Similar Problems
Train a Tiny Micrograd MLPHardLogistic Regression Training LoopMediumImplement Dice LossMediumImplement Contrastive Loss (Siamese)MediumImplement Triplet LossMediumImplement Cross-Entropy Loss in PythonMediumImplement Symmetric CLIP Contrastive LossMediumCompare Sequence and Token Length NormalizationMediumHigher-Order Derivatives & Loss CurvatureMediumLoss FunctionsMediumBradley-Terry Reward Model LossMediumREINFORCE with BaselineMedium

Accepts: array, number

Accepts: array, number

You must run your code first.