def example_to_supervised_cases(example): """Convert Example to a list of supervised ParseCases. Only possible if example.logical_form is known. Args: example (Example) Returns: list[ParseCase] """ path = ParsePath([], context=example.context) predicates = example.logical_form cases = [] for pred in predicates: case = path.extend() if pred not in case.choices: case.choices.append(pred)
def get_intermediate_beams(self, examples, verbose=False): intermediates = [[] for _ in examples] terminated = [set() for ex in examples] particles = [[ParsePath.empty(ex.context)] for ex in examples] distributions = [[1] for _ in xrange(len(examples))] num_iterations = get_num_iterations( self._config.iterations_per_utterance, examples) if verbose: iterations = verboserate(range(num_iterations), desc='Performing randomized search') else: iterations = xrange(num_iterations) for _ in iterations: for ex_idx, (beam, terminated_set) in enumerate( zip(particles, terminated)): intermediates[ex_idx].append(Beam( sorted(terminated_set, key=self._ranker, reverse=True) + sorted(beam, key=self._ranker, reverse=True))) terminated, particles, distributions = self.advance( terminated, particles, distributions) return [Beam(sorted(list(paths), key=self._ranker, reverse=True)) for paths in terminated], intermediates
def initial_beam(self, context): """Return the initial beam for the context. Args: context (Context) Returns: Beam """ return Beam([ParsePath.empty(context)])
def get_intermediate_beams(self, examples, verbose=False): # Start with beam_size empty paths for each example beams = [Beam([ParsePath.empty(ex.context) for _ in xrange(self._config.beam_size)]) for ex in examples] intermediates = [[] for _ in examples] num_iterations = get_num_iterations( self._config.iterations_per_utterance, examples) if verbose: iterations = verboserate(range(num_iterations), desc='Batched REINFORCE') else: iterations = xrange(num_iterations) for _ in iterations: for ex_idx, beam in enumerate(beams): intermediates[ex_idx].append(beam) beams = self.advance(beams) return beams, intermediates
def get_beams(self, examples, verbose=False): terminated = [set() for _ in examples] # initialize beams beams = [[ParsePath.empty(ex.context)] for ex in examples] # put all probability mass on the root distributions = [[1] for _ in examples] num_iterations = get_num_iterations( self._config.iterations_per_utterance, examples) iterations = xrange(num_iterations) if verbose: iterations = verboserate( iterations, desc='Performing randomized search') for _ in iterations: terminated, beams, distributions = self.advance( terminated, beams, distributions) return [Beam(sorted(list(paths), key=self._ranker, reverse=True)) for paths in terminated]
def path(self, cases): return ParsePath(cases)
def test_prob_some_more(self): empty_path = ParsePath([], context='hello') assert empty_path.prob == 1.