code: def to_undirected(  # noqa: F811
      edge_index: Tensor,
      edge_attr: Union[Optional[Tensor], List[Tensor], str] = MISSING,
      num_nodes: Optional[int] = None,
      reduce: str = "add",
  ) -> Union[
      Tensor, Tuple[Tensor, OptTensor], Tuple[Tensor, List[Tensor]]
  ]:
      r"""Converts the graph given by :attr:`edge_index` to an undirected graph
      such that :math:`(j,i) \in \mathcal{E}` for every edge :math:`(i,j) \in
      \mathcal{E}`.
  
      Args:
          edge_index (LongTensor): The edge indices.
          edge_attr (Tensor or List[Tensor], optional): Edge weights or multi-
              dimensional edge features.
              If given as a list, will remove duplicates for all its entries.
              (default: :obj:`None`)
          num_nodes (int, optional): The number of nodes, *i.e.*
              :obj:`max(edge_index) + 1`. (default: :obj:`None`)
          reduce (str, optional): The reduce operation to use for merging edge
              features (:obj:`"add"`, :obj:`"mean"`, :obj:`"min"`, :obj:`"max"`,
              :obj:`"mul"`). (default: :obj:`"add"`)
  
      :rtype: :class:`LongTensor` if :attr:`edge_attr` is not passed, else
          (:class:`LongTensor`, :obj:`Optional[Tensor]` or :obj:`List[Tensor]]`)
  
      .. warning::
  
          From :pyg:`PyG >= 2.3.0` onwards, this function will always return a
          tuple whenever :obj:`edge_attr` is passed as an argument (even in case
          it is set to :obj:`None`).
  
      Examples:
          >>> edge_index = torch.tensor([[0, 1, 1],
          ...                            [1, 0, 2]])
          >>> to_undirected(edge_index)
          tensor([[0, 1, 1, 2],
                  [1, 0, 2, 1]])
  
          >>> edge_index = torch.tensor([[0, 1, 1],
          ...                            [1, 0, 2]])
          >>> edge_weight = torch.tensor([1., 1., 1.])
          >>> to_undirected(edge_index, edge_weight)
          (tensor([[0, 1, 1, 2],
                  [1, 0, 2, 1]]),
          tensor([2., 2., 1., 1.]))
  
          >>> # Use 'mean' operation to merge edge features
          >>>  to_undirected(edge_index, edge_weight, reduce='mean')
          (tensor([[0, 1, 1, 2],
                  [1, 0, 2, 1]]),
          tensor([1., 1., 1., 1.]))
      """
      # Maintain backward compatibility to `to_undirected(edge_index, num_nodes)`
      if isinstance(edge_attr, int):
          num_nodes = edge_attr
          edge_attr = MISSING
  
      row, col = edge_index[0], edge_index[1]
      row, col = torch.cat([row, col], dim=0), torch.cat(
          [col, row], dim=0
      )
      edge_index = torch.stack([row, col], dim=0)
  
      if isinstance(edge_attr, Tensor):
          edge_attr = torch.cat([edge_attr, edge_attr], dim=0)
      elif isinstance(edge_attr, (list, tuple)):
          edge_attr = [torch.cat([e, e], dim=0) for e in edge_attr]
  
      return coalesce(edge_index, edge_attr, num_nodes, reduce)
  
file_path: pytorch_geometric/torch_geometric/utils/undirected.py
imports: import typing
  from typing import List, Optional, Tuple, Union
  import torch
  from torch import Tensor
  from torch_geometric.typing import OptTensor
  from torch_geometric.utils import coalesce, sort_edge_index
  from torch_geometric.utils.num_nodes import maybe_num_nodes
label_desc: Use image node_id 8 for calling a global function with example usage: to_undirected(edge_index, edge_attr, num_nodes, reduce) and returns: coalesce