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 score method for a NeuralNet through sub-classing. This is useful, for example, when combining skorch models with sklearn objects that rely on the model’s score method. For example:

>>> class ScoredNet(skorch.NeuralNetClassifier):
...     def score(self, X, y=None):
...         return loss_scoring(self, X, y)

A fitted Skorch NeuralNet object.

Xinput 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 Dataset that can deal with the data.

ytarget data, compatible with skorch.dataset.Dataset

The same data types as for X are supported. If your X is a Dataset that contains the target, y may be set to None.

sample_weightarray-like of shape (n_samples,)

Sample weights.

loss_valuefloat32 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 a np.ndarray object.