def test_load_grammar(self): grammar = grammar_utils.load_grammar( grammar_path='third_party/google_research/google_research/' 'neural_guided_symbolic_regression/grammar/' 'univariate_one_constant_grammar.txt') grammar_production_rule_strings = [ str(rule) for rule in grammar.prod_rules ] self.assertIsInstance(grammar, arithmetic_grammar.Grammar) self.assertListEqual( grammar_production_rule_strings, [ # pylint: disable=g-inconsistent-quotes "Nothing -> None", # Padding at 0-th index. "O -> S", # Unique starting production rule at 1-st index. "S -> S '+' T", "S -> S '-' T", "S -> S '*' T", "S -> S '/' T", "S -> T", "T -> '(' S ')'", "T -> 'x'", "T -> '1'", # pylint: enable=g-inconsistent-quotes ])
def run(): """Runs train_and_evaluate.""" hparams_filename = os.path.join(FLAGS.model_dir, 'hparams.json') if FLAGS.is_chief: gfile.MakeDirs(FLAGS.model_dir) hparams = core.read_hparams(FLAGS.hparams, get_hparams()) core.write_hparams(hparams, hparams_filename) # Always load HParams from model_dir. hparams = core.wait_for_hparams(hparams_filename, get_hparams()) grammar = grammar_utils.load_grammar(grammar_path=hparams.grammar_path) estimator = tf.estimator.Estimator( model_fn=functools.partial(model_fn, grammar=grammar), params=hparams, config=tf.estimator.RunConfig( save_checkpoints_secs=hparams.save_checkpoints_secs, keep_checkpoint_max=hparams.keep_checkpoint_max)) train_spec = tf.estimator.TrainSpec(input_fn=functools.partial( input_ops.input_fn, input_pattern=hparams.train_pattern, grammar=grammar), max_steps=hparams.train_steps) # NOTE(leeley): The SavedModel will be stored under the # tf.saved_model.tag_constants.SERVING tag. latest_exporter = tf.estimator.LatestExporter( name='latest_exported_model', serving_input_receiver_fn=functools.partial( input_ops.serving_input_receiver_fn, params=hparams, num_production_rules=grammar.num_production_rules), exports_to_keep=hparams.exports_to_keep) eval_hooks = [] if hparams.num_expressions_per_condition > 0: eval_hooks.append( metrics.GenerationWithLeadingPowersHook( generation_leading_powers_abs_sums=core.hparams_list_value( hparams.generation_leading_powers_abs_sums), num_expressions_per_condition=hparams. num_expressions_per_condition, max_length=hparams.max_length, grammar=grammar)) eval_spec = tf.estimator.EvalSpec( input_fn=functools.partial(input_ops.input_fn, input_pattern=hparams.tune_pattern, grammar=grammar), steps=hparams.eval_steps, exporters=latest_exporter, start_delay_secs=hparams.start_delay_secs, throttle_secs=hparams.throttle_secs, hooks=eval_hooks) tf.estimator.train_and_evaluate(estimator, train_spec, eval_spec)
def test_get_number_valid_next_step(self, prod_rules_sequence_indices, expected): grammar = grammar_utils.load_grammar( grammar_path='third_party/google_research/google_research/' 'neural_guided_symbolic_regression/grammar/' 'univariate_one_constant_grammar.txt') number_valid_next_step = ( generate_empirical_distribution_df.get_number_valid_next_step( prod_rules_sequence_indices, grammar)) self.assertEqual(number_valid_next_step, expected)
def setUp(self): super(GenerateEmpiricalDistributionDfMainTest, self).setUp() # Production rule sequence of ( 1 ) is 1,6,7,6,9. # Production rule sequence of ( x ) is 1,6,7,6,8. self.expression_df = pd.DataFrame( {'expression_string': ['( 1 )', '( x )'], 'leading_at_0': [0, 1], 'leading_at_inf': [0, 1]}) self.grammar = grammar_utils.load_grammar( grammar_path='third_party/google_research/google_research/' 'neural_guided_symbolic_regression/grammar/' 'univariate_one_constant_grammar.txt') self.max_length = 11
def setUp(self): super(GenerateExpressionTest, self).setUp() # The grammar contains: # 0: Nothing -> None # 1: O -> S # 2: S -> S '+' T # 3: S -> S '-' T # 4: S -> S '*' T # 5: S -> S '/' T # 6: S -> T # 7: T -> '(' S ')' # 8: T -> 'x' # 9: T -> '1' self.grammar = grammar_utils.load_grammar( grammar_path='third_party/google_research/google_research/' 'neural_guided_symbolic_regression/grammar/' 'univariate_one_constant_grammar.txt') self.empirical_distribution_df = pd.DataFrame( np.array([[-4, -4], [-3, -3], [-3, 0], [-3, 1]]), columns=['leading_at_0', 'leading_at_inf']) self.empirical_distribution_df['partial_sequence_indices'] = '1' self.empirical_distribution_df.set_index( ['partial_sequence_indices', 'leading_at_0', 'leading_at_inf'], inplace=True) self.empirical_distribution_df = pd.DataFrame( [[0, 0, 0, 0, 0, 1, 0, 0, 0, 0], [0, 0, 0, 0, 0.05, 0.9, 0.05, 0, 0, 0], [0, 0, 0.25, 0.25, 0, 0.5, 0, 0, 0, 0], [0, 0, 0.25, 0.25, 0, 0.5, 0, 0, 0, 0]], index=self.empirical_distribution_df.index) self.limited_history_empirical_distribution_df = ( self.empirical_distribution_df.copy()) self.limited_history_empirical_distribution_df.index.names = ( ['tail_partial_sequence_indices'] + self.limited_history_empirical_distribution_df.index.names[1:]) self.empirical_distribution_df_without_condition = ( self.empirical_distribution_df.iloc[[0], :].reset_index( level=['leading_at_0', 'leading_at_inf'], drop=True)) self.limited_history_empirical_distribution_df_without_condition = ( self.limited_history_empirical_distribution_df.iloc[ [0], :].reset_index(level=['leading_at_0', 'leading_at_inf'], drop=True)) self.next_production_rule_mask = [0, 0, 1, 1, 1, 1, 1, 0, 0, 0]