class_docstr: Interface between a TensorFlow model and a Thinc Model. This container is
  *not* a Thinc Model subclass itself.
  
  Reference for custom training:
  https://www.tensorflow.org/tutorials/customization/custom_training_walkthrough
class_docstr_tok: ["Interface","between","a","TensorFlow","model","and","a","Thinc","Model",".","This","container","is","*","not","*","a","Thinc","Model","subclass","itself",".","Reference","for","custom","training",":","https",":","\/\/www.tensorflow.org\/tutorials\/customization\/custom_training_walkthrough"]
code: def begin_update(self, X: ArgsKwargs):
      tf.keras.backend.set_learning_phase(1)
      tape = tf.GradientTape()
      tape.__enter__()
      tape.watch(X.args)  # watch the input layers
      output = self._model(*X.args, **X.kwargs)
  
      def backprop(d_output):
          # d_args[0] contains derivative of loss wrt output (d_loss/d_output)
          tape.__exit__(None, None, None)
          # We need to handle a tuple of inputs
          if len(X.args) == 1:
              wrt_tensors = [
                  X.args[0]
              ]  # add the input layer also for d_loss/d_input
          else:
              wrt_tensors = list(X.args[0])
          wrt_tensors.extend(self._model.trainable_variables)
          all_gradients = tape.gradient(
              output, wrt_tensors, output_gradients=d_output
          )
          dX = all_gradients[: len(X.args)]
          opt_grads = all_gradients[1:]
          # Accumulate gradients
          if self.gradients is not None:
              assert len(opt_grads) == len(
                  self.gradients
              ), "gradients must match"
              variable: tf.Variable
              for variable, new_variable in zip(
                  self.gradients, opt_grads
              ):
                  variable.assign_add(new_variable)
          else:
              # Create variables from the grads to allow accumulation
              self.gradients = [tf.Variable(f) for f in opt_grads]
          return ArgsKwargs(args=tuple(dX), kwargs={})
  
      return output, backprop
  
imports: import contextlib
  import copy
  from io import BytesIO
  from typing import Any, Dict, List, Optional
  import catalogue
  import numpy
  from ..backends import Ops, get_current_ops
  from ..compat import cupy, h5py
  from ..compat import tensorflow
  from ..optimizers import Optimizer
  from ..types import ArgsKwargs, ArrayXd
  from ..util import get_array_module
  from .shim import Shim
label_desc: Use image node_id 5 for calling the TensorFlowShim obj's underlying member method code with example usage: obj.begin_update(X) and returns: output,  backprop, ArgsKwargs