parameters: (*shapes: Shape, check_exact: tuple or list = (), order=(batch, instance, spatial, channel))
return_statement: return concat_shapes(*merged)
docstring: Combines `shapes` into a single `Shape`, grouping dimensions by type.
      If dimensions with equal names are present in multiple shapes, their types and sizes must match.
  
      The shorthand `shape1 & shape2` merges shapes with `check_exact=[spatial]`.
  
      See Also:
          `concat_shapes()`.
  
      Args:
          *shapes: `Shape` objects to combine.
          check_exact: Sequence of type filters, such as `channel`, `batch`, `spatial` or `instance`.
              These types are checked for exact match, i.e. shapes must either contain all dimensions of that type or none.
              The order of the dimensions does not matter.
              For example, when checking `spatial`, the shapes `spatial(x=5)` and `spatial(y=4)` cannot be combined.
              However, `spatial(x=5, y=4)` can be combined with `spatial(y=4, x=5)` and `channel('vector')`.
          order: Dimension type order as `tuple` of type filters (`channel`, `batch`, `spatial` or `instance`). Dimensions are grouped by type while merging.
  
      Returns:
          Merged `Shape`
docstring_summary: Combines `shapes` into a single `Shape`, grouping dimensions by type.
      If dimensions with equal names are present in multiple shapes, their types and sizes must match.
docstring_tokens: Combines shapes into a single Shape grouping dimensions by type . If dimensions with equal names are present in multiple shapes their types and sizes must match .
function: def merge_shapes(*shapes: Shape, check_exact: tuple or list = (), order=(batch, instance, spatial, channel)):
      """
      Combines `shapes` into a single `Shape`, grouping dimensions by type.
      If dimensions with equal names are present in multiple shapes, their types and sizes must match.
  
      The shorthand `shape1 & shape2` merges shapes with `check_exact=[spatial]`.
  
      See Also:
          `concat_shapes()`.
  
      Args:
          *shapes: `Shape` objects to combine.
          check_exact: Sequence of type filters, such as `channel`, `batch`, `spatial` or `instance`.
              These types are checked for exact match, i.e. shapes must either contain all dimensions of that type or none.
              The order of the dimensions does not matter.
              For example, when checking `spatial`, the shapes `spatial(x=5)` and `spatial(y=4)` cannot be combined.
              However, `spatial(x=5, y=4)` can be combined with `spatial(y=4, x=5)` and `channel('vector')`.
          order: Dimension type order as `tuple` of type filters (`channel`, `batch`, `spatial` or `instance`). Dimensions are grouped by type while merging.
  
      Returns:
          Merged `Shape`
      """
      if not shapes:
          return EMPTY_SHAPE
      merged = []
      for dim_type in order:
          check_type_exact = dim_type in check_exact
          group = dim_type(shapes[0])
          for shape in shapes[1:]:
              shape = dim_type(shape)
              if check_type_exact:
                  if group.rank == 0:
                      group = shape
                  elif shape.rank > 0:  # check exact match
                      if shape.rank != group.rank:
                          raise IncompatibleShapes(f"Failed to combine {shapes} because a different number of {dim_type.__name__} dimensions are present but exact checks are enabled for dimensions of type {dim_type.__name__}. Try declaring all spatial dimensions in one call. Types are {[s.types for s in shapes]}", *shapes)
                      elif set(shape.names) != set(group.names):
                          raise IncompatibleShapes(f"Failed to combine {shapes} because {dim_type.__name__} dimensions do not match but exact checks were enabled for dimensions of type {dim_type.__name__}. Try declaring all spatial dimensions in one call. Types are {[s.types for s in shapes]}", *shapes)
                      elif shape._reorder(group) != group:
                          raise IncompatibleShapes(f"Failed to combine {shapes} because {dim_type.__name__} dimensions do not match but exact checks were enabled for dimensions of type {dim_type.__name__}. Try declaring all spatial dimensions in one call. Types are {[s.types for s in shapes]}", *shapes)
              else:
                  for dim in shape:
                      if dim not in group:
                          group = group._expand(dim, pos=-1)
                      else:  # check size match
                          if not _size_equal(dim.size, group.get_size(dim.name)):
                              raise IncompatibleShapes(f"Cannot merge shapes {shapes} because dimension '{dim.name}' exists with different sizes.", *shapes)
          merged.append(group)
      return concat_shapes(*merged)
function_tokens: def merge_shapes ( * shapes : Shape , check_exact : tuple or list = ( ) , order = ( batch , instance , spatial , channel ) ) : if not shapes : return EMPTY_SHAPE merged = [ ] for dim_type in order : check_type_exact = dim_type in check_exact group = dim_type ( shapes [ 0 ] ) for shape in shapes [ 1 : ] : shape = dim_type ( shape ) if check_type_exact : if group . rank == 0 : group = shape elif shape . rank > 0 : # check exact match if shape . rank != group . rank : raise IncompatibleShapes ( f"Failed to combine {shapes} because a different number of {dim_type.__name__} dimensions are present but exact checks are enabled for dimensions of type {dim_type.__name__}. Try declaring all spatial dimensions in one call. Types are {[s.types for s in shapes]}" , * shapes ) elif set ( shape . names ) != set ( group . names ) : raise IncompatibleShapes ( f"Failed to combine {shapes} because {dim_type.__name__} dimensions do not match but exact checks were enabled for dimensions of type {dim_type.__name__}. Try declaring all spatial dimensions in one call. Types are {[s.types for s in shapes]}" , * shapes ) elif shape . _reorder ( group ) != group : raise IncompatibleShapes ( f"Failed to combine {shapes} because {dim_type.__name__} dimensions do not match but exact checks were enabled for dimensions of type {dim_type.__name__}. Try declaring all spatial dimensions in one call. Types are {[s.types for s in shapes]}" , * shapes ) else : for dim in shape : if dim not in group : group = group . _expand ( dim , pos = - 1 ) else : # check size match if not _size_equal ( dim . size , group . get_size ( dim . name ) ) : raise IncompatibleShapes ( f"Cannot merge shapes {shapes} because dimension '{dim.name}' exists with different sizes." , * shapes ) merged . append ( group ) return concat_shapes ( * merged )