question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[BUG] hinge_loss multi

See original GitHub issue
>>> sklearn.metrics.hinge_loss(y_true=[2,1,0,1,0,1,1], pred_decision=[0,1,2,1,0,2,1])

  2378         y_true = le.transform(y_true)
   2379         mask = np.ones_like(pred_decision, dtype=bool)
-> 2380         mask[np.arange(y_true.shape[0]), y_true] = False
   2381         margin = pred_decision[~mask]
   2382         margin -= np.max(pred_decision[mask].reshape(y_true.shape[0], -1),

IndexError: too many indices for array: array is 1-dimensional, but 2 were indexed

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
PierreAttardcommented, Mar 8, 2021

Ok, so I take and raise an Error if the shape of pred_decision is not consistent with y_true (or labels) like said @thomasjpfan.

0reactions
NicolasHugcommented, Mar 8, 2021

No, the hinge loss requires the real-valued decision function, not the predicted classes

    In the multiclass case:

    >>> import numpy as np
    >>> X = np.array([[0], [1], [2], [3]])
    >>> Y = np.array([0, 1, 2, 3])
    >>> labels = np.array([0, 1, 2, 3])
    >>> est = svm.LinearSVC()
    >>> est.fit(X, Y)
    LinearSVC()
    >>> pred_decision = est.decision_function([[-1], [2], [3]])
    >>> y_true = [0, 2, 3]
    >>> hinge_loss(y_true, pred_decision, labels=labels)
    0.56...

where pred_decision is

In [3]: pred_decision
Out[3]:
array([[ 1.27271904,  0.03418064, -0.68377391, -1.40170867],
       [-1.45454381, -0.58121055, -0.37602669, -0.17093709],
       [-2.36363142, -0.78634095, -0.27344428,  0.23932011]]
Read more comments on GitHub >

github_iconTop Results From Across the Web

4. Hinge Loss/Multi-class SVM Loss - YouTube
Hinge Loss / Multi -class SVM Loss is used for maximum-margin classification, especially for support vector machines or SVM. Hinge loss at ...
Read more >
How can I use Hinge loss for Multi-class Support Vector ...
I have coded a Multi-class SVM classifier using Hinge loss as loss function. I worked on the knowledge in this article: ...
Read more >
How to Choose Loss Functions When Training Deep Learning ...
Binary Classification Loss Functions. Binary Cross-Entropy; Hinge Loss; Squared Hinge Loss. Multi-Class Classification Loss Functions.
Read more >
Categorical hinge loss · Issue #2830 · keras-team/keras - GitHub
If the hinge loss were multiclass (=multiple classes, single correct label) in keras then it should implement:.
Read more >
How to implement multi-class hinge loss in tensorflow
I find it difficult to get the second max prediction probability when the prediction is correct. I tried to use tf.nn.top_k to calculate...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found