class_docstr: Pipeline component to label spans of text.
  
  DOCS: https://spacy.io/api/spancategorizer
class_docstr_tok: ["Pipeline","component","to","label","spans","of","text",".","DOCS",":","https",":","\/\/spacy.io\/api\/spancategorizer"]
docstr: Find the loss and gradient of loss for the batch of documents and
  their predicted scores.
  
  examples (Iterable[Examples]): The batch of examples.
  spans_scores: Scores representing the model's predictions.
  RETURNS (Tuple[float, float]): The loss and the gradient.
  
  DOCS: https://spacy.io/api/spancategorizer#get_loss
docstr_tok: ["Find","the","loss","and","gradient","of","loss","for","the","batch","of","documents","and","their","predicted","scores",".","examples","(","Iterable","[","Examples","]",")",":","The","batch","of","examples",".","spans_scores",":","Scores","representing","the","model","'s","predictions",".","RETURNS","(","Tuple","[","float",",","float","]",")",":","The","loss","and","the","gradient",".","DOCS",":","https",":","\/\/spacy.io\/api\/spancategorizer","#","get_loss"]
code: def get_loss(
      self,
      examples: Iterable[Example],
      spans_scores: Tuple[Ragged, Floats2d],
  ) -> Tuple[float, float]:
      """Find the loss and gradient of loss for the batch of documents and
      their predicted scores.
  
      examples (Iterable[Examples]): The batch of examples.
      spans_scores: Scores representing the model's predictions.
      RETURNS (Tuple[float, float]): The loss and the gradient.
  
      DOCS: https://spacy.io/api/spancategorizer#get_loss
      """
      spans, scores = spans_scores
      spans = Ragged(
          self.model.ops.to_numpy(spans.data),
          self.model.ops.to_numpy(spans.lengths),
      )
      target = numpy.zeros(scores.shape, dtype=scores.dtype)
      if self.add_negative_label:
          negative_spans = numpy.ones((scores.shape[0]))
      offset = 0
      label_map = self._label_map
      for i, eg in enumerate(examples):
          # Map (start, end) offset of spans to the row in the d_scores array,
          # so that we can adjust the gradient for predictions that were
          # in the gold standard.
          spans_index = {}
          spans_i = spans[i].dataXd
          for j in range(spans.lengths[i]):
              start = int(spans_i[j, 0])  # type: ignore
              end = int(spans_i[j, 1])  # type: ignore
              spans_index[(start, end)] = offset + j
          for gold_span in self._get_aligned_spans(eg):
              key = (gold_span.start, gold_span.end)
              if key in spans_index:
                  row = spans_index[key]
                  k = label_map[gold_span.label_]
                  target[row, k] = 1.0
                  if self.add_negative_label:
                      # delete negative label target.
                      negative_spans[row] = 0.0
          # The target is a flat array for all docs. Track the position
          # we're at within the flat array.
          offset += spans.lengths[i]
      target = self.model.ops.asarray(target, dtype="f")  # type: ignore
      if self.add_negative_label:
          negative_samples = numpy.nonzero(negative_spans)[0]
          target[negative_samples, self._negative_label_i] = 1.0  # type: ignore
      # The target will have the values 0 (for untrue predictions) or 1
      # (for true predictions).
      # The scores should be in the range [0, 1].
      # If the prediction is 0.9 and it's true, the gradient
      # will be -0.1 (0.9 - 1.0).
      # If the prediction is 0.9 and it's false, the gradient will be
      # 0.9 (0.9 - 0.0)
      d_scores = scores - target
      if self.add_negative_label:
          neg_weight = cast(float, self.cfg["negative_weight"])
          if neg_weight != 1.0:
              d_scores[negative_samples] *= neg_weight
      loss = float((d_scores**2).sum())
      return loss, d_scores
  
imports: from dataclasses import dataclass
  from functools import partial
  from typing import Any, Callable, Dict, Iterable, List, Optional, Tuple, Union, cast
  import numpy
  from thinc.api import Config, Model, Ops, Optimizer, get_current_ops, set_dropout_rate
  from thinc.types import Floats2d, Ints1d, Ints2d, Ragged
  from ..compat import Protocol, runtime_checkable
  from ..errors import Errors
  from ..language import Language
  from ..scorer import Scorer
  from ..tokens import Doc, Span, SpanGroup
  from ..training import Example, validate_examples
  from ..util import registry
  from ..vocab import Vocab
  from .trainable_pipe import TrainablePipe
label_desc: Use image node_id 14 for calling the SpanCategorizer obj's underlying member method code with example usage: obj.get_loss(examples, spans_scores) and returns: loss,  d_scores