skorch.scoring¶
Custom scoring functions
-
skorch.scoring.loss_scoring(net, X, y=None, sample_weight=None)[source]¶ Calculate score using the criterion of the net
Use the exact same logic as during model training to calculate the score.
This function can be used to implement the
scoremethod for aNeuralNetthrough sub-classing. This is useful, for example, when combining skorch models with sklearn objects that rely on the model’sscoremethod. For example:>>> class ScoredNet(skorch.NeuralNetClassifier): ... def score(self, X, y=None): ... return loss_scoring(self, X, y)
Parameters: - net : skorch.NeuralNet
A fitted Skorch
NeuralNetobject.- X : input data, compatible with skorch.dataset.Dataset
By default, you should be able to pass:
- numpy arrays
- torch tensors
- pandas DataFrame or Series
- scipy sparse CSR matrices
- a dictionary of the former three
- a list/tuple of the former three
- a Dataset
If this doesn’t work with your data, you have to pass a
Datasetthat can deal with the data.- y : target data, compatible with skorch.dataset.Dataset
The same data types as for
Xare supported. If your X is a Dataset that contains the target,ymay be set to None.- sample_weight : array-like of shape (n_samples,)
Sample weights.
Returns: - loss_value : float32 or np.ndarray
Return type depends on
net.criterion_.reduction, and will be a float if reduction is'sum'or'mean'. If reduction is'none'then this function returns anp.ndarrayobject.