class_docstr: :class:`KnowledgeBasedDataset` is based on :class:`~recbole.data.dataset.dataset.Dataset`,
  and load ``.kg`` and ``.link`` additionally.
  
  Entities are remapped together with ``item_id`` specially.
  All entities are remapped into three consecutive ID sections.
  
  - virtual entities that only exist in interaction data.
  - entities that exist both in interaction data and kg triplets.
  - entities only exist in kg triplets.
  
  It also provides several interfaces to transfer ``.kg`` features into coo sparse matrix,
  csr sparse matrix, :class:`DGL.Graph` or :class:`PyG.Data`.
  
  Attributes:
      head_entity_field (str): The same as ``config['HEAD_ENTITY_ID_FIELD']``.
  
      tail_entity_field (str): The same as ``config['TAIL_ENTITY_ID_FIELD']``.
  
      relation_field (str): The same as ``config['RELATION_ID_FIELD']``.
  
      entity_field (str): The same as ``config['ENTITY_ID_FIELD']``.
  
      kg_feat (pandas.DataFrame): Internal data structure stores the kg triplets.
          It's loaded from file ``.kg``.
  
      item2entity (dict): Dict maps ``item_id`` to ``entity``,
          which is loaded from  file ``.link``.
  
      entity2item (dict): Dict maps ``entity`` to ``item_id``,
          which is loaded from  file ``.link``.
  
  Note:
      :attr:`entity_field` doesn't exist exactly. It's only a symbol,
      representing entity features.
  
      ``[UI-Relation]`` is a special relation token.
docstr: Get graph or sparse matrix that describe relations between entities.
  
  For an edge of <src, tgt>, ``graph[src, tgt] = 1`` if ``value_field`` is ``None``,
  else ``graph[src, tgt] = self.kg_feat[value_field][src, tgt]``.
  
  Currently, we support graph in `DGL`_ and `PyG`_,
  and two type of sparse matrices, ``coo`` and ``csr``.
  
  Args:
      form (str, optional): Format of sparse matrix, or library of graph data structure.
          Defaults to ``coo``.
      value_field (str, optional): edge attributes of graph, or data of sparse matrix,
          Defaults to ``None``.
  
  Returns:
      Graph / Sparse matrix of kg triplets.
  
  .. _DGL:
      https://www.dgl.ai/
  
  .. _PyG:
      https://github.com/rusty1s/pytorch_geometric
docstr_tok: ["Get","graph","or","sparse","matrix","that","describe","relations","between","entities",".","For","an","edge","of","<","src",",","tgt",">",",","``","graph","[","src",",","tgt","]","=","1","``","if","``","value_field","``","is","``","None","``",",","else","``","graph","[","src",",","tgt","]","=","self.kg_feat","[","value_field","]","[","src",",","tgt","]","``",".","Currently",",","we","support","graph","in","`","DGL","`","_","and","`","PyG","`","_",",","and","two","type","of","sparse","matrices",",","``","coo","``","and","``","csr","``",".","Args",":","form","(","str",",","optional",")",":","Format","of","sparse","matrix",",","or","library","of","graph","data","structure",".","Defaults","to","``","coo","``",".","value_field","(","str",",","optional",")",":","edge","attributes","of","graph",",","or","data","of","sparse","matrix",",","Defaults","to","``","None","``",".","Returns",":","Graph","\/","Sparse","matrix","of","kg","triplets",".","..","_DGL",":","https",":","\/\/www.dgl.ai\/","..","_PyG",":","https",":","\/\/github.com\/rusty1s\/pytorch_geometric"]
code: def kg_graph(self, form="coo", value_field=None):
      """Get graph or sparse matrix that describe relations between entities.
  
      For an edge of <src, tgt>, ``graph[src, tgt] = 1`` if ``value_field`` is ``None``,
      else ``graph[src, tgt] = self.kg_feat[value_field][src, tgt]``.
  
      Currently, we support graph in `DGL`_ and `PyG`_,
      and two type of sparse matrices, ``coo`` and ``csr``.
  
      Args:
          form (str, optional): Format of sparse matrix, or library of graph data structure.
              Defaults to ``coo``.
          value_field (str, optional): edge attributes of graph, or data of sparse matrix,
              Defaults to ``None``.
  
      Returns:
          Graph / Sparse matrix of kg triplets.
  
      .. _DGL:
          https://www.dgl.ai/
  
      .. _PyG:
          https://github.com/rusty1s/pytorch_geometric
      """
      args = [
          self.kg_feat,
          self.head_entity_field,
          self.tail_entity_field,
          form,
          value_field,
      ]
      if form in ["coo", "csr"]:
          return self._create_sparse_matrix(*args)
      elif form in ["dgl", "pyg"]:
          return self._create_graph(*args)
      else:
          raise NotImplementedError(
              "kg graph format [{}] has not been implemented."
          )
  
imports: import os
  from collections import Counter
  import numpy
  import pandas
  import torch
  from scipy.sparse import coo_matrix
  from recbole.data.dataset import Dataset
  from recbole.utils import FeatureSource, FeatureType, set_color
  from recbole.utils.url import decide_download, download_url, extract_zip
label_desc: Use image node_id 27 for calling the KnowledgeBasedDataset obj's underlying member method code with example usage: obj.kg_graph(form, value_field) and returns: self, self