MCPcopy Create free account
hub / github.com/modAL-python/modAL / _SVM_loss

Function _SVM_loss

modAL/multilabel.py:12–41  ·  view source on GitHub ↗

Utility function for max_loss and mean_max_loss strategies. Args: multiclass_classifier: sklearn.multiclass.OneVsRestClassifier instance for which the loss is to be calculated. X: The pool of samples to query from. most_certain_classes: optional, indexes

(multiclass_classifier: ActiveLearner,
              X: modALinput, most_certain_classes: Optional[int] = None)

Source from the content-addressed store, hash-verified

10
11
12def _SVM_loss(multiclass_classifier: ActiveLearner,
13 X: modALinput, most_certain_classes: Optional[int] = None) -> np.ndarray:
14 """
15 Utility function for max_loss and mean_max_loss strategies.
16
17 Args:
18 multiclass_classifier: sklearn.multiclass.OneVsRestClassifier instance for which the loss
19 is to be calculated.
20 X: The pool of samples to query from.
21 most_certain_classes: optional, indexes of most certainly predicted class for each instance.
22 If None, loss is calculated for all classes.
23
24 Returns:
25 np.ndarray of shape (n_instances, ), losses for the instances in X.
26
27 """
28 predictions = 2*multiclass_classifier.predict(X)-1
29 n_classes = len(multiclass_classifier.classes_)
30
31 if most_certain_classes is None:
32 cls_mtx = 2*np.eye(n_classes, n_classes) - 1
33 loss_mtx = np.maximum(1-np.dot(predictions, cls_mtx), 0)
34 return loss_mtx.mean(axis=1)
35 else:
36 cls_mtx = -np.ones(shape=(len(X), n_classes))
37 for inst_idx, most_certain_class in enumerate(most_certain_classes):
38 cls_mtx[inst_idx, most_certain_class] = 1
39
40 cls_loss = np.maximum(1 - np.multiply(cls_mtx, predictions), 0).sum(axis=1)
41 return cls_loss
42
43
44def SVM_binary_minimum(classifier: ActiveLearner, X_pool: modALinput,

Callers 2

max_lossFunction · 0.85
mean_max_lossFunction · 0.85

Calls 1

predictMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…