(
        self,
        examples: Iterable[Example],
        *,
        drop: float = 0.0,
        sgd: Optional[Optimizer] = None,
        losses: Optional[Dict[str, float]] = None,
    )
return losses
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
Prepare for an update to the transformer.
Prepare for an update to the transformer .
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 = []
        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:
                    # type: ignore
                    losses[self.name] += float((d_tensor ** 2).sum())
                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)
            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
def update ( self , examples : Iterable [ Example ] , * , drop : float = 0.0 , sgd : Optional [ Optimizer ] = None , losses : Optional [ Dict [ str , float ] ] = None , ) -> Dict [ str , float ] : 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 = [ ] 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 : # type: ignore losses [ self . name ] += float ( ( d_tensor ** 2 ) . sum ( ) ) 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 ) 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