class_docstr: The MetaPath2Vec model from the `"metapath2vec: Scalable Representation
  Learning for Heterogeneous Networks"
  <https://ericdongyx.github.io/papers/
  KDD17-dong-chawla-swami-metapath2vec.pdf>`_ paper where random walks based
  on a given :obj:`metapath` are sampled in a heterogeneous graph, and node
  embeddings are learned via negative sampling optimization.
  
  .. note::
  
      For an example of using MetaPath2Vec, see
      `examples/hetero/metapath2vec.py
      <https://github.com/pyg-team/pytorch_geometric/blob/master/examples/
      hetero/metapath2vec.py>`_.
  
  Args:
      edge_index_dict (Dict[Tuple[str, str, str], torch.Tensor]): Dictionary
          holding edge indices for each
          :obj:`(src_node_type, rel_type, dst_node_type)` edge type present
          in the heterogeneous graph.
      embedding_dim (int): The size of each embedding vector.
      metapath (List[Tuple[str, str, str]]): The metapath described as a list
          of :obj:`(src_node_type, rel_type, dst_node_type)` tuples.
      walk_length (int): The walk length.
      context_size (int): The actual context size which is considered for
          positive samples. This parameter increases the effective sampling
          rate by reusing samples across different source nodes.
      walks_per_node (int, optional): The number of walks to sample for each
          node. (default: :obj:`1`)
      num_negative_samples (int, optional): The number of negative samples to
          use for each positive sample. (default: :obj:`1`)
      num_nodes_dict (Dict[str, int], optional): Dictionary holding the
          number of nodes for each node type. (default: :obj:`None`)
      sparse (bool, optional): If set to :obj:`True`, gradients w.r.t. to the
          weight matrix will be sparse. (default: :obj:`False`)
class_docstr_tok: ["The","MetaPath2Vec","model","from","the","`","``","metapath2vec",":","Scalable","Representation","Learning","for","Heterogeneous","Networks","''","<","https",":","\/\/ericdongyx.github.io\/papers\/","KDD17-dong-chawla-swami-metapath2vec.pdf",">","`","_","paper","where","random","walks","based","on","a","given",":","obj",":","`","metapath","`","are","sampled","in","a","heterogeneous","graph",",","and","node","embeddings","are","learned","via","negative","sampling","optimization",".","..","note",":",":","For","an","example","of","using","MetaPath2Vec",",","see","`","examples\/hetero\/metapath2vec.py","<","https",":","\/\/github.com\/pyg-team\/pytorch_geometric\/blob\/master\/examples\/","hetero\/metapath2vec.py",">","`","_",".","Args",":","edge_index_dict","(","Dict","[","Tuple","[","str",",","str",",","str","]",",","torch.Tensor","]",")",":","Dictionary","holding","edge","indices","for","each",":","obj",":","`","(","src_node_type",",","rel_type",",","dst_node_type",")","`","edge","type","present","in","the","heterogeneous","graph",".","embedding_dim","(","int",")",":","The","size","of","each","embedding","vector",".","metapath","(","List","[","Tuple","[","str",",","str",",","str","]","]",")",":","The","metapath","described","as","a","list","of",":","obj",":","`","(","src_node_type",",","rel_type",",","dst_node_type",")","`","tuples",".","walk_length","(","int",")",":","The","walk","length",".","context_size","(","int",")",":","The","actual","context","size","which","is","considered","for","positive","samples",".","This","parameter","increases","the","effective","sampling","rate","by","reusing","samples","across","different","source","nodes",".","walks_per_node","(","int",",","optional",")",":","The","number","of","walks","to","sample","for","each","node",".","(","default",":",":","obj",":","`","1","`",")","num_negative_samples","(","int",",","optional",")",":","The","number","of","negative","samples","to","use","for","each","positive","sample",".","(","default",":",":","obj",":","`","1","`",")","num_nodes_dict","(","Dict","[","str",",","int","]",",","optional",")",":","Dictionary","holding","the","number","of","nodes","for","each","node","type",".","(","default",":",":","obj",":","`","None","`",")","sparse","(","bool",",","optional",")",":","If","set","to",":","obj",":","`","True","`",",","gradients","w.r.t",".","to","the","weight","matrix","will","be","sparse",".","(","default",":",":","obj",":","`","False","`",")"]
code: def __init__(
      self,
      edge_index_dict: Dict[EdgeType, Tensor],
      embedding_dim: int,
      metapath: List[EdgeType],
      walk_length: int,
      context_size: int,
      walks_per_node: int = 1,
      num_negative_samples: int = 1,
      num_nodes_dict: Optional[Dict[NodeType, int]] = None,
      sparse: bool = False,
  ):
      super().__init__()
  
      if num_nodes_dict is None:
          num_nodes_dict = {}
          for keys, edge_index in edge_index_dict.items():
              key = keys[0]
              N = int(edge_index[0].max() + 1)
              num_nodes_dict[key] = max(N, num_nodes_dict.get(key, N))
  
              key = keys[-1]
              N = int(edge_index[1].max() + 1)
              num_nodes_dict[key] = max(N, num_nodes_dict.get(key, N))
  
      self.rowptr_dict, self.col_dict, self.rowcount_dict = {}, {}, {}
      for keys, edge_index in edge_index_dict.items():
          sizes = (num_nodes_dict[keys[0]], num_nodes_dict[keys[-1]])
          row, col = sort_edge_index(
              edge_index, num_nodes=max(sizes)
          ).cpu()
          rowptr = index2ptr(row, size=sizes[0])
          self.rowptr_dict[keys] = rowptr
          self.col_dict[keys] = col
          self.rowcount_dict[keys] = rowptr[1:] - rowptr[:-1]
  
      for edge_type1, edge_type2 in zip(metapath[:-1], metapath[1:]):
          if edge_type1[-1] != edge_type2[0]:
              raise ValueError(
                  "Found invalid metapath. Ensure that the destination node "
                  "type matches with the source node type across all "
                  "consecutive edge types."
              )
  
      assert walk_length + 1 >= context_size
      if (
          walk_length > len(metapath)
          and metapath[0][0] != metapath[-1][-1]
      ):
          raise AttributeError(
              "The 'walk_length' is longer than the given 'metapath', but "
              "the 'metapath' does not denote a cycle"
          )
  
      self.embedding_dim = embedding_dim
      self.metapath = metapath
      self.walk_length = walk_length
      self.context_size = context_size
      self.walks_per_node = walks_per_node
      self.num_negative_samples = num_negative_samples
      self.num_nodes_dict = num_nodes_dict
  
      types = set([x[0] for x in metapath]) | set(
          [x[-1] for x in metapath]
      )
      types = sorted(list(types))
  
      count = 0
      self.start, self.end = {}, {}
      for key in types:
          self.start[key] = count
          count += num_nodes_dict[key]
          self.end[key] = count
  
      offset = [self.start[metapath[0][0]]]
      offset += [self.start[keys[-1]] for keys in metapath] * int(
          (walk_length / len(metapath)) + 1
      )
      offset = offset[: walk_length + 1]
      assert len(offset) == walk_length + 1
      self.offset = torch.tensor(offset)
  
      # + 1 denotes a dummy node used to link to for isolated nodes.
      self.embedding = Embedding(
          count + 1, embedding_dim, sparse=sparse
      )
      self.dummy_idx = count
  
      self.reset_parameters()
  
imports: from typing import Dict, List, Optional, Tuple
  import torch
  from torch import Tensor
  from torch.nn import Embedding
  from torch.utils.data import DataLoader
  from torch_geometric.typing import EdgeType, NodeType, OptTensor
  from torch_geometric.utils import sort_edge_index
  from torch_geometric.utils.sparse import index2ptr
label_desc: Use image node_id 1 to create a new MetaPath2Vec object from inherited base classes: torch.nn with example: obj = MetaPath2Vec(edge_index_dict, embedding_dim, metapath, walk_length, context_size, walks_per_node, num_negative_samples, num_nodes_dict, sparse)