class_docstr: Read a knowledge graph to memory
class_docstr_tok: ["Read","a","knowledge","graph","to","memory"]
docstr: Sample next actions for training.
docstr_tok: ["Sample","next","actions","for","training","."]
code: def get_next_kg_actions_sampled(
      self,
      current_ents,
      all_answers,
      query_rels,
      all_negatives,
      max_kg_relations=None,
  ):
      """Sample next actions for training."""
      if not max_kg_relations:
          max_kg_relations = self.max_kg_relations
      actions = np.ones(
          (current_ents.shape[0], max_kg_relations, 2), dtype=np.int32
      )
      actions[:, :, 0] *= self.entity_vocab[self.entity_pad_token]
      actions[:, :, 1] *= self.relation_vocab[self.relation_pad_token]
      for i in range(current_ents.shape[0]):
          e1 = current_ents[i]
          actions[i, 0, 0] = e1
          # actions[i, 0, 1] = self.relation_vocab[self.no_op_relation]
          nactions = 0
          ents = all_answers[i] + all_negatives[i]
          np.random.shuffle(ents)
          answers = set(all_answers[i])
          # negatives = set(all_negatives[i])
          nrels = max_kg_relations / len(ents)
          for e2 in ents:
              # if nactions == self.max_kg_relations:
              #     break
              if nactions >= max_kg_relations:
                  logging.info("reached max kg relations")
                  break
              # if nactions >= 0.5 * self.max_kg_relations:
              #     # import ipdb; ipdb.set_trace()
              #     break
              # import ipdb; ipdb.set_trace()
              if e2 in self.kg_data[e1]:
                  rels = self.kg_data[e1][e2]
              else:
                  rels = []
              if len(rels) > nrels:
                  rels = np.random.choice(
                      rels, size=nrels, replace=False
                  )
              # if e2 in answers:
              # take all positive relations
              for rel in rels:
                  # if nactions >= 0.5 * self.max_kg_relations:
                  #     break
                  if nactions >= max_kg_relations:
                      logging.info("reached max kg relations")
                      break
                  if rel == query_rels[i] and e2 in answers:
                      actions[i, nactions, 0] = self.ePAD
                      actions[i, nactions, 1] = self.rPAD
                  else:
                      actions[i, nactions, 0] = e2
                      actions[i, nactions, 1] = rel
                  nactions += 1
      return actions
  
file_path: neural-structured-learning/research/a2n/graph.py
imports: from __future__ import absolute_import
  from __future__ import division
  from __future__ import print_function
  from collections import defaultdict
  import csv
  from absl import logging
  import numpy
label_desc: Use image node_id 9 for calling the Graph obj's underlying member method code with example usage: obj.get_next_kg_actions_sampled(current_ents, all_answers, query_rels, all_negatives, max_kg_relations) and returns: actions