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 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)
Parameters:
net : skorch.NeuralNet

A fitted Skorch NeuralNet object.

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

y : target 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_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 a np.ndarray object.