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)
| 10 | |
| 11 | |
| 12 | def _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 | |
| 44 | def SVM_binary_minimum(classifier: ActiveLearner, X_pool: modALinput, |
no test coverage detected
searching dependent graphs…