TensorTonicTensorTonic
Problems
Study PlansProjectsNewInterviewPricingFeedback
Problems
Loading...
1 / 1

Implement Hinge Loss (Binary SVM)

Loss Functions
Easy

Compute binary hinge loss from labels in {−1,+1}\{-1,+1\}{−1,+1} and real-valued prediction scores:

ℓi=max⁡(0,m−yisi)\ell_i = \max(0, m-y_is_i)ℓi​=max(0,m−yi​si​)

Here, yiy_iyi​ is the label, sis_isi​ is its prediction score, mmm is the margin, and ℓi\ell_iℓi​ is the sample loss. Return the mean of the sample losses when reduction="mean" or their sum when reduction="sum". The result must be a Python float.

Loading visualization...

Examples

Input: y_true = [1, 1, -1], y_score = [2, 0, 0], margin = 1.0, reduction = "mean"

Output: 0.666667

Explanation: The sample losses are [0, 1, 1], whose mean is 2/3.

Input: y_true = [-1, 1], y_score = [-3, 0.5], margin = 1.0, reduction = "mean"

Output: 0.25

Hint 1

Use np.maximum(0.0, margin - y_true * y_score) to compute all sample losses.

Hint 2

Finish with .mean() or .sum() according to reduction.

Requirements

  • Compute every sample loss with NumPy
  • Support mean and sum reduction
  • Return a Python float

Constraints

  • y_true and y_score are equal-length, nonempty one-dimensional numeric lists
  • Every label is -1 or 1
  • reduction is mean or sum
  • Use NumPy only
Try Similar Problems
Cross Entropy LossHuber LossMean Squared ErrorLogistic Regression TrainingTriplet Loss

Sign in to take notes on this problem

Case 1
Case 2

Accepts: array

Accepts: array

Accepts: number

Accepts: string

You must run your code first.
PrevNext

Implement Hinge Loss (Binary SVM)

Loss Functions
Easy

Compute binary hinge loss from labels in {−1,+1}\{-1,+1\}{−1,+1} and real-valued prediction scores:

ℓi=max⁡(0,m−yisi)\ell_i = \max(0, m-y_is_i)ℓi​=max(0,m−yi​si​)

Here, yiy_iyi​ is the label, sis_isi​ is its prediction score, mmm is the margin, and ℓi\ell_iℓi​ is the sample loss. Return the mean of the sample losses when reduction="mean" or their sum when reduction="sum". The result must be a Python float.

Loading visualization...

Examples

Input: y_true = [1, 1, -1], y_score = [2, 0, 0], margin = 1.0, reduction = "mean"

Output: 0.666667

Explanation: The sample losses are [0, 1, 1], whose mean is 2/3.

Input: y_true = [-1, 1], y_score = [-3, 0.5], margin = 1.0, reduction = "mean"

Output: 0.25

Hint 1

Use np.maximum(0.0, margin - y_true * y_score) to compute all sample losses.

Hint 2

Finish with .mean() or .sum() according to reduction.

Requirements

  • Compute every sample loss with NumPy
  • Support mean and sum reduction
  • Return a Python float

Constraints

  • y_true and y_score are equal-length, nonempty one-dimensional numeric lists
  • Every label is -1 or 1
  • reduction is mean or sum
  • Use NumPy only
Try Similar Problems
Cross Entropy LossHuber LossMean Squared ErrorLogistic Regression TrainingTriplet Loss

Sign in to take notes on this problem

Case 1
Case 2

Accepts: array

Accepts: array

Accepts: number

Accepts: string

You must run your code first.