code: def batch_nested_tensors(tensors, specs=None):
      """Add batch dimension if needed to nested tensors while checking their specs.
  
      If specs is None, a batch dimension is added to each tensor.
      If specs are provided, each tensor is compared to the corresponding spec,
      and a batch dimension is added only if the tensor doesn't already have it.
  
      For each tensor, it checks the dimensions with respect to specs, and adds an
      extra batch dimension if it doesn't already have it.
  
      Args:
        tensors: Nested list/tuple or dict of Tensors.
        specs: Nested list/tuple or dict of TensorSpecs, describing the shape of the
          non-batched Tensors.
  
      Returns:
        A nested batched version of each tensor.
      Raises:
        ValueError: if the tensors and specs have incompatible dimensions or shapes.
      """
      if specs is None:
          return tf.nest.map_structure(
              lambda x: composite.expand_dims(x, 0), tensors
          )
  
      assert_same_structure(
          tensors,
          specs,
          message="Tensors and specs do not have matching structures",
      )
  
      flat_tensors = tf.nest.flatten(tensors)
      flat_shapes = [spec_shape(s) for s in tf.nest.flatten(specs)]
      batched_tensors = []
  
      tensor_rank = lambda tensor: tensor.shape.rank
      for tensor, shape in zip(flat_tensors, flat_shapes):
          if tensor_rank(tensor) == shape.rank:
              tensor.shape.assert_is_compatible_with(shape)
              tensor = composite.expand_dims(tensor, 0)
          elif tensor_rank(tensor) == shape.rank + 1:
              tensor.shape[1:].assert_is_compatible_with(shape)
          else:
              raise ValueError(
                  "Tensor does not have the correct dimensions. "
                  "tensor.shape {} expected shape {}".format(
                      tensor.shape, shape
                  )
              )
          batched_tensors.append(tensor)
      return tf.nest.pack_sequence_as(tensors, batched_tensors)
  
imports: from __future__ import absolute_import
  from __future__ import division
  from __future__ import print_function
  import collections
  import numbers
  from typing import Optional, Text
  from absl import logging
  import numpy
  from six.moves import zip
  import tensorflow
  from tf_agents.typing import types
  from tf_agents.utils import composite
  import wrapt
  from tensorflow.python.util import nest
label_desc: Use image node_id 14 for calling a global function with example usage: batch_nested_tensors(tensors, specs) and returns: tf, tf