class_docstr: spaCy pipeline component that provides access to a transformer model from
  the Huggingface transformers library. Usually you will connect subsequent
  components to the shared transformer using the TransformerListener layer.
  This works similarly to spaCy's Tok2Vec component and Tok2VecListener
  sublayer.
  
  The activations from the transformer are saved in the doc._.trf_data extension
  attribute. You can also provide a callback to set additional annotations.
  
  vocab (Vocab): The Vocab object for the pipeline.
  model (Model[List[Doc], FullTransformerBatch]): A thinc Model object wrapping
      the transformer. Usually you will want to use the TransformerModel
      layer for this.
  set_extra_annotations (Callable[[List[Doc], FullTransformerBatch], None]): A
      callback to set additional information onto the batch of `Doc` objects.
      The doc._.trf_data attribute is set prior to calling the callback.
      By default, no additional annotations are set.
docstr: Prepare for an update to the transformer.
  
  Like the `Tok2Vec` component, the `Transformer` component is unusual
  in that it does not receive "gold standard" annotations to calculate
  a weight update. The optimal output of the transformer data is unknown;
  it's a hidden layer inside the network that is updated by backpropagating
  from output layers.
  
  The `Transformer` component therefore does not perform a weight update
  during its own `update` method. Instead, it runs its transformer model
  and communicates the output and the backpropagation callback to any
  downstream components that have been connected to it via the
  TransformerListener sublayer. If there are multiple listeners, the last
  layer will actually backprop to the transformer and call the optimizer,
  while the others simply increment the gradients.
  
  examples (Iterable[Example]):
      A batch of Example objects. Only the `predicted` doc object is used,
      the reference doc is ignored.
  drop (float): The dropout rate.
  sgd (thinc.api.Optimizer): The optimizer.
  losses (Dict[str, float]): Optional record of the loss during training.
      Updated using the component name as the key.
  RETURNS (Dict[str, float]): The updated losses dictionary.
  
  DOCS: https://spacy.io/api/transformer#update
docstr_tok: ["Prepare","for","an","update","to","the","transformer",".","Like","the","`","Tok2Vec","`","component",",","the","`","Transformer","`","component","is","unusual","in","that","it","does","not","receive","``","gold","standard","''","annotations","to","calculate","a","weight","update",".","The","optimal","output","of","the","transformer","data","is","unknown",";","it","'s","a","hidden","layer","inside","the","network","that","is","updated","by","backpropagating","from","output","layers",".","The","`","Transformer","`","component","therefore","does","not","perform","a","weight","update","during","its","own","`","update","`","method",".","Instead",",","it","runs","its","transformer","model","and","communicates","the","output","and","the","backpropagation","callback","to","any","downstream","components","that","have","been","connected","to","it","via","the","TransformerListener","sublayer",".","If","there","are","multiple","listeners",",","the","last","layer","will","actually","backprop","to","the","transformer","and","call","the","optimizer",",","while","the","others","simply","increment","the","gradients",".","examples","(","Iterable","[","Example","]",")",":","A","batch","of","Example","objects",".","Only","the","`","predicted","`","doc","object","is","used",",","the","reference","doc","is","ignored",".","drop","(","float",")",":","The","dropout","rate",".","sgd","(","thinc.api.Optimizer",")",":","The","optimizer",".","losses","(","Dict","[","str",",","float","]",")",":","Optional","record","of","the","loss","during","training",".","Updated","using","the","component","name","as","the","key",".","RETURNS","(","Dict","[","str",",","float","]",")",":","The","updated","losses","dictionary",".","DOCS",":","https",":","\/\/spacy.io\/api\/transformer","#","update"]
code: def update(
      self,
      examples: Iterable[Example],
      *,
      drop: float = 0.0,
      sgd: Optional[Optimizer] = None,
      losses: Optional[Dict[str, float]] = None,
  ) -> Dict[str, float]:
      """Prepare for an update to the transformer.
  
      Like the `Tok2Vec` component, the `Transformer` component is unusual
      in that it does not receive "gold standard" annotations to calculate
      a weight update. The optimal output of the transformer data is unknown;
      it's a hidden layer inside the network that is updated by backpropagating
      from output layers.
  
      The `Transformer` component therefore does not perform a weight update
      during its own `update` method. Instead, it runs its transformer model
      and communicates the output and the backpropagation callback to any
      downstream components that have been connected to it via the
      TransformerListener sublayer. If there are multiple listeners, the last
      layer will actually backprop to the transformer and call the optimizer,
      while the others simply increment the gradients.
  
      examples (Iterable[Example]):
          A batch of Example objects. Only the `predicted` doc object is used,
          the reference doc is ignored.
      drop (float): The dropout rate.
      sgd (thinc.api.Optimizer): The optimizer.
      losses (Dict[str, float]): Optional record of the loss during training.
          Updated using the component name as the key.
      RETURNS (Dict[str, float]): The updated losses dictionary.
  
      DOCS: https://spacy.io/api/transformer#update
      """
      validate_examples(examples, "Transformer.update")
      if losses is None:
          losses = {}
      docs = [eg.predicted for eg in examples]
      if isinstance(docs, Doc):
          docs = [docs]
      if not any(len(doc) for doc in docs):
          # Handle cases where there are no tokens in any docs.
          return losses
      set_dropout_rate(self.model, drop)
      trf_full, bp_trf_full = self.model.begin_update(docs)
      d_tensors: List = []
      losses.setdefault(self.name, 0.0)
  
      def accumulate_gradient(d_trf_datas: List[TransformerData]):
          """Accumulate tok2vec loss and gradient. This is passed as a callback
          to all but the last listener. Only the last one does the backprop.
          """
          nonlocal d_tensors
          for i, d_trf_data in enumerate(d_trf_datas):
              for d_tensor in d_trf_data.tensors:
                  losses[self.name] += float(
                      (d_tensor**2).sum()
                  )  # type:ignore
              if i >= len(d_tensors):
                  d_tensors.append(list(d_trf_data.tensors))
              else:
                  for j, d_tensor in enumerate(d_trf_data.tensors):
                      d_tensors[i][j] += d_tensor
  
      def backprop(d_trf_datas: List[TransformerData]):
          """Callback to actually do the backprop. Passed to last listener."""
          nonlocal d_tensors
          accumulate_gradient(d_trf_datas)
          d_trf_full = trf_full.unsplit_by_doc(d_tensors)
          d_docs = bp_trf_full(d_trf_full)  # type: ignore
          if sgd is not None:
              self.model.finish_update(sgd)
          d_tensors = []
          return d_docs
  
      batch_id = TransformerListener.get_batch_id(docs)
      for listener in self.listeners[:-1]:
          listener.receive(
              batch_id, trf_full.doc_data, accumulate_gradient
          )
      if self.listeners:
          self.listeners[-1].receive(
              batch_id, trf_full.doc_data, backprop
          )
      return losses
  
imports: from typing import List, Callable, Iterable, Iterator, Optional, Dict, Union
  import warnings
  from spacy.language import Language
  from spacy.pipeline.trainable_pipe import TrainablePipe
  from spacy.pipeline.pipe import deserialize_config
  from spacy.tokens import Doc
  from spacy.vocab import Vocab
  from spacy.training import Example, validate_examples
  from spacy import util, Errors
  from spacy.util import minibatch
  from thinc.api import Model, Config, set_dropout_rate, Optimizer
  import srsly
  from pathlib import Path
  from .layers.transformer_model import huggingface_from_pretrained
  from .util import batch_by_length
  from .annotation_setters import null_annotation_setter
  from .data_classes import FullTransformerBatch, TransformerData
  from .layers import TransformerListener
label_desc: Use image node_id 11 for calling the Transformer obj's underlying member method code with example usage: obj.update(examples) and returns: losses, losses, d_docs