def testSetFromMap(self): hparams = hparam.HParams(a=1, b=2.0, c='tanh') hparams.override_from_dict({'a': -2, 'c': 'identity'}) self.assertDictEqual({'a': -2, 'c': 'identity', 'b': 2.0}, hparams.values()) hparams = hparam.HParams(x=1, b=2.0, d=[0.5]) hparams.override_from_dict({'d': [0.1, 0.2, 0.3]}) self.assertDictEqual({'d': [0.1, 0.2, 0.3], 'x': 1, 'b': 2.0}, hparams.values())
def resnet_weight(): hp = hparam.HParams() hp.add_hparam("strategy", "weight") hp.add_hparam("black_list", ["logits", "bias"]) hp.add_hparam("white_list", ["td_conv"]) hp.add_hparam("sparsities", [0.1 * i for i in range(10)]) return hp
def testLossSingleWeights(self): """Ensure _loss_single() respects optional 'weights' argument.""" with tf.Graph().as_default(): with self.test_session() as sess: batch_size = 2 sequence_size = 16 vocab_size = 3 model_hparams = hparam.HParams( prepend_mode="none", loss={}, weights_fn={}, label_smoothing=0.0, shared_embedding_and_softmax_weights=False) ph = problem_hparams.TestProblem( vocab_size, vocab_size).get_hparams(model_hparams) model = t2t_model.T2TModel(model_hparams, problem_hparams=ph) logits = tf.zeros((batch_size, sequence_size, 1, 1, vocab_size)) feature = tf.ones((batch_size, sequence_size, 1, 1)) # all-zero weights == zero loss. weights = tf.zeros((batch_size, sequence_size)) loss_num, loss_denom = model._loss_single( logits, "targets", feature, weights=weights) self.assertAllClose(tf.zeros_like(loss_num), sess.run(loss_num)) self.assertAllClose(tf.zeros_like(loss_denom), sess.run(loss_denom)) # non-zero weights > zero loss. weights = tf.ones((batch_size, sequence_size)) loss_num, loss_denom = model._loss_single( logits, "targets", feature, weights=weights) self.assertAllLess(0.0, sess.run(loss_num)) self.assertAllClose(batch_size * sequence_size, sess.run(loss_denom))
def testLists(self): hparams = hparam.HParams(aaa=[1], b=[2.0, 3.0], c_c=['relu6']) self.assertDictEqual({ 'aaa': [1], 'b': [2.0, 3.0], 'c_c': ['relu6'] }, hparams.values()) self.assertEqual([1], hparams.aaa) self.assertEqual([2.0, 3.0], hparams.b) self.assertEqual(['relu6'], hparams.c_c) hparams.parse('aaa=[12]') self.assertEqual([12], hparams.aaa) hparams.parse('aaa=[12,34,56]') self.assertEqual([12, 34, 56], hparams.aaa) hparams.parse('c_c=[relu4,relu12],b=[1.0]') self.assertEqual(['relu4', 'relu12'], hparams.c_c) self.assertEqual([1.0], hparams.b) hparams.parse('c_c=[],aaa=[-34]') self.assertEqual([-34], hparams.aaa) self.assertEqual([], hparams.c_c) hparams.parse('c_c=[_12,3\'4"],aaa=[+3]') self.assertEqual([3], hparams.aaa) self.assertEqual(['_12', '3\'4"'], hparams.c_c) with self.assertRaisesRegexp(ValueError, 'Unknown hyperparameter'): hparams.parse('x=[123]') with self.assertRaisesRegexp(ValueError, 'Could not parse'): hparams.parse('aaa=[poipoi]') with self.assertRaisesRegexp(ValueError, 'Could not parse'): hparams.parse('aaa=[1.0]') with self.assertRaisesRegexp(ValueError, 'Could not parse'): hparams.parse('b=[12x]') with self.assertRaisesRegexp(ValueError, 'Could not parse'): hparams.parse('b=[relu]') with self.assertRaisesRegexp(ValueError, 'Must pass a list'): hparams.parse('aaa=123')
def shake_shake_fgsm(): aparams = hparam.HParams() aparams.attack = "fgsm" aparams.attack_epsilons = [(i + 1) * 0.1 for i in range(12)] aparams.add_hparam("clip_min", 0.0) aparams.add_hparam("clip_max", 255.0) return aparams
def testEmpty(self): hparams = hparam.HParams() self.assertDictEqual({}, hparams.values()) hparams.parse('') self.assertDictEqual({}, hparams.values()) with self.assertRaisesRegexp(ValueError, 'Unknown hyperparameter'): hparams.parse('xyz=123')
def default_model_hparams(): return hparam.HParams( max_input_seq_length=0, max_target_seq_length=0, prepend_mode="none", split_to_length=0, data_dir=None)
def create_hparams_from_json(json_path, hparams=None): """Loading hparams from json; can also start from hparams if specified.""" tf.logging.info("Loading hparams from existing json %s" % json_path) with tf.gfile.Open(json_path, "r") as f: hparams_values = json.load(f) # Prevent certain keys from overwriting the passed-in hparams. # TODO(trandustin): Remove this hack after registries are available to avoid # saving them as functions. if hparams: hparams_values.pop("bottom", None) hparams_values.pop("loss", None) hparams_values.pop("name", None) hparams_values.pop("top", None) hparams_values.pop("weights_fn", None) new_hparams = hparam.HParams(**hparams_values) # Some keys are in new_hparams but not hparams, so we need to be more # careful than simply using parse_json() from HParams if hparams: # hparams specified, so update values from json for key in sorted(new_hparams.values().keys()): if hasattr(hparams, key): # Overlapped keys value = getattr(hparams, key) new_value = getattr(new_hparams, key) if value != new_value: # Different values tf.logging.info("Overwrite key %s: %s -> %s" % ( key, value, new_value)) setattr(hparams, key, new_value) else: hparams = new_hparams return hparams
def _default_hparams(): """A set of basic model hyperparameters.""" return hparam.HParams( # Use this parameter to get comparable perplexity numbers with different # tokenizations. This value should be set to the ratio of the number of # tokens in the test set according to the tokenization used to the number # of tokens in the test set in the "official" tokenization. For # example, if we are using a word-piece based model and we want to # compute per-word perplexity, then we set loss_multiplier to the number # of wordpieces per word in the test set. loss_multiplier=1.0, # Use this parameter to allow for larger sequences in the batch. Without # the use of this parameter, the size of the inner two dimensions will # be used to judge the sequence length. batch_size_multiplier=1, # During inference for autoregressive problems, if the batch_size is 1, # the inference will stop when the model predict a text_encoder.EOS_ID # token. stop_at_eos=False, # Modalities used to map from features to a space compatible with # chosen model architecture. It comprises key-value pairs of a feature # name (str) and its modality type. modality={}, vocab_size={}, # Identifiers used to tell the model which input/target space will be # expected. For example, it can tell that we expect French as characters # as output, or Spanish as sound. Spaces defined as constants in SpaceID # class. input_space_id=SpaceID.GENERIC, target_space_id=SpaceID.GENERIC)
def rlmf_original(): return hparam.HParams( game="pong", sticky_actions=False, base_algo="ppo", base_algo_params="ppo_original_params", batch_size=16, eval_batch_size=2, frame_stack_size=4, eval_sampling_temps=[0.0, 0.2, 0.5, 0.8, 1.0, 2.0], max_num_noops=8, eval_max_num_noops=8, eval_rl_env_max_episode_steps=1000, resize_height_factor=2, resize_width_factor=2, distributional_size=1, # In distributional RL, number of buckets. distributional_subscale=0.04, # How to scale values to buckets. distributional_threshold=0.0, # Optimism threshold for experiments. grayscale=0, rl_env_max_episode_steps=-1, # If set, use this as the gym env name, instead of changing game mode etc. rl_env_name="", # Controls whether we should derive observation space, do some # pre-processing etc. See T2TGymEnv._derive_observation_space. rl_should_derive_observation_space=True, aunused=0, # unused param for multi-run settings. )
def dqn_atari_base(): # These params are based on agents/dqn/configs/dqn.gin # with some modifications taking into account our code return hparam.HParams( agent_gamma=0.99, agent_update_horizon=1, agent_min_replay_history=20000, # agent steps agent_update_period=4, agent_target_update_period=8000, # agent steps agent_epsilon_train=0.01, agent_epsilon_eval=0.001, agent_epsilon_decay_period=250000, # agent steps agent_generates_trainable_dones=True, agent_type="VanillaDQN", # one of ["Rainbow", "VanillaDQN"] optimizer_class="RMSProp", optimizer_learning_rate=0.00025, optimizer_decay=0.95, optimizer_momentum=0.0, optimizer_epsilon=0.00001, optimizer_centered=True, # TODO(kozak): change names maybe replay_buffer -> agent? # Also batch_size is now buffer_batch_size in _DQNAgent. replay_buffer_replay_capacity=1000000, replay_buffer_buffer_batch_size=32, time_limit=27000, save_every_steps=50000, num_frames=int(20 * 1e6), # TODO(konradczechowski) this is not used in trainer_model_free, clean # this up after evaluation refactor eval_episodes_num=3, )
def testBoolParsing(self): for value in 'true', 'false', 'True', 'False', '1', '0': for initial in False, True: hparams = hparam.HParams(use_gpu=initial) hparams.parse('use_gpu=' + value) self.assertEqual(hparams.use_gpu, value in ['True', 'true', '1'])
def create_simulated_env( output_dir, grayscale, resize_width_factor, resize_height_factor, frame_stack_size, generative_model, generative_model_params, random_starts=True, which_epoch_data="last", **other_hparams ): """"Create SimulatedEnv with minimal subset of hparams.""" # We need these, to initialize T2TGymEnv, but these values (hopefully) have # no effect on player. a_bit_risky_defaults = { "game": "pong", # assumes that T2TGymEnv has always reward_range (-1,1) "real_batch_size": 1, "rl_env_max_episode_steps": -1, "max_num_noops": 0 } for key in a_bit_risky_defaults: if key not in other_hparams: other_hparams[key] = a_bit_risky_defaults[key] hparams = hparam.HParams( grayscale=grayscale, resize_width_factor=resize_width_factor, resize_height_factor=resize_height_factor, frame_stack_size=frame_stack_size, generative_model=generative_model, generative_model_params=generative_model_params, **other_hparams ) return load_data_and_make_simulated_env( output_dir, wm_dir=None, hparams=hparams, which_epoch_data=which_epoch_data, random_starts=random_starts)
def testCreateOutputTrainMode(self, likelihood, num_mixtures, depth): batch = 1 height = 8 width = 8 channels = 3 rows = height if likelihood == common_image_attention.DistributionType.CAT: cols = channels * width else: cols = width hparams = hparam.HParams( hidden_size=2, likelihood=likelihood, num_channels=channels, mode=tf_estimator.ModeKeys.TRAIN, num_mixtures=num_mixtures, ) decoder_output = tf.random_normal( [batch, rows, cols, hparams.hidden_size]) targets = tf.random_uniform([batch, height, width, channels], minval=-1., maxval=1.) output = common_image_attention.create_output(decoder_output, rows, cols, targets, hparams) if hparams.likelihood == common_image_attention.DistributionType.CAT: self.assertEqual(output.shape, (batch, height, width, channels, depth)) else: self.assertEqual(output.shape, (batch, height, width, depth))
def testSetHParamTypeMismatch(self): hparams = hparam.HParams( int_=1, str_='str', bool_=True, float_=1.1, list_int=[1, 2], none=None) with self.assertRaises(ValueError): hparams.set_hparam('str_', 2.2) with self.assertRaises(ValueError): hparams.set_hparam('int_', False) with self.assertRaises(ValueError): hparams.set_hparam('bool_', 1) with self.assertRaises(ValueError): hparams.set_hparam('int_', 2.2) with self.assertRaises(ValueError): hparams.set_hparam('list_int', [2, 3.3]) with self.assertRaises(ValueError): hparams.set_hparam('int_', '2') # Casting int to float is OK hparams.set_hparam('float_', 1) # Getting stuck with NoneType :( hparams.set_hparam('none', '1') self.assertEqual('1', hparams.none)
def testSetHParam(self): hparams = hparam.HParams(aaa=1, b=2.0, c_c='relu6', d=True) self.assertDictEqual({ 'aaa': 1, 'b': 2.0, 'c_c': 'relu6', 'd': True }, hparams.values()) self.assertEqual(1, hparams.aaa) self.assertEqual(2.0, hparams.b) self.assertEqual('relu6', hparams.c_c) hparams.set_hparam('aaa', 12) hparams.set_hparam('b', 3.0) hparams.set_hparam('c_c', 'relu4') hparams.set_hparam('d', False) self.assertDictEqual({ 'aaa': 12, 'b': 3.0, 'c_c': 'relu4', 'd': False }, hparams.values()) self.assertEqual(12, hparams.aaa) self.assertEqual(3.0, hparams.b) self.assertEqual('relu4', hparams.c_c)
def testFunction(self): def f(x): return x hparams = hparam.HParams(function=f) self.assertEqual(hparams.function, f) json_str = hparams.to_json() self.assertEqual(json_str, '{}')
def resnet_fgsm(): aparams = hparam.HParams() aparams.attack = "fgsm" aparams.epsilon_name = "eps" aparams.attack_epsilons = [i * 0.8 for i in range(20)] aparams.add_hparam("clip_min", 0.0) aparams.add_hparam("clip_max", 255.0) return aparams
def decode_hparams(overrides=""): """Hyperparameters for decoding.""" hp = hparam.HParams( decode_mode=0, save_images=False, log_results=True, extra_length=100, min_length_ratio=0.0, batch_size=0, beam_size=4, alpha=0.6, eos_penalty=0.0, block_size=0, guess_and_check_top_k=0, guess_and_check_epsilon=-1, insertion_parallel=False, return_beams=False, write_beam_scores=False, max_input_size=-1, identity_output=False, num_samples=-1, # Number of examples to decode. delimiter="\n", decode_to_file="", # str. Prefix for filename to write decodings to. decode_reference="", # str. Filename to read references from. decode_in_memory=False, # How much decode should wait for the next checkpoint decode_timeout_mins=240, summaries_log_dir="decode", # Directory to write hook summaries. shards=1, # How many shards of data to decode (treating 1 as None). shard_id=0, # Which shard are we decoding if more than 1 above. shards_start_offset=0, # Number of the first shard to decode. shard_google_format=False, # If True use Google shard naming format. num_decodes=1, # Number of times to go over the dataset. force_decode_length=False, display_decoded_images=False, # Multi-problem decoding task id. multiproblem_task_id=-1, # Used for video decoding. frames_per_second=10, skip_eos_postprocess=False, # Creates a blue/red border covering border_percent of the frame. border_percent=2, # Maximum number of videos displayed. # number of videos displayed = max_display_outputs * max_display_decodes max_display_outputs=10, max_display_decodes=5, # Used in computation of VGG feature based video metrics. # Set this to be the path to a trained VGG ckpt to output # useful metrics. vgg_ckpt_path="", # Used for MLPerf compliance logging. mlperf_decode_step=0.0, mlperf_threshold=25.0, mlperf_success=False, # A comma-delimited list of additional infer() outputs to be exported. export_extra_infer_outputs="") hp.parse(overrides) return hp
def start_tf(): import problems global problem global request_fn problem = registry.problem("translate_many_to_many") hparams = hparam.HParams(data_dir=DATA_DIR) # ./vocab.txt problem.get_hparams(hparams) request_fn = make_request_fn() print("TF initialized OK")
def merge_unscoped_hparams(scopes_and_hparams): """Merge multiple HParams into one with scopes.""" merged_values = {} for (scope, hparams) in scopes_and_hparams: for key, value in six.iteritems(hparams.values()): scoped_key = "%s.%s" % (scope, key) merged_values[scoped_key] = value return hparam.HParams(**merged_values)
def copy_hparams(hparams): hp_vals = hparams.values() new_hparams = hparam.HParams(**hp_vals) other_attrs = ["problem", "problem_hparams"] for attr in other_attrs: attr_val = getattr(hparams, attr, None) if attr_val is not None: setattr(new_hparams, attr, attr_val) return new_hparams
def main_setup(): #try: global request_fn global problem_hp usr_dir.import_usr_dir(t2t_usr_dir) problem_hp = registry.problem(problem) hparams = hparam.HParams(data_dir=os.path.expanduser(data_dir)) problem_hp.get_hparams(hparams) request_fn = make_request_fn()
def split_scoped_hparams(scopes, merged_hparams): """Split single HParams with scoped keys into multiple.""" split_values = {scope: {} for scope in scopes} merged_values = merged_hparams.values() for scoped_key, value in six.iteritems(merged_values): scope = scoped_key.split(".")[0] key = scoped_key[len(scope) + 1:] split_values[scope][key] = value return [hparam.HParams(**split_values[scope]) for scope in scopes]
def planner_tiny(): return hparam.HParams( num_rollouts=1, planning_horizon=2, rollout_agent_type="random", batch_size=1, env_type="simulated", uct_const=0.0, uniform_first_action=True, )
def planner_base(): return hparam.HParams( num_rollouts=96, batch_size=96, planning_horizon=8, rollout_agent_type="policy", env_type="simulated", uct_const=0., uniform_first_action=True, )
def planner_small(): return hparam.HParams( num_rollouts=64, planning_horizon=16, rollout_agent_type="policy", batch_size=64, env_type="simulated", uct_const=0.0, uniform_first_action=True, )
def testSummarizeLosses(self): with tf.Graph().as_default(): model = t2t_model.T2TModel(hparam.HParams()) losses = {"training": tf.random_normal([]), "extra": tf.random_normal([])} outputs = model._summarize_losses(losses) self.assertIsNone(outputs, None) self.assertEqual( len(tf.get_collection(tf.GraphKeys.SUMMARIES, scope="losses")), len(losses))
def testImagenetMultiResolutionPreprocessExample(self, resize_method): example = {"inputs": tf.random_uniform([64, 64, 3], minval=-1.)} mode = tf.estimator.ModeKeys.TRAIN hparams = hparam.HParams(resolutions=[8, 16, 32]) if resize_method is not None: hparams.resize_method = resize_method problem = imagenet.ImageImagenetMultiResolutionGen() preprocessed_example = problem.preprocess_example(example, mode, hparams) self.assertLen(preprocessed_example, 1) self.assertEqual(preprocessed_example["inputs"].shape, (42, 32, 3))
def main_setup(): #try: global request_fn global problem_hp validate_flags() usr_dir.import_usr_dir(FLAGS.t2t_usr_dir) problem_hp = registry.problem(FLAGS.problem) #print(FLAGS.data_dir, '<---') hparams = hparam.HParams(data_dir=os.path.expanduser(FLAGS.data_dir)) problem_hp.get_hparams(hparams) request_fn = make_request_fn()