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 = 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 get_default_hparams(): """Get a tf.HParams object with the default values for the hyperparameters. name: string name of the low-rank matrix decompressor specification. rank: integer rank of the low-rank decomposition that is performed. compressor_option: integer indicates what type of factorization (if any) is used. is_b_matrix_trainable: bool indicates whether the b_matrix matrix in the factorization is to be trained. is_c_matrix_trainable: bool indicates whether the c_matrix matrix in the factorization is to be trained. Returns: tf.HParams object initialized to default values. """ return HParams(name='model_compression', rank=100, num_rows=10, num_cols=10, use_tpu=False, compressor_option=0, is_b_matrix_trainable=True, is_c_matrix_trainable=True, is_c_matrix_present=True, block_size=1, pruning_fraction=0.0, use_lsh=False)
def dqn_atari_base(): # These params are based on agents/dqn/configs/dqn.gin # with some modifications taking into account our code return 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, optimizer_class="RMSProp", optimizer_learning_rate=0.00025, optimizer_decay=0.95, optimizer_momentum=0.0, optimizer_epsilon=0.00001, optimizer_centered=True, replay_buffer_replay_capacity=1000000, replay_buffer_batch_size=32, time_limit=27000, save_every_steps=50000, num_frames=int(20 * 1e6), )
def dqn_atari_base(): # These params are based on agents/dqn/configs/dqn.gin # with some modifications taking into account our code return 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, 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 shake_shake_fgsm(): aparams = 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 default_model_hparams(): return HParams( max_input_seq_length=0, max_target_seq_length=0, prepend_mode="none", split_to_length=0, data_dir=None)
def resnet_weight(): hp = 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 _default_hparams(): """A set of basic model hyperparameters.""" return 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={}, # 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 get_config(batch_size, data_path): return configs.Config( model=MusicVAE(lstm_models.BidirectionalLstmEncoder(), lstm_models.CategoricalLstmDecoder()), hparams=merge_hparams( lstm_models.get_default_hparams(), HParams( batch_size=512, max_seq_len=32, # 2 bars w/ 16 steps per bar z_size=512, enc_rnn_size=[2048], dec_rnn_size=[2048, 2048, 2048], free_bits=0, max_beta=0.5, beta_rate=0.99999, sampling_schedule='inverse_sigmoid', sampling_rate=1000, )), note_sequence_augmenter=data.NoteSequenceAugmenter( transpose_range=(-5, 5)), data_converter=data.OneHotMelodyConverter( valid_programs=data.MEL_PROGRAMS, skip_polyphony=False, max_bars=100, # Truncate long melodies before slicing. slice_bars=2, steps_per_quarter=4), train_examples_path=data_path, eval_examples_path=data_path, )
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. 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 = 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 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 = 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 get_default_hparams(): """Get a tf.HParams object with the default values for the hyperparameters. name: string name of the compression specification. Used for adding summaries and ops under a common tensorflow name_scope. alpha_decrement_value: float a positive real number by which alpha is decremented at each update. begin_compression_step: integer the global step at which to begin compression. end_compression_step: integer the global step at which to terminate compression. Defaults to -1 implying that compression continues till the training stops. use_tpu: False indicates whether to use TPU. compression_option: integer indicates what type of factorization (if any) is used. rank: integer indicates what type of factorization (if any) is used. update_option: integer indicates how the update logic is being run. More specifically: 0 - run the update logic in TF; needed when using GPU/TPU. 1 - run the update logic in regular python as opposed to TF. 2 - run the update logic in TF and in regular python. Returns: tf.HParams object initialized to default values. """ return HParams( name='model_compression', alpha_decrement_value=0.01, begin_compression_step=0, end_compression_step=-1, compression_frequency=10, use_tpu=False, compression_option=0, rank=100, update_option=0, run_update_interval_check=1, block_size=1, pruning_fraction=0.0, begin_pruning_step=0, end_pruning_step=-1, weight_sparsity_map=[''], block_dims_map=[''], threshold_decay=0.0, pruning_frequency=10, nbins=256, block_height=1, block_width=1, block_pooling_function='AVG', initial_sparsity=0.0, target_sparsity=0.5, sparsity_function_begin_step=0, sparsity_function_end_step=100, sparsity_function_exponent=3.0, gradient_decay_rate=0.99, prune_option='weight')
def resnet_fgsm(): aparams = 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 default_hparams(): return HParams( n_vocab=0, n_ctx=1024, n_embd=768, n_head=12, n_layer=12, )
def transformer_weight_mjc(): hp = HParams() hp.add_hparam("strategy", "weight") hp.add_hparam("black_list", ["logits", "bias"]) hp.add_hparam("white_list", ["attention"]) hp.add_hparam("sparsities", [0.1 * i for i in range(8)]) hp.add_hparam("weight_sparsities", [0.1 * i for i in range(8)]) return hp
def resnet_weight_mjc(): hp = HParams() hp.add_hparam("strategy", "weight") hp.add_hparam("black_list", ["logits", "bias"]) hp.add_hparam("white_list", ["conv2d"]) hp.add_hparam("sparsities", [0.3 * i for i in range(1)]) hp.add_hparam("weight_sparsities", [0.3 * i for i in range(1)]) return hp
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 HParams(**merged_values)
def copy_hparams(hparams): hp_vals = hparams.values() new_hparams = 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 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 [HParams(**split_values[scope]) for scope in scopes]
def planner_tiny(): return 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_small(): return 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 planner_base(): return 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 decode_hparams(overrides=""): """Hyperparameters for decoding.""" hp = HParams( 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=None, # str. Prefix for filename to write decodings to. 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, multi_targets=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) hp.parse(overrides) 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 = HParams( label_smoothing=0.0, shared_embedding_and_softmax_weights=False) problem_hparams = HParams(loss_multiplier=1.0) problem_hparams.modality = {} model = t2t_model.T2TModel(model_hparams, problem_hparams=problem_hparams) logits = tf.zeros( (batch_size, sequence_size, 1, 1, vocab_size)) target_modality = modality.Modality(model_hparams) 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, target_modality, 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, target_modality, feature, weights=weights) self.assertAllLess(0.0, sess.run(loss_num)) self.assertAllClose(batch_size * sequence_size, sess.run(loss_denom))
def testSummarizeLosses(self): with tf.Graph().as_default(): model = t2t_model.T2TModel(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 = 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 testCelebaMultiResolutionPreprocessExample(self, resize_method): example = {"inputs": tf.random_uniform([218, 178, 3], minval=-1.)} mode = tf.estimator.ModeKeys.TRAIN hparams = HParams(resolutions=[8, 16, 32]) if resize_method is not None: hparams.resize_method = resize_method problem = celeba.ImageCelebaMultiResolution() preprocessed_example = problem.preprocess_example( example, mode, hparams) self.assertLen(preprocessed_example, 2) self.assertEqual(preprocessed_example["inputs"].shape, (138, 138, 3)) self.assertEqual(preprocessed_example["targets"].shape, (42, 32, 3))
def translate(self, inputs): # Registrierung der Problem-Klasse problem = registry.problem(self.problem) # Instanziierung des HPrams-Objekts hparams = HParams(data_dir=os.path.expanduser(self.data_dir)) problem.get_hparams(hparams) request_fn = self.make_request_fn() inputs = inputs # Prediction outputs = serving_utils.predict([inputs], problem, request_fn) outputs, = outputs output, score = outputs return {'inputs': inputs, 'outputs': output, 'scores': score}
def testPostProcessImageTrainMode(self, likelihood, num_mixtures, depth): batch = 1 rows = 8 cols = 24 hparams = HParams( hidden_size=2, likelihood=likelihood, mode=tf.estimator.ModeKeys.TRAIN, num_mixtures=num_mixtures, ) inputs = tf.random_uniform([batch, rows, cols, hparams.hidden_size], minval=-1., maxval=1.) outputs = common_image_attention.postprocess_image( inputs, rows, cols, hparams) self.assertEqual(outputs.shape, (batch, rows, cols, depth))
def check_latent_to_dist(self, architecture): with tf.Graph().as_default(): x = tf.random_uniform(shape=(16, 5, 5, 32)) hparams = HParams(architecture=architecture) x_prior = glow_ops.latent_to_dist("split_prior", x, hparams=hparams, output_channels=64) mean_t, scale_t = x_prior.loc, x_prior.scale with tf.Session() as session: session.run(tf.global_variables_initializer()) mean, scale = session.run([mean_t, scale_t]) self.assertEqual(mean.shape, (16, 5, 5, 64)) self.assertEqual(scale.shape, (16, 5, 5, 64)) self.assertTrue(np.allclose(mean, 0.0)) self.assertTrue(np.allclose(scale, 1.0))