class_docstr: KGNN-LS is a knowledge-based recommendation model.
  KGNN-LS transforms the knowledge graph into a user-specific weighted graph and then apply a graph neural network to
  compute personalized item embeddings. To provide better inductive bias, KGNN-LS relies on label smoothness
  assumption, which posits that adjacent items in the knowledge graph are likely to have similar user relevance
  labels/scores. Label smoothness provides regularization over the edge weights and it is equivalent  to a label
  propagation scheme on a graph.
docstr: Get neighbors and corresponding relations for each entity in the KG.
  
  Args:
      kg_graph(scipy.sparse.coo_matrix): an undirected graph
  
  Returns:
      tuple:
          - adj_entity (torch.LongTensor): each line stores the sampled neighbor entities for a given entity,
            shape: [n_entities, neighbor_sample_size]
          - adj_relation (torch.LongTensor): each line stores the corresponding sampled neighbor relations,
            shape: [n_entities, neighbor_sample_size]
docstr_tok: ["Get","neighbors","and","corresponding","relations","for","each","entity","in","the","KG",".","Args",":","kg_graph","(","scipy.sparse.coo_matrix",")",":","an","undirected","graph","Returns",":","tuple",":","-","adj_entity","(","torch.LongTensor",")",":","each","line","stores","the","sampled","neighbor","entities","for","a","given","entity",",","shape",":","[","n_entities",",","neighbor_sample_size","]","-","adj_relation","(","torch.LongTensor",")",":","each","line","stores","the","corresponding","sampled","neighbor","relations",",","shape",":","[","n_entities",",","neighbor_sample_size","]"]
code: def construct_adj(self, kg_graph):
      r"""Get neighbors and corresponding relations for each entity in the KG.
  
      Args:
          kg_graph(scipy.sparse.coo_matrix): an undirected graph
  
      Returns:
          tuple:
              - adj_entity (torch.LongTensor): each line stores the sampled neighbor entities for a given entity,
                shape: [n_entities, neighbor_sample_size]
              - adj_relation (torch.LongTensor): each line stores the corresponding sampled neighbor relations,
                shape: [n_entities, neighbor_sample_size]
      """
      # self.logger.info('constructing knowledge graph ...')
      # treat the KG as an undirected graph
      kg_dict = dict()
      for triple in zip(kg_graph.row, kg_graph.data, kg_graph.col):
          head = triple[0]
          relation = triple[1]
          tail = triple[2]
          if head not in kg_dict:
              kg_dict[head] = []
          kg_dict[head].append((tail, relation))
          if tail not in kg_dict:
              kg_dict[tail] = []
          kg_dict[tail].append((head, relation))
  
      # self.logger.info('constructing adjacency matrix ...')
      # each line of adj_entity stores the sampled neighbor entities for a given entity
      # each line of adj_relation stores the corresponding sampled neighbor relations
      entity_num = kg_graph.shape[0]
      adj_entity = np.zeros(
          [entity_num, self.neighbor_sample_size], dtype=np.int64
      )
      adj_relation = np.zeros(
          [entity_num, self.neighbor_sample_size], dtype=np.int64
      )
      for entity in range(entity_num):
          if entity not in kg_dict.keys():
              adj_entity[entity] = np.array(
                  [entity] * self.neighbor_sample_size
              )
              adj_relation[entity] = np.array(
                  [0] * self.neighbor_sample_size
              )
              continue
  
          neighbors = kg_dict[entity]
          n_neighbors = len(neighbors)
          if n_neighbors >= self.neighbor_sample_size:
              sampled_indices = np.random.choice(
                  list(range(n_neighbors)),
                  size=self.neighbor_sample_size,
                  replace=False,
              )
          else:
              sampled_indices = np.random.choice(
                  list(range(n_neighbors)),
                  size=self.neighbor_sample_size,
                  replace=True,
              )
          adj_entity[entity] = np.array(
              [neighbors[i][0] for i in sampled_indices]
          )
          adj_relation[entity] = np.array(
              [neighbors[i][1] for i in sampled_indices]
          )
  
      return torch.from_numpy(adj_entity), torch.from_numpy(
          adj_relation
      )
  
imports: import random
  import numpy
  import torch
  import torch.nn
  from recbole.model.abstract_recommender import KnowledgeRecommender
  from recbole.model.init import xavier_normal_initialization
  from recbole.model.loss import EmbLoss
  from recbole.utils import InputType
label_desc: Use image node_id 4 for calling the KGNNLS obj's underlying member method code with example usage: obj.construct_adj(kg_graph) and returns: torch,  torch