def construct_adj(self, kg_graph):
    'Get neighbors and corresponding relations for each entity in the KG.\n\n        Args:\n            kg_graph(scipy.sparse.coo_matrix): an undirected graph\n\n        Returns:\n            tuple:\n                - adj_entity (torch.LongTensor): each line stores the sampled neighbor entities for a given entity,\n                  shape: [n_entities, neighbor_sample_size]\n                - adj_relation (torch.LongTensor): each line stores the corresponding sampled neighbor relations,\n                  shape: [n_entities, neighbor_sample_size]\n        '
    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))
    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))
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]
def construct_adj(self, kg_graph):
    'Get neighbors and corresponding relations for each entity in the KG.\n\n        Args:\n            kg_graph(scipy.sparse.coo_matrix): an undirected graph\n\n        Returns:\n            tuple:\n                - adj_entity (torch.LongTensor): each line stores the sampled neighbor entities for a given entity,\n                  shape: [n_entities, neighbor_sample_size]\n                - adj_relation (torch.LongTensor): each line stores the corresponding sampled neighbor relations,\n                  shape: [n_entities, neighbor_sample_size]\n        '
    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))
    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))