class_docstr: Callback that performs generic scoring on predictions.
  
  At the end of each epoch, this callback makes a prediction on
  train or validation data, determines the score for that prediction
  and whether it is the best yet, and stores the result in the net's
  history.
  
  In case you already computed a score value for each batch you
  can omit the score computation step by return the value from
  the history. For example:
  
      >>> def my_score(net, X=None, y=None):
      ...     losses = net.history[-1, 'batches', :, 'my_score']
      ...     batch_sizes = net.history[-1, 'batches', :, 'valid_batch_size']
      ...     return np.average(losses, weights=batch_sizes)
      >>> net = MyNet(callbacks=[
      ...     ('my_score', Scoring(my_score, name='my_score'))
  
  If you fit with a custom dataset, this callback should work as
  expected as long as ``use_caching=True`` which enables the
  collection of ``y`` values from the dataset. If you decide to
  disable the caching of predictions and ``y`` values, you need
  to write your own scoring function that is able to deal with the
  dataset and returns a scalar, for example:
  
      >>> def ds_accuracy(net, ds, y=None):
      ...     # assume ds yields (X, y), e.g. torchvision.datasets.MNIST
      ...     y_true = [y for _, y in ds]
      ...     y_pred = net.predict(ds)
      ...     return sklearn.metrics.accuracy_score(y_true, y_pred)
      >>> net = MyNet(callbacks=[
      ...     EpochScoring(ds_accuracy, use_caching=False)])
      >>> ds = torchvision.datasets.MNIST(root=mnist_path)
      >>> net.fit(ds)
  
  Parameters
  ----------
  scoring : None, str, or callable (default=None)
    If None, use the ``score`` method of the model. If str, it
    should be a valid sklearn scorer (e.g. "f1", "accuracy"). If a
    callable, it should have the signature (model, X, y), and it
    should return a scalar. This works analogously to the
    ``scoring`` parameter in sklearn's ``GridSearchCV`` et al.
  
  lower_is_better : bool (default=True)
    Whether lower scores should be considered better or worse.
  
  on_train : bool (default=False)
    Whether this should be called during train or validation data.
  
  name : str or None (default=None)
    If not an explicit string, tries to infer the name from the
    ``scoring`` argument.
  
  target_extractor : callable (default=to_numpy)
    This is called on y before it is passed to scoring.
  
  use_caching : bool (default=True)
    Collect labels and predictions (``y_true`` and ``y_pred``)
    over the course of one epoch and use the cached values for
    computing the score. The cached values are shared between
    all ``EpochScoring`` instances. Disabling this will result
    in an additional inference step for each epoch and an
    inability to use arbitrary datasets as input (since we
    don't know how to extract ``y_true`` from an arbitrary
    dataset). Note that the net may override the use of caching.
class_docstr_tok: ["Callback","that","performs","generic","scoring","on","predictions",".","At","the","end","of","each","epoch",",","this","callback","makes","a","prediction","on","train","or","validation","data",",","determines","the","score","for","that","prediction","and","whether","it","is","the","best","yet",",","and","stores","the","result","in","the","net's","history",".","In","case","you","already","computed","a","score","value","for","each","batch","you","can","omit","the","score","computation","step","by","return","the","value","from","the","history",".","For","example",":",">",">",">","def","my_score","(","net",",","X=None",",","y=None",")",":","...","losses","=","net.history","[","-1",",","'batches","'",",",":",",","'my_score","'","]","...","batch_sizes","=","net.history","[","-1",",","'batches","'",",",":",",","'valid_batch_size","'","]","...","return","np.average","(","losses",",","weights=batch_sizes",")",">",">",">","net","=","MyNet","(","callbacks=","[","...","(","'my_score","'",",","Scoring","(","my_score",",","name='my_score","'",")",")","If","you","fit","with","a","custom","dataset",",","this","callback","should","work","as","expected","as","long","as","``","use_caching=True","``","which","enables","the","collection","of","``","y","``","values","from","the","dataset",".","If","you","decide","to","disable","the","caching","of","predictions","and","``","y","``","values",",","you","need","to","write","your","own","scoring","function","that","is","able","to","deal","with","the","dataset","and","returns","a","scalar",",","for","example",":",">",">",">","def","ds_accuracy","(","net",",","ds",",","y=None",")",":","...","#","assume","ds","yields","(","X",",","y",")",",","e.g",".","torchvision.datasets.MNIST","...","y_true","=","[","y","for","_",",","y","in","ds","]","...","y_pred","=","net.predict","(","ds",")","...","return","sklearn.metrics.accuracy_score","(","y_true",",","y_pred",")",">",">",">","net","=","MyNet","(","callbacks=","[","...","EpochScoring","(","ds_accuracy",",","use_caching=False",")","]",")",">",">",">","ds","=","torchvision.datasets.MNIST","(","root=mnist_path",")",">",">",">","net.fit","(","ds",")","Parameters","--","--","--","--","--","scoring",":","None",",","str",",","or","callable","(","default=None",")","If","None",",","use","the","``","score","``","method","of","the","model",".","If","str",",","it","should","be","a","valid","sklearn","scorer","(","e.g",".","``","f1","''",",","``","accuracy","''",")",".","If","a","callable",",","it","should","have","the","signature","(","model",",","X",",","y",")",",","and","it","should","return","a","scalar",".","This","works","analogously","to","the","``","scoring","``","parameter","in","sklearn","'s","``","GridSearchCV","``","et","al",".","lower_is_better",":","bool","(","default=True",")","Whether","lower","scores","should","be","considered","better","or","worse",".","on_train",":","bool","(","default=False",")","Whether","this","should","be","called","during","train","or","validation","data",".","name",":","str","or","None","(","default=None",")","If","not","an","explicit","string",",","tries","to","infer","the","name","from","the","``","scoring","``","argument",".","target_extractor",":","callable","(","default=to_numpy",")","This","is","called","on","y","before","it","is","passed","to","scoring",".","use_caching",":","bool","(","default=True",")","Collect","labels","and","predictions","(","``","y_true","``","and","``","y_pred","``",")","over","the","course","of","one","epoch","and","use","the","cached","values","for","computing","the","score",".","The","cached","values","are","shared","between","all","``","EpochScoring","``","instances",".","Disabling","this","will","result","in","an","additional","inference","step","for","each","epoch","and","an","inability","to","use","arbitrary","datasets","as","input","(","since","we","do","n't","know","how","to","extract","``","y_true","``","from","an","arbitrary","dataset",")",".","Note","that","the","net","may","override","the","use","of","caching","."]
code: def on_epoch_begin(self, net, dataset_train, dataset_valid, **kwargs):
      self._initialize_cache()
  
imports: from contextlib import contextmanager
  from contextlib import suppress
  from functools import partial
  import warnings
  import numpy
  import sklearn
  from sklearn.metrics import make_scorer, check_scoring
  from skorch.callbacks import Callback
  from skorch.dataset import unpack_data
  from sklearn.metrics._scorer import _BaseScorer
  from skorch.utils import data_from_dataset
  from skorch.utils import is_skorch_dataset
  from skorch.utils import to_numpy
  from skorch.utils import check_indexing
  from skorch.utils import to_device
label_desc: Use image node_id 3 for calling the EpochScoring obj's underlying member method code with example usage: obj.on_epoch_begin(net, dataset_train, dataset_valid) without return types