Code
stringlengths
103
85.9k
Summary
sequencelengths
0
94
Please provide a description of the function:def ae_transformer_internal(inputs, targets, target_space, hparams, cache=None): # Encoder. inputs = common_layers.flatten4d3d(inputs) inputs, ed = encode(inputs, target_space, hparams, "input_enc") # Autoencoding. losses = {"extra": tf.constant(0.0), "latent_pred": tf.constant(0.0)} max_targets_len_from_inputs = tf.concat([inputs, inputs], axis=1) targets, _ = common_layers.pad_to_same_length( targets, max_targets_len_from_inputs, final_length_divisible_by=2**hparams.num_compress_steps) targets_c = compress(targets, hparams, "compress") if hparams.mode != tf.estimator.ModeKeys.PREDICT: # Compress and bottleneck. latents_discrete_hot, extra_loss = vq_discrete_bottleneck( x=targets_c, hparams=hparams) latents_dense = vq_discrete_unbottleneck( latents_discrete_hot, hparams=hparams) latents_dense = targets_c + tf.stop_gradient(latents_dense - targets_c) latents_discrete = tf.argmax(latents_discrete_hot, axis=-1) tf.summary.histogram("codes", tf.reshape(latents_discrete[:, 0, :], [-1])) losses["extra"] = extra_loss # Extra loss predicting latent code from input. latents_pred = decode_transformer(inputs, ed, latents_dense, hparams, "extra") latent_pred_loss = get_latent_pred_loss(latents_pred, latents_discrete_hot, hparams) losses["latent_pred"] = tf.reduce_mean(latent_pred_loss) else: latent_len = common_layers.shape_list(targets_c)[1] embed = functools.partial(vq_discrete_unbottleneck, hparams=hparams) latents_dense = tf.zeros_like(targets_c[:, :latent_len, :, :]) if cache is None: cache = ae_latent_sample_beam(latents_dense, inputs, ed, embed, hparams) cache_hot = tf.one_hot(cache, depth=2**hparams.bottleneck_bits) latents_dense = embed(cache_hot) # Postprocess. d = latents_dense pos = tf.get_variable("pos", [1, 1000, 1, hparams.hidden_size]) pos = pos[:, :common_layers.shape_list(latents_dense)[1] + 1, :, :] latents_dense = tf.pad(latents_dense, [[0, 0], [1, 0], [0, 0], [0, 0]]) + pos # Decompressing the dense latents for i in range(hparams.num_compress_steps): j = hparams.num_compress_steps - i - 1 d = residual_conv(d, 1, (3, 1), hparams, "decompress_rc_%d" % j) d = decompress_step(d, hparams, i > 0, "decompress_%d" % j) masking = common_layers.inverse_lin_decay(hparams.mask_startup_steps) masking *= common_layers.inverse_exp_decay( hparams.mask_startup_steps // 4) # Not much at start. masking = tf.minimum(tf.maximum(masking, 0.0), 1.0) if hparams.mode == tf.estimator.ModeKeys.PREDICT: masking = 1.0 mask = tf.less(masking, tf.random_uniform(common_layers.shape_list(targets)[:-1])) mask = tf.expand_dims(tf.to_float(mask), 3) # targets is always [batch, length, 1, depth] targets = mask * targets + (1.0 - mask) * d res = decode_transformer(inputs, ed, targets, hparams, "decoder") latent_time = tf.less(hparams.mask_startup_steps, tf.to_int32(tf.train.get_global_step())) losses["latent_pred"] *= tf.to_float(latent_time) return res, losses, cache
[ "Main step used for training." ]
Please provide a description of the function:def transformer_nat_small(): hparams = transformer.transformer_small() hparams.batch_size = 2048 hparams.learning_rate = 0.2 hparams.learning_rate_warmup_steps = 4000 hparams.num_hidden_layers = 3 hparams.hidden_size = 384 hparams.filter_size = 2048 hparams.label_smoothing = 0.0 hparams.force_full_predict = True hparams.optimizer = "adam" hparams.optimizer_adam_epsilon = 1e-9 hparams.optimizer_adam_beta1 = 0.9 hparams.optimizer_adam_beta2 = 0.997 hparams.add_hparam("bottleneck_kind", "vq") hparams.add_hparam("bottleneck_bits", 12) hparams.add_hparam("num_compress_steps", 3) hparams.add_hparam("beta", 0.25) hparams.add_hparam("epsilon", 1e-5) hparams.add_hparam("decay", 0.999) hparams.add_hparam("num_samples", 10) hparams.add_hparam("mask_startup_steps", 50000) return hparams
[ "Set of hyperparameters." ]
Please provide a description of the function:def transformer_nat_base(): hparams = transformer_nat_small() hparams.batch_size = 2048 hparams.hidden_size = 512 hparams.filter_size = 4096 hparams.num_hidden_layers = 6 return hparams
[ "Set of hyperparameters." ]
Please provide a description of the function:def transformer_nat_big(): hparams = transformer_nat_small() hparams.batch_size = 2048 hparams.hidden_size = 1024 hparams.filter_size = 4096 hparams.num_hidden_layers = 6 hparams.num_heads = 16 hparams.layer_prepostprocess_dropout = 0.3 return hparams
[ "Set of hyperparameters." ]
Please provide a description of the function:def policy_net(rng_key, batch_observations_shape, num_actions, bottom_layers=None): # Use the bottom_layers as the bottom part of the network and just add the # required layers on top of it. if bottom_layers is None: bottom_layers = [] # NOTE: The LogSoftmax instead of the Softmax. bottom_layers.extend([layers.Dense(num_actions), layers.LogSoftmax()]) net = layers.Serial(*bottom_layers) return net.initialize(batch_observations_shape, rng_key), net
[ "A policy net function." ]
Please provide a description of the function:def value_net(rng_key, batch_observations_shape, num_actions, bottom_layers=None): del num_actions if bottom_layers is None: bottom_layers = [] bottom_layers.extend([ layers.Dense(1), ]) net = layers.Serial(*bottom_layers) return net.initialize(batch_observations_shape, rng_key), net
[ "A value net function." ]
Please provide a description of the function:def policy_and_value_net(rng_key, batch_observations_shape, num_actions, bottom_layers=None): # Layers. cur_layers = [] if bottom_layers is not None: cur_layers.extend(bottom_layers) # Now, with the current logits, one head computes action probabilities and the # other computes the value function. # NOTE: The LogSoftmax instead of the Softmax because of numerical stability. cur_layers.extend([layers.Branch(), layers.Parallel( layers.Serial(layers.Dense(num_actions), layers.LogSoftmax()), layers.Dense(1) )]) net = layers.Serial(*cur_layers) return net.initialize(batch_observations_shape, rng_key), net
[ "A policy and value net function." ]
Please provide a description of the function:def log_params(params, name="params"): for i, param in enumerate(params): if not param: # Empty tuple. continue if not isinstance(param, (list, tuple)): logging.error( "%s[%d] : (%s) = [%s]", name, i, param.shape, onp.array(param)) else: for j, p in enumerate(param): logging.error( "\t%s[%d, %d] = [%s]", name, i, j, onp.array(p))
[ "Dumps the params with `logging.error`." ]
Please provide a description of the function:def collect_trajectories(env, policy_fun, num_trajectories=1, policy="greedy", max_timestep=None, epsilon=0.1): trajectories = [] for t in range(num_trajectories): t_start = time.time() rewards = [] actions = [] done = False observation = env.reset() # This is currently shaped (1, 1) + OBS, but new observations will keep # getting added to it, making it eventually (1, T+1) + OBS observation_history = observation[np.newaxis, np.newaxis, :] # Run either till we're done OR if max_timestep is defined only till that # timestep. ts = 0 while ((not done) and (not max_timestep or observation_history.shape[1] < max_timestep)): ts_start = time.time() # Run the policy, to pick an action, shape is (1, t, A) because # observation_history is shaped (1, t) + OBS predictions = policy_fun(observation_history) # We need the predictions for the last time-step, so squeeze the batch # dimension and take the last time-step. predictions = np.squeeze(predictions, axis=0)[-1] # Policy can be run in one of the following ways: # - Greedy # - Epsilon-Greedy # - Categorical-Sampling action = None if policy == "greedy": action = np.argmax(predictions) elif policy == "epsilon-greedy": # A schedule for epsilon is 1/k where k is the episode number sampled. if onp.random.random() < epsilon: # Choose an action at random. action = onp.random.randint(0, high=len(predictions)) else: # Return the best action. action = np.argmax(predictions) elif policy == "categorical-sampling": # NOTE: The predictions aren't probabilities but log-probabilities # instead, since they were computed with LogSoftmax. # So just np.exp them to make them probabilities. predictions = np.exp(predictions) action = onp.argwhere(onp.random.multinomial(1, predictions) == 1) else: raise ValueError("Unknown policy: %s" % policy) # NOTE: Assumption, single batch. try: action = int(action) except TypeError as err: # Let's dump some information before we die off. logging.error("Cannot convert action into an integer: [%s]", err) logging.error("action.shape: [%s]", action.shape) logging.error("action: [%s]", action) logging.error("predictions.shape: [%s]", predictions.shape) logging.error("predictions: [%s]", predictions) logging.error("observation_history: [%s]", observation_history) raise err observation, reward, done, _ = env.step(action) # observation is of shape OBS, so add extra dims and concatenate on the # time dimension. observation_history = np.concatenate( [observation_history, observation[np.newaxis, np.newaxis, :]], axis=1) rewards.append(reward) actions.append(action) ts += 1 logging.vlog( 2, " Collected time-step[ %5d] of trajectory[ %5d] in [%0.2f] msec.", ts, t, get_time(ts_start)) logging.vlog( 2, " Collected trajectory[ %5d] in [%0.2f] msec.", t, get_time(t_start)) # This means we are done we're been terminated early. assert done or ( max_timestep and max_timestep >= observation_history.shape[1]) # observation_history is (1, T+1) + OBS, lets squeeze out the batch dim. observation_history = np.squeeze(observation_history, axis=0) trajectories.append( (observation_history, np.stack(actions), np.stack(rewards))) return trajectories
[ "Collect trajectories with the given policy net and behaviour.\n\n Args:\n env: A gym env interface, for now this is not-batched.\n policy_fun: observations(B,T+1) -> log-probabs(B,T+1, A) callable.\n num_trajectories: int, number of trajectories.\n policy: string, \"greedy\", \"epsilon-greedy\", or \"categorical-sampling\" i.e.\n how to use the policy_fun to return an action.\n max_timestep: int or None, the index of the maximum time-step at which we\n return the trajectory, None for ending a trajectory only when env\n returns done.\n epsilon: float, the epsilon for `epsilon-greedy` policy.\n\n Returns:\n trajectory: list of (observation, action, reward) tuples, where each element\n `i` is a tuple of numpy arrays with shapes as follows:\n observation[i] = (B, T_i + 1)\n action[i] = (B, T_i)\n reward[i] = (B, T_i)\n " ]
Please provide a description of the function:def get_padding_value(dtype): padding_value = None if dtype == np.uint8: padding_value = np.uint8(0) elif dtype == np.uint16: padding_value = np.uint16(0) elif dtype == np.float32: padding_value = 0.0 else: padding_value = 0 assert padding_value is not None return padding_value
[ "Returns the padding value given a dtype." ]
Please provide a description of the function:def pad_trajectories(trajectories, boundary=20): # Let's compute max(t) over all trajectories. t_max = max(r.shape[0] for (_, _, r) in trajectories) # t_max is rounded to the next multiple of `boundary` boundary = int(boundary) bucket_length = boundary * int(np.ceil(float(t_max) / boundary)) # So all obs will be padded to t_max + 1 and actions and rewards to t_max. padded_observations = [] padded_actions = [] padded_rewards = [] padded_lengths = [] reward_masks = [] for (o, a, r) in trajectories: # Determine the amount to pad, this holds true for obs, actions and rewards. num_to_pad = bucket_length + 1 - o.shape[0] padded_lengths.append(num_to_pad) if num_to_pad == 0: padded_observations.append(o) padded_actions.append(a) padded_rewards.append(r) reward_masks.append(onp.ones_like(r, dtype=np.int32)) continue # First pad observations. padding_config = [(0, num_to_pad, 0)] for _ in range(o.ndim - 1): padding_config.append((0, 0, 0)) padding_config = tuple(padding_config) padding_value = get_padding_value(o.dtype) action_padding_value = get_padding_value(a.dtype) reward_padding_value = get_padding_value(r.dtype) padded_obs = lax.pad(o, padding_value, padding_config) padded_observations.append(padded_obs) # Now pad actions and rewards. assert a.ndim == 1 and r.ndim == 1 padding_config = ((0, num_to_pad, 0),) padded_action = lax.pad(a, action_padding_value, padding_config) padded_actions.append(padded_action) padded_reward = lax.pad(r, reward_padding_value, padding_config) padded_rewards.append(padded_reward) # Also create the mask to use later. reward_mask = onp.ones_like(r, dtype=np.int32) reward_masks.append(lax.pad(reward_mask, 0, padding_config)) return padded_lengths, np.stack(reward_masks), np.stack( padded_observations), np.stack(padded_actions), np.stack(padded_rewards)
[ "Pad trajectories to a bucket length that is a multiple of boundary.\n\n Args:\n trajectories: list[(observation, actions, rewards)], where each observation\n is shaped (t+1,) + OBS and actions & rewards are shaped (t,), with the\n length of the list being B (batch size).\n boundary: int, bucket length, the actions and rewards are padded to integer\n multiples of boundary.\n\n Returns:\n tuple: (padding lengths, reward_mask, padded_observations, padded_actions,\n padded_rewards) where padded_observations is shaped (B, T+1) + OBS and\n padded_actions, padded_rewards & reward_mask are shaped (B, T).\n Where T is max(t) rounded up to an integer multiple of boundary.\n padded_length is how much padding we've added and\n reward_mask is 1s for actual rewards and 0s for the padding.\n " ]
Please provide a description of the function:def rewards_to_go(rewards, mask, gamma=0.99): r B, T = rewards.shape # pylint: disable=invalid-name,unused-variable masked_rewards = rewards * mask # (B, T) # We use the following recurrence relation, derived from the equation above: # # r2g[t+1] = (r2g[t] - r[t]) / gamma # # This means we'll need to calculate r2g[0] first and then r2g[1] and so on .. # # **However** this leads to overflows for long sequences: r2g[t] - r[t] > 0 # and gamma < 1.0, so the division keeps increasing. # # So we just run the recurrence in reverse, i.e. # # r2g[t] = r[t] + (gamma*r2g[t+1]) # # This is much better, but might have lost updates since the (small) rewards # at earlier time-steps may get added to a (very?) large sum. # Compute r2g_{T-1} at the start and then compute backwards in time. r2gs = [masked_rewards[:, -1]] # Go from T-2 down to 0. for t in reversed(range(T - 1)): r2gs.append(masked_rewards[:, t] + (gamma * r2gs[-1])) # The list should have length T. assert T == len(r2gs) # First we stack them in the correct way to make it (B, T), but these are # still from newest (T-1) to oldest (0), so then we flip it on time axis. return np.flip(np.stack(r2gs, axis=1), axis=1)
[ "Computes rewards to go.\n\n Reward to go is defined as follows, the discounted reward that we have to\n yet collect, going forward from this point, i.e.:\n\n r2g_t = \\sum_{l=0}^{\\infty} (\\gamma^{l} * reward_{t+l})\n\n Args:\n rewards: np.ndarray of shape (B, T) of rewards.\n mask: np.ndarray of shape (B, T) of mask for the rewards.\n gamma: float, discount factor.\n\n Returns:\n rewards to go, np.ndarray of shape (B, T).\n " ]
Please provide a description of the function:def value_loss(value_net_apply, value_net_params, observations, rewards, reward_mask, gamma=0.99): B, T = rewards.shape # pylint: disable=invalid-name assert (B, T + 1) == observations.shape[:2] # NOTE: observations is (B, T+1) + OBS, value_prediction is (B, T+1, 1) value_prediction = value_net_apply(observations, value_net_params) assert (B, T + 1, 1) == value_prediction.shape return value_loss_given_predictions(value_prediction, rewards, reward_mask, gamma)
[ "Computes the value loss.\n\n Args:\n value_net_apply: value net apply function with signature (params, ndarray of\n shape (B, T+1) + OBS) -> ndarray(B, T+1, 1)\n value_net_params: params of value_net_apply.\n observations: np.ndarray of shape (B, T+1) + OBS\n rewards: np.ndarray of shape (B, T) of rewards.\n reward_mask: np.ndarray of shape (B, T), the mask over rewards.\n gamma: float, discount factor.\n\n Returns:\n The average L2 value loss, averaged over instances where reward_mask is 1.\n " ]
Please provide a description of the function:def value_loss_given_predictions(value_prediction, rewards, reward_mask, gamma=0.99): B, T = rewards.shape # pylint: disable=invalid-name assert (B, T) == reward_mask.shape assert (B, T + 1, 1) == value_prediction.shape value_prediction = np.squeeze(value_prediction, axis=2) # (B, T+1) value_prediction = value_prediction[:, :-1] * reward_mask # (B, T) r2g = rewards_to_go(rewards, reward_mask, gamma=gamma) # (B, T) loss = (value_prediction - r2g)**2 # Take an average on only the points where mask != 0. return np.sum(loss) / np.sum(reward_mask)
[ "Computes the value loss given the prediction of the value function.\n\n Args:\n value_prediction: np.ndarray of shape (B, T+1, 1)\n rewards: np.ndarray of shape (B, T) of rewards.\n reward_mask: np.ndarray of shape (B, T), the mask over rewards.\n gamma: float, discount factor.\n\n Returns:\n The average L2 value loss, averaged over instances where reward_mask is 1.\n " ]
Please provide a description of the function:def deltas(predicted_values, rewards, mask, gamma=0.99): r # `d`s are basically one-step TD residuals. d = [] _, T = rewards.shape # pylint: disable=invalid-name for t in range(T): d.append(rewards[:, t] + (gamma * predicted_values[:, t + 1]) - predicted_values[:, t]) return np.array(d).T * mask
[ "Computes TD-residuals from V(s) and rewards.\n\n Where a `delta`, i.e. a td-residual is defined as:\n\n delta_{b,t} = r_{b,t} + \\gamma * v_{b,t+1} - v_{b,t}.\n\n Args:\n predicted_values: ndarray of shape (B, T+1). NOTE: Expects axis 2 was\n squeezed. These represent V(s_bt) for b < B and t < T+1\n rewards: ndarray of shape (B, T) of rewards.\n mask: ndarray of shape (B, T) of mask for rewards.\n gamma: float, discount factor.\n\n Returns:\n ndarray of shape (B, T) of one-step TD-residuals.\n " ]
Please provide a description of the function:def gae_advantages(td_deltas, mask, lambda_=0.95, gamma=0.99): r return rewards_to_go(td_deltas, mask, lambda_ * gamma)
[ "Computes the GAE advantages given the one step TD-residuals.\n\n The formula for a GAE advantage estimator is as follows:\n\n A_{bt} = \\sum_{l=0}^{\\infty}(\\gamma * \\lambda)^{l}(\\delta_{b,t+l}).\n\n Internally we just call rewards_to_go, since it is the same computation.\n\n Args:\n td_deltas: np.ndarray of shape (B, T) of one step TD-residuals.\n mask: np.ndarray of shape (B, T) of mask for the residuals. It maybe the\n case that the `td_deltas` are already masked correctly since they are\n produced by `deltas(...)`\n lambda_: float, lambda parameter for GAE estimators.\n gamma: float, lambda parameter for GAE estimators.\n\n Returns:\n GAE advantage estimates.\n " ]
Please provide a description of the function:def chosen_probabs(probab_observations, actions): B, T = actions.shape # pylint: disable=invalid-name assert (B, T + 1) == probab_observations.shape[:2] return probab_observations[np.arange(B)[:, None], np.arange(T), actions]
[ "Picks out the probabilities of the actions along batch and time-steps.\n\n Args:\n probab_observations: ndarray of shape `[B, T+1, A]`, where\n probab_observations[b, t, i] contains the log-probability of action = i at\n the t^th time-step in the b^th trajectory.\n actions: ndarray of shape `[B, T]`, with each entry in [0, A) denoting which\n action was chosen in the b^th trajectory's t^th time-step.\n\n Returns:\n `[B, T]` ndarray with the log-probabilities of the chosen actions.\n " ]
Please provide a description of the function:def compute_probab_ratios(p_new, p_old, actions, reward_mask): B, T = actions.shape # pylint: disable=invalid-name assert (B, T + 1) == p_old.shape[:2] assert (B, T + 1) == p_new.shape[:2] logp_old = chosen_probabs(p_old, actions) logp_new = chosen_probabs(p_new, actions) assert (B, T) == logp_old.shape assert (B, T) == logp_new.shape # Since these are log-probabilities, we just subtract them. probab_ratios = np.exp(logp_new - logp_old) * reward_mask assert (B, T) == probab_ratios.shape return probab_ratios
[ "Computes the probability ratios for each time-step in a trajectory.\n\n Args:\n p_new: ndarray of shape [B, T+1, A] of the log-probabilities that the policy\n network assigns to all the actions at each time-step in each batch using\n the old parameters.\n p_old: ndarray of shape [B, T+1, A], same as above, but using old policy\n network parameters.\n actions: ndarray of shape [B, T] where each element is from [0, A).\n reward_mask: ndarray of shape [B, T] masking over probabilities.\n\n Returns:\n probab_ratios: ndarray of shape [B, T], where\n probab_ratios_{b,t} = p_new_{b,t,action_{b,t}} / p_old_{b,t,action_{b,t}}\n " ]
Please provide a description of the function:def ppo_loss(policy_net_apply, new_policy_params, old_policy_params, value_net_apply, value_net_params, padded_observations, padded_actions, padded_rewards, reward_mask, gamma=0.99, lambda_=0.95, epsilon=0.2): B, T = padded_rewards.shape # pylint: disable=invalid-name assert (B, T + 1) == padded_observations.shape[:2] assert (B, T) == padded_actions.shape assert (B, T) == padded_rewards.shape assert (B, T) == reward_mask.shape # Compute predicted values and predicted log-probs and hand it over to # `ppo_loss_given_predictions`. # (B, T+1, 1) predicted_values = value_net_apply(padded_observations, value_net_params) assert (B, T + 1, 1) == predicted_values.shape # log_probab_actions_{old,new} are both (B, T+1, A) log_probab_actions_old = policy_net_apply(padded_observations, old_policy_params) log_probab_actions_new = policy_net_apply(padded_observations, new_policy_params) assert (B, T + 1) == log_probab_actions_old.shape[:2] assert (B, T + 1) == log_probab_actions_new.shape[:2] assert log_probab_actions_old.shape[-1] == log_probab_actions_new.shape[-1] return ppo_loss_given_predictions(log_probab_actions_new, log_probab_actions_old, predicted_values, padded_actions, padded_rewards, reward_mask, gamma=gamma, lambda_=lambda_, epsilon=epsilon)
[ "PPO objective, with an eventual minus sign, given observations." ]
Please provide a description of the function:def ppo_loss_given_predictions(log_probab_actions_new, log_probab_actions_old, predicted_values, padded_actions, padded_rewards, reward_mask, gamma=0.99, lambda_=0.95, epsilon=0.2): B, T = padded_rewards.shape # pylint: disable=invalid-name assert (B, T) == padded_actions.shape assert (B, T) == reward_mask.shape _, _, A = log_probab_actions_old.shape # pylint: disable=invalid-name assert (B, T + 1, 1) == predicted_values.shape assert (B, T + 1, A) == log_probab_actions_old.shape assert (B, T + 1, A) == log_probab_actions_new.shape # (B, T) td_deltas = deltas( np.squeeze(predicted_values, axis=2), # (B, T+1) padded_rewards, reward_mask, gamma=gamma) # (B, T) advantages = gae_advantages( td_deltas, reward_mask, lambda_=lambda_, gamma=gamma) # (B, T) ratios = compute_probab_ratios(log_probab_actions_new, log_probab_actions_old, padded_actions, reward_mask) assert (B, T) == ratios.shape # (B, T) objective = clipped_objective( ratios, advantages, reward_mask, epsilon=epsilon) assert (B, T) == objective.shape # () average_objective = np.sum(objective) / np.sum(reward_mask) # Loss is negative objective. return -average_objective
[ "PPO objective, with an eventual minus sign, given predictions." ]
Please provide a description of the function:def combined_loss_given_predictions(log_probab_actions_new, log_probab_actions_old, value_prediction, padded_actions, padded_rewards, reward_mask, gamma=0.99, lambda_=0.95, epsilon=0.2, c1=1.0, c2=0.01): loss_value = value_loss_given_predictions( value_prediction, padded_rewards, reward_mask, gamma=gamma) loss_ppo = ppo_loss_given_predictions(log_probab_actions_new, log_probab_actions_old, value_prediction, padded_actions, padded_rewards, reward_mask, gamma=gamma, lambda_=lambda_, epsilon=epsilon) # TODO(afrozm): Add the entropy bonus, but since we don't do that in T2T # we'll skip if for now. entropy_bonus = 0.0 return (loss_ppo + (c1 * loss_value) - (c2 * entropy_bonus), loss_ppo, loss_value, entropy_bonus)
[ "Computes the combined (clipped loss + value loss) given predictions." ]
Please provide a description of the function:def combined_loss(new_params, old_params, policy_and_value_net_apply, padded_observations, padded_actions, padded_rewards, reward_mask, gamma=0.99, lambda_=0.95, epsilon=0.2, c1=1.0, c2=0.01): log_probab_actions_new, value_predictions = policy_and_value_net_apply( padded_observations, new_params) log_probab_actions_old, _ = policy_and_value_net_apply( padded_observations, old_params) # (combined_loss, ppo_loss, value_loss, entropy_bonus) return combined_loss_given_predictions(log_probab_actions_new, log_probab_actions_old, value_predictions, padded_actions, padded_rewards, reward_mask, c1=c1, c2=c2, gamma=gamma, lambda_=lambda_, epsilon=epsilon)
[ "Computes the combined (clipped loss + value loss) given observations." ]
Please provide a description of the function:def ppo_opt_step(i, opt_state, ppo_opt_update, policy_net_apply, old_policy_params, value_net_apply, value_net_params, padded_observations, padded_actions, padded_rewards, reward_mask, gamma=0.99, lambda_=0.95, epsilon=0.1): new_policy_params = trax_opt.get_params(opt_state) g = grad( ppo_loss, argnums=1)( policy_net_apply, new_policy_params, old_policy_params, value_net_apply, value_net_params, padded_observations, padded_actions, padded_rewards, reward_mask, gamma=gamma, lambda_=lambda_, epsilon=epsilon) return ppo_opt_update(i, g, opt_state)
[ "PPO optimizer step." ]
Please provide a description of the function:def value_opt_step(i, opt_state, opt_update, value_net_apply, padded_observations, padded_rewards, reward_mask, gamma=0.99): value_params = trax_opt.get_params(opt_state) # Note this partial application here and argnums above in ppo_opt_step. g = grad(functools.partial(value_loss, value_net_apply))( value_params, padded_observations, padded_rewards, reward_mask, gamma=gamma) return opt_update(i, g, opt_state)
[ "Value optimizer step." ]
Please provide a description of the function:def policy_and_value_opt_step(i, opt_state, opt_update, policy_and_value_net_apply, old_params, padded_observations, padded_actions, padded_rewards, reward_mask, c1=1.0, c2=0.01, gamma=0.99, lambda_=0.95, epsilon=0.1): # Combined loss function given the new params. def policy_and_value_loss(params): (loss, _, _, _) = combined_loss( params, old_params, policy_and_value_net_apply, padded_observations, padded_actions, padded_rewards, reward_mask, c1=c1, c2=c2, gamma=gamma, lambda_=lambda_, epsilon=epsilon) return loss new_params = trax_opt.get_params(opt_state) g = grad(policy_and_value_loss)(new_params) return opt_update(i, g, opt_state)
[ "Policy and Value optimizer step.", "Returns the combined loss given just parameters." ]
Please provide a description of the function:def training_loop(env=None, env_name="CartPole-v0", epochs=EPOCHS, policy_net_fun=None, value_net_fun=None, policy_and_value_net_fun=None, policy_optimizer_fun=None, value_optimizer_fun=None, policy_and_value_optimizer_fun=None, batch_size=BATCH_TRAJECTORIES, num_optimizer_steps=NUM_OPTIMIZER_STEPS, print_every_optimizer_steps=PRINT_EVERY_OPTIMIZER_STEP, boundary=20, max_timestep=None, random_seed=None, gamma=GAMMA, lambda_=LAMBDA, epsilon=EPSILON, c1=1.0, c2=0.01): jax_rng_key = trax.get_random_number_generator_and_set_seed(random_seed) value_losses = [] ppo_objective = [] combined_losses = [] average_rewards = [] env = env if env is not None else gym.make(env_name) # Batch Observations Shape = [-1, -1] + OBS, because we will eventually call # policy and value networks on shape [B, T] +_OBS batch_observations_shape = (-1, -1) + env.observation_space.shape assert isinstance(env.action_space, gym.spaces.Discrete) num_actions = env.action_space.n policy_and_value_net_params, policy_and_value_net_apply = None, None policy_and_value_opt_state, policy_and_value_opt_update = None, None policy_net_params, policy_net_apply = None, None value_net_params, value_net_apply = None, None if policy_and_value_net_fun is not None: jax_rng_key, subkey = jax_random.split(jax_rng_key) # Initialize the policy and value network. policy_and_value_net_params, policy_and_value_net_apply = ( policy_and_value_net_fun(subkey, batch_observations_shape, num_actions)) # Initialize the optimizers. policy_and_value_opt_state, policy_and_value_opt_update = ( policy_and_value_optimizer_fun(policy_and_value_net_params)) else: # Initialize the policy and value functions. assert policy_net_fun and value_net_fun jax_rng_key, key1, key2 = jax_random.split(jax_rng_key, num=3) policy_net_params, policy_net_apply = policy_net_fun( key1, batch_observations_shape, num_actions) value_net_params, value_net_apply = value_net_fun(key2, batch_observations_shape, num_actions) # Initialize the optimizers. ppo_opt_state, ppo_opt_update = policy_optimizer_fun(policy_net_params) value_opt_state, value_opt_update = value_optimizer_fun(value_net_params) # A function that will call the appropriate policy function with parameters. def get_policy_output(observations): if policy_net_apply is not None: assert policy_net_params return policy_net_apply(observations, policy_net_params) assert policy_and_value_net_apply and policy_and_value_net_params policy_predictions, unused_value_predictions = policy_and_value_net_apply( observations, policy_and_value_net_params) return policy_predictions for i in range(epochs): t = time.time() t0 = t logging.vlog(1, "Epoch [% 6d] collecting trajectories.", i) trajs = collect_trajectories( env, policy_fun=get_policy_output, num_trajectories=batch_size, policy=POLICY, max_timestep=max_timestep, epsilon=(10.0 / (i + 10.0))) # this is a different epsilon. avg_reward = float(sum(np.sum(traj[2]) for traj in trajs)) / len(trajs) max_reward = max(np.sum(traj[2]) for traj in trajs) min_reward = min(np.sum(traj[2]) for traj in trajs) average_rewards.append(avg_reward) logging.vlog(1, "Rewards average=[%0.2f], max=[%0.2f], min=[%0.2f]", avg_reward, max_reward, min_reward) logging.vlog(1, "Collecting trajectories took %0.2f msec.", get_time(t)) logging.vlog(1, "Trajectory Length average=[%0.2f], max=[%0.2f], min=[%0.2f]", float(sum(len(traj[0]) for traj in trajs)) / len(trajs), max(len(traj[0]) for traj in trajs), min(len(traj[0]) for traj in trajs)) t = time.time() (_, reward_mask, padded_observations, padded_actions, padded_rewards) = pad_trajectories(trajs, boundary=boundary) logging.vlog(1, "Padding trajectories took %0.2f msec.", get_time(t)) logging.vlog(1, "Padded Observations' shape [%s]", str(padded_observations.shape)) logging.vlog(1, "Padded Actions' shape [%s]", str(padded_actions.shape)) logging.vlog(1, "Padded Rewards' shape [%s]", str(padded_rewards.shape)) # Some assertions. B, T = padded_actions.shape # pylint: disable=invalid-name assert (B, T) == padded_rewards.shape assert (B, T) == reward_mask.shape assert (B, T + 1) == padded_observations.shape[:2] assert (B, T + 1) + env.observation_space.shape == padded_observations.shape # Linear annealing from 0.1 to 0.0 epsilon_schedule = epsilon if epochs == 1 else epsilon * (1.0 - (i / (epochs - 1))) # Compute value and ppo losses. cur_value_loss, cur_ppo_loss, cur_combined_loss = None, None, None if policy_and_value_net_apply is not None: t = time.time() cur_combined_loss, cur_ppo_loss, cur_value_loss, _ = ( combined_loss( policy_and_value_net_params, policy_and_value_net_params, policy_and_value_net_apply, padded_observations, padded_actions, padded_rewards, reward_mask, gamma=gamma, lambda_=lambda_, epsilon=epsilon_schedule, c1=c1, c2=c2)) logging.vlog( 1, "Calculating P&V loss [%10.2f(%10.2f, %10.2f)] took %0.2f msec.", cur_combined_loss, cur_value_loss, cur_ppo_loss, get_time(t)) else: t = time.time() cur_value_loss = value_loss( value_net_apply, value_net_params, padded_observations, padded_rewards, reward_mask, gamma=gamma) logging.vlog(1, "Calculating value loss took %0.2f msec.", get_time(t)) t = time.time() cur_ppo_loss = ppo_loss( policy_net_apply, policy_net_params, policy_net_params, value_net_apply, value_net_params, padded_observations, padded_actions, padded_rewards, reward_mask, gamma=gamma, lambda_=lambda_, epsilon=epsilon_schedule) logging.vlog(1, "Calculating PPO loss took %0.2f msec.", get_time(t)) value_losses.append(cur_value_loss) ppo_objective.append(-1.0 * cur_ppo_loss) combined_losses.append(cur_combined_loss) if policy_and_value_net_apply: logging.vlog(1, "Policy and Value Optimization") t1 = time.time() for j in range(num_optimizer_steps): t = time.time() # Update the optimizer state. policy_and_value_opt_state = policy_and_value_opt_step( j, policy_and_value_opt_state, policy_and_value_opt_update, policy_and_value_net_apply, policy_and_value_net_params, padded_observations, padded_actions, padded_rewards, reward_mask, c1=c1, c2=c2, gamma=gamma, lambda_=lambda_, epsilon=epsilon_schedule) t2 = time.time() # Get the new params. new_policy_and_value_net_params = trax_opt.get_params( policy_and_value_opt_state) if ((j + 1) % print_every_optimizer_steps == 0) or (j == num_optimizer_steps - 1): # Compute and log the loss. (loss_combined, loss_ppo, loss_value, unused_entropy_bonus) = ( combined_loss( new_policy_and_value_net_params, policy_and_value_net_params, # old params policy_and_value_net_apply, padded_observations, padded_actions, padded_rewards, reward_mask, gamma=gamma, lambda_=lambda_, epsilon=epsilon_schedule, c1=c1, c2=c2)) logging.vlog(1, "One Policy and Value grad desc took: %0.2f msec", get_time(t, t2)) logging.vlog( 1, "Combined Loss(value, ppo) [%10.2f] -> [%10.2f(%10.2f,%10.2f)]", cur_combined_loss, loss_combined, loss_value, loss_ppo) # Update the params. policy_and_value_net_params = new_policy_and_value_net_params logging.vlog( 1, "Total PPO loss reduction [%0.2f]%%", (100 * (cur_combined_loss - loss_combined) / np.abs(cur_combined_loss))) logging.info( "Epoch [% 6d], Reward[min, max, avg] [%10.2f,%10.2f,%10.2f], Combined" " Loss(value, ppo) [%10.2f(%10.2f,%10.2f)], took [%10.2f msec]", i, min_reward, max_reward, avg_reward, loss_combined, loss_value, loss_ppo, get_time(t1)) else: # Run optimizers. logging.vlog(1, "PPO Optimization") t1 = time.time() for j in range(num_optimizer_steps): t = time.time() # Update the optimizer state. ppo_opt_state = ppo_opt_step( j, ppo_opt_state, ppo_opt_update, policy_net_apply, policy_net_params, value_net_apply, value_net_params, padded_observations, padded_actions, padded_rewards, reward_mask, gamma=gamma, lambda_=lambda_, epsilon=epsilon_schedule, ) t2 = time.time() # Get the new params. new_policy_net_params = trax_opt.get_params(ppo_opt_state) if ((j + 1) % print_every_optimizer_steps == 0) or (j == num_optimizer_steps - 1): new_ppo_loss = ppo_loss( policy_net_apply, new_policy_net_params, policy_net_params, value_net_apply, value_net_params, padded_observations, padded_actions, padded_rewards, reward_mask, gamma=gamma, lambda_=lambda_, epsilon=epsilon_schedule, ) logging.vlog(1, "One PPO grad desc took: %0.2f msec", get_time(t, t2)) logging.vlog(1, "PPO loss [%10.2f] -> [%10.2f]", cur_ppo_loss, new_ppo_loss) # Update the params. policy_net_params = new_policy_net_params logging.vlog(1, "Total PPO loss reduction [%0.2f]%%", (100 * (cur_ppo_loss - new_ppo_loss) / np.abs(cur_ppo_loss))) logging.vlog(1, "Value Optimization") for j in range(num_optimizer_steps): t = time.time() value_opt_state = value_opt_step( j, value_opt_state, value_opt_update, value_net_apply, padded_observations, padded_rewards, reward_mask, gamma=gamma) t2 = time.time() value_net_params = trax_opt.get_params(value_opt_state) if ((j + 1) % print_every_optimizer_steps == 0) or (j == num_optimizer_steps - 1): new_value_loss = value_loss( value_net_apply, value_net_params, padded_observations, padded_rewards, reward_mask, gamma=gamma) logging.vlog(1, "One value grad desc took: %0.2f msec", get_time(t, t2)) logging.vlog(1, "Value loss [%10.2f] -> [%10.2f]", cur_value_loss, new_value_loss) logging.vlog(1, "Total value loss reduction [%0.2f]%%", (100 * (cur_value_loss - new_value_loss) / np.abs(cur_value_loss))) logging.vlog(1, "Grad desc took %0.2f msec", get_time(t1)) # Set the optimized params to new params. policy_net_params = trax_opt.get_params(ppo_opt_state) value_net_params = trax_opt.get_params(value_opt_state) logging.info( "Epoch [% 6d], Reward[min, max, avg] [%10.2f,%10.2f,%10.2f], " "ppo loss [%10.2f], value loss [%10.2f], took [%10.2f msec]", i, min_reward, max_reward, avg_reward, new_ppo_loss, new_value_loss, get_time(t0)) # Log the parameters, just for the sake of it. if policy_net_params: log_params(policy_net_params, "policy_net_params") if value_net_params: log_params(value_net_params, "value_net_params") if policy_and_value_net_params: log_params(policy_and_value_net_params, "policy_and_value_net_params") if value_losses: logging.vlog(1, "value_losses: %s", np.stack(value_losses)) if ppo_objective: logging.vlog(1, "ppo_objective: %s", np.stack(ppo_objective)) if average_rewards: logging.vlog(1, "average_rewards: %s", average_rewards) return ((policy_net_params, value_net_params), average_rewards, np.stack(value_losses), np.stack(ppo_objective))
[ "Runs the training loop for PPO, with fixed policy and value nets." ]
Please provide a description of the function:def _maybe_download_corpora(tmp_dir): mnli_filename = "MNLI.zip" mnli_finalpath = os.path.join(tmp_dir, "MNLI") if not tf.gfile.Exists(mnli_finalpath): zip_filepath = generator_utils.maybe_download( tmp_dir, mnli_filename, _MNLI_URL) zip_ref = zipfile.ZipFile(zip_filepath, "r") zip_ref.extractall(tmp_dir) zip_ref.close() return mnli_finalpath
[ "Download corpora for multinli.\n\n Args:\n tmp_dir: a string\n Returns:\n a string\n " ]
Please provide a description of the function:def _example_generator(filename): for idx, line in enumerate(tf.gfile.Open(filename, "rb")): if idx == 0: continue # skip header line = text_encoder.to_unicode_utf8(line.strip()) split_line = line.split("\t") # Works for both splits even though dev has some extra human labels. yield { "premise": split_line[8], "hypothesis": split_line[9], "label": split_line[-1] }
[ "Generate mnli examples.\n\n Args:\n filename: a string\n Yields:\n dictionaries containing \"premise\", \"hypothesis\" and \"label\" strings\n " ]
Please provide a description of the function:def shake_shake_skip_connection(x, output_filters, stride, is_training): curr_filters = common_layers.shape_list(x)[-1] if curr_filters == output_filters: return x stride_spec = [1, stride, stride, 1] # Skip path 1. path1 = tf.nn.avg_pool(x, [1, 1, 1, 1], stride_spec, "VALID") path1 = tf.layers.conv2d( path1, int(output_filters / 2), (1, 1), padding="SAME", name="path1_conv") # Skip path 2. pad_arr = [[0, 0], [0, 1], [0, 1], [0, 0]] # First pad with 0's then crop. path2 = tf.pad(x, pad_arr)[:, 1:, 1:, :] path2 = tf.nn.avg_pool(path2, [1, 1, 1, 1], stride_spec, "VALID") path2 = tf.layers.conv2d( path2, int(output_filters / 2), (1, 1), padding="SAME", name="path2_conv") # Concat and apply BN. final_path = tf.concat(values=[path1, path2], axis=-1) final_path = tf.layers.batch_normalization( final_path, training=is_training, name="final_path_bn") return final_path
[ "Adds a residual connection to the filter x for the shake-shake model." ]
Please provide a description of the function:def shake_shake_branch(x, output_filters, stride, rand_forward, rand_backward, hparams): is_training = hparams.mode == tf.estimator.ModeKeys.TRAIN x = tf.nn.relu(x) x = tf.layers.conv2d( x, output_filters, (3, 3), strides=(stride, stride), padding="SAME", name="conv1") x = tf.layers.batch_normalization(x, training=is_training, name="bn1") x = tf.nn.relu(x) x = tf.layers.conv2d(x, output_filters, (3, 3), padding="SAME", name="conv2") x = tf.layers.batch_normalization(x, training=is_training, name="bn2") if is_training: x = x * rand_backward + tf.stop_gradient(x * rand_forward - x * rand_backward) else: x *= 1.0 / hparams.shake_shake_num_branches return x
[ "Building a 2 branching convnet." ]
Please provide a description of the function:def shake_shake_block(x, output_filters, stride, hparams): is_training = hparams.mode == tf.estimator.ModeKeys.TRAIN batch_size = common_layers.shape_list(x)[0] # Generate random numbers for scaling the branches. rand_forward = [ tf.random_uniform( [batch_size, 1, 1, 1], minval=0, maxval=1, dtype=tf.float32) for _ in range(hparams.shake_shake_num_branches) ] rand_backward = [ tf.random_uniform( [batch_size, 1, 1, 1], minval=0, maxval=1, dtype=tf.float32) for _ in range(hparams.shake_shake_num_branches) ] # Normalize so that all sum to 1. total_forward = tf.add_n(rand_forward) total_backward = tf.add_n(rand_backward) rand_forward = [samp / total_forward for samp in rand_forward] rand_backward = [samp / total_backward for samp in rand_backward] zipped_rand = zip(rand_forward, rand_backward) branches = [] for branch, (r_forward, r_backward) in enumerate(zipped_rand): with tf.variable_scope("branch_{}".format(branch)): b = shake_shake_branch(x, output_filters, stride, r_forward, r_backward, hparams) b = tf.nn.dropout(b, 1.0 - hparams.layer_prepostprocess_dropout) branches.append(b) res = shake_shake_skip_connection(x, output_filters, stride, is_training) if hparams.shake_shake_concat: concat_values = [res] + branches concat_output = tf.concat(values=concat_values, axis=-1) concat_output = tf.nn.relu(concat_output) concat_output = tf.layers.conv2d( concat_output, output_filters, (1, 1), name="concat_1x1") concat_output = tf.layers.batch_normalization( concat_output, training=is_training, name="concat_bn") return concat_output else: return res + tf.add_n(branches)
[ "Builds a full shake-shake sub layer." ]
Please provide a description of the function:def shake_shake_layer(x, output_filters, num_blocks, stride, hparams): for block_num in range(num_blocks): curr_stride = stride if (block_num == 0) else 1 with tf.variable_scope("layer_{}".format(block_num)): x = shake_shake_block(x, output_filters, curr_stride, hparams) return x
[ "Builds many sub layers into one full layer." ]
Please provide a description of the function:def shakeshake_small(): hparams = common_hparams.basic_params1() hparams.batch_size = 128 hparams.hidden_size = 32 hparams.layer_prepostprocess_dropout = 0.0 hparams.dropout = 0 hparams.label_smoothing = 0.0 hparams.clip_grad_norm = 0.0 # No clipping for now, one can also try 2.0. hparams.num_hidden_layers = 26 hparams.learning_rate_decay_scheme = "cosine" # Model should be run for 700000 steps with batch size 128 (~1800 epochs) hparams.learning_rate_cosine_cycle_steps = 700000 hparams.learning_rate = 0.2 hparams.learning_rate_warmup_steps = 100 # That's basically unused. hparams.initializer = "uniform_unit_scaling" hparams.initializer_gain = 1.0 hparams.weight_decay = 1e-4 hparams.optimizer = "Momentum" hparams.optimizer_momentum_momentum = 0.9 hparams.add_hparam("shake_shake_num_branches", 2) hparams.add_hparam("shake_shake_concat", int(False)) return hparams
[ "Parameters for CIFAR-10. Gets to about 96% accuracy@700K steps, 1 GPU." ]
Please provide a description of the function:def has_metric_plateaued(steps, values, num_steps=100, delta=0.1, decrease=True): assert num_steps > 0 if len(steps) < 2: return False steps_at_least_num_steps_ago = [ s for s in steps if s <= (steps[-1] - num_steps) ] if not steps_at_least_num_steps_ago: # Not enough steps yet return False delta_step_idx = len(steps_at_least_num_steps_ago) - 1 start_val = values[delta_step_idx] values_to_check = values[delta_step_idx:] observed_deltas = [] for val in values_to_check: if decrease: observed_delta = start_val - val else: observed_delta = val - start_val observed_deltas.append(observed_delta) within_range = [obs < delta for obs in observed_deltas] return all(within_range)
[ "Check if metric has plateaued.\n\n A metric has plateaued if the value has not increased/decreased (depending on\n `decrease`) by `delta` for at least `num_steps`.\n\n Args:\n steps: list<int> list of global steps for values.\n values: list<float> list of metric values.\n num_steps: int, number of steps the metric has to have been plateaued for.\n delta: float, how much the metric should have changed by over num_steps.\n decrease: bool, whether to check if the metric has decreased by delta or\n increased by delta.\n\n Returns:\n bool, whether the metric has plateaued.\n " ]
Please provide a description of the function:def next_frame_savp(): hparams = sv2p_params.next_frame_sv2p() hparams.add_hparam("z_dim", 8) hparams.add_hparam("num_discriminator_filters", 32) hparams.add_hparam("use_vae", True) hparams.add_hparam("use_gan", False) hparams.add_hparam("use_spectral_norm", True) hparams.add_hparam("gan_loss", "cross_entropy") hparams.add_hparam("gan_loss_multiplier", 0.01) hparams.add_hparam("gan_vae_loss_multiplier", 0.01) hparams.add_hparam("gan_optimization", "joint") hparams.bottom = { "inputs": modalities.video_raw_bottom, "targets": modalities.video_raw_targets_bottom, } hparams.loss = { "targets": modalities.video_l1_raw_loss, } hparams.top = { "targets": modalities.video_raw_top, } hparams.latent_loss_multiplier_schedule = "linear" hparams.upsample_method = "bilinear_upsample_conv" hparams.internal_loss = False hparams.reward_prediction = False hparams.anneal_end = 100000 hparams.num_iterations_1st_stage = 0 hparams.num_iterations_2nd_stage = 50000 return hparams
[ "SAVP model hparams." ]
Please provide a description of the function:def next_frame_savp_vae(): hparams = next_frame_savp() hparams.use_vae = True hparams.use_gan = False hparams.latent_loss_multiplier = 1e-3 hparams.latent_loss_multiplier_schedule = "linear_anneal" return hparams
[ "SAVP - VAE only model." ]
Please provide a description of the function:def next_frame_savp_gan(): hparams = next_frame_savp() hparams.use_gan = True hparams.use_vae = False hparams.gan_loss_multiplier = 0.001 hparams.optimizer_adam_beta1 = 0.5 hparams.learning_rate_constant = 2e-4 hparams.gan_loss = "cross_entropy" hparams.learning_rate_decay_steps = 100000 hparams.learning_rate_schedule = "constant*linear_decay" return hparams
[ "SAVP - GAN only model." ]
Please provide a description of the function:def diet_adam_optimizer_params(): return hparam.HParams( quantize=True, # use 16-bit fixed-point quantization_scale=10.0 / tf.int16.max, optimizer="DietAdam", learning_rate=1.0, learning_rate_warmup_steps=2000, learning_rate_decay_scheme="noam", # "noam" or "none" epsilon=1e-10, beta1=0.0, # we can save memory if beta1=0 beta2=0.98, factored_second_moment_accumulator=True, # this saves memory )
[ "Default hyperparameters for a DietAdamOptimizer.\n\n Returns:\n a hyperparameters object.\n " ]
Please provide a description of the function:def diet_expert(x, hidden_size, params): @fn_with_diet_vars(params) def diet_expert_internal(x): dim = x.get_shape().as_list()[-1] h = tf.layers.dense(x, hidden_size, activation=tf.nn.relu, use_bias=False) y = tf.layers.dense(h, dim, use_bias=False) y *= tf.rsqrt(tf.to_float(dim * hidden_size)) return y return diet_expert_internal(x)
[ "A two-layer feed-forward network with relu activation on hidden layer.\n\n Uses diet variables.\n Recomputes hidden layer on backprop to save activation memory.\n\n Args:\n x: a Tensor with shape [batch, io_size]\n hidden_size: an integer\n params: a diet variable HParams object.\n\n Returns:\n a Tensor with shape [batch, io_size]\n " ]
Please provide a description of the function:def _quantize(x, params, randomize=True): if not params.quantize: return x if not randomize: return tf.bitcast( tf.cast(x / params.quantization_scale, tf.int16), tf.float16) abs_x = tf.abs(x) sign_x = tf.sign(x) y = abs_x / params.quantization_scale y = tf.floor(y + tf.random_uniform(common_layers.shape_list(x))) y = tf.minimum(y, tf.int16.max) * sign_x q = tf.bitcast(tf.cast(y, tf.int16), tf.float16) return q
[ "Quantize x according to params, optionally randomizing the rounding." ]
Please provide a description of the function:def _dequantize(q, params): if not params.quantize: return q return tf.to_float(tf.bitcast(q, tf.int16)) * params.quantization_scale
[ "Dequantize q according to params." ]
Please provide a description of the function:def make_diet_var_getter(params): def diet_var_initializer(shape, dtype, partition_info=None): del dtype del partition_info with common_layers.fn_device_dependency("diet_init") as out_deps: float_range = math.sqrt(3) ret = tf.random_uniform(shape, -float_range, float_range) if params.quantize: ret = _quantize(ret, params, randomize=False) out_deps.append(ret) return ret def diet_var_getter(getter, **kwargs): if params.quantize: kwargs["dtype"] = tf.float16 kwargs["initializer"] = diet_var_initializer kwargs["trainable"] = False base_var = getter(**kwargs) dequantized = _dequantize(base_var, params) if not hasattr(params, "dequantized"): params.dequantized = defaultdict(list) params.dequantized[base_var.name].append(dequantized) return dequantized return diet_var_getter
[ "Create a custom variable getter for diet variables according to params.", "Initializer for a diet variable.", "Get diet variable and return it dequantized." ]
Please provide a description of the function:def _fn_with_diet_vars(fn, args, params): vs_ctr = [] def grad_fn(inputs, variables, outputs, output_grads): del outputs # recomputing below with common_layers.fn_device_dependency("diet_grad", output_grads[0].device) as out_dep: with tf.variable_scope(vs_ctr[0], reuse=True): outputs = fn(*inputs) variables = [common_layers.underlying_variable_ref(v) for v in variables] dequantized_variables = [ params.dequantized[v.name][-1] for v in variables ] grads = tf.gradients(outputs, inputs + dequantized_variables, output_grads) grad_inputs = grads[:len(inputs)] grad_variables = grads[len(inputs):] opt = _create_diet_optimizer(params) # Apply grad_variables here var_updates = [] for v, dv in zip(variables, grad_variables): with tf.variable_scope(vs_ctr[0].name): opt.create_slots(v) update_op = opt.update_variable(v, dv) var_updates.append(update_op) with tf.control_dependencies(var_updates): grad_inputs = [tf.identity(dx) for dx in grad_inputs] out_dep.append(grad_inputs) return grad_inputs, [None] * len(variables) @common_layers.fn_with_custom_grad(grad_fn, use_global_vars=True) def forward(*inputs): with tf.variable_scope( None, default_name="diet", custom_getter=make_diet_var_getter(params)) as vs: vs_ctr.append(vs) outputs = fn(*inputs) return outputs with common_layers.fn_device_dependency("diet_forward", args[0].device) as out_dep: outputs = forward(*args) out_dep.append(outputs) return outputs
[ "Call function with args; use diet variables according to params.", "Custom gradient function." ]
Please provide a description of the function:def fn_with_diet_vars(params): params = copy.copy(params) def dec(fn): def wrapped(*args): return _fn_with_diet_vars(fn, args, params) return wrapped return dec
[ "Decorator for graph-building function to use diet variables." ]
Please provide a description of the function:def create_slots(self, var): params = self.params shape = var.get_shape().as_list() if not hasattr(params, "slots"): params.slots = defaultdict(dict) name = var.op.name slots = params.slots[name] if params.factored_second_moment_accumulator and len(shape) == 2: slots["adam_vr"] = tf.get_variable( name + "_adam_vr", [shape[0], 1], trainable=False, initializer=tf.zeros_initializer()) slots["adam_vc"] = tf.get_variable( name + "_adam_vc", [1, shape[1]], trainable=False, initializer=tf.zeros_initializer()) else: slots["adam_v"] = tf.get_variable( name + "_adam_v", shape, trainable=False, initializer=tf.zeros_initializer()) if params.beta1 != 0.0: slots["adam_m"] = tf.get_variable( name + "_adam_m", shape, trainable=False, initializer=tf.zeros_initializer())
[ "Create the factorized Adam accumulators for diet variables." ]
Please provide a description of the function:def update_variable(self, var, grad_var): params = self.params global_step = tf.to_float(self.global_step) + 1 # compute learning rate lrate = params.learning_rate if params.learning_rate_decay_scheme == "noam": lrate *= tf.minimum(global_step * params.learning_rate_warmup_steps**-1.5, global_step**-0.5) else: assert params.learning_rate_decay_scheme == "none" lrate *= tf.minimum(global_step / params.learning_rate_warmup_steps, 1.0) # compute adjustment due to second moment slots = params.slots[var.op.name] grad_squared = tf.square(grad_var) beta2_pow = tf.pow(params.beta2, global_step) if params.factored_second_moment_accumulator and len(var.shape) == 2: vr_update = tf.assign(slots["adam_vr"], slots["adam_vr"] * params.beta2 + tf.reduce_mean(grad_squared, 1, keepdims=True) * (1.0 - params.beta2)) vc_update = tf.assign(slots["adam_vc"], slots["adam_vc"] * params.beta2 + tf.reduce_mean(grad_squared, 0, keepdims=True) * (1.0 - params.beta2)) with tf.control_dependencies([vr_update, vc_update]): vr = tf.sqrt(slots["adam_vr"] / (1.0 - beta2_pow)) + params.epsilon vc = tf.sqrt(slots["adam_vc"] / (1.0 - beta2_pow)) + params.epsilon vc /= tf.reduce_mean(vc) denom = vr * vc else: v_update = tf.assign(slots["adam_v"], slots["adam_v"] * params.beta2 + grad_squared * (1.0 - params.beta2)) with tf.control_dependencies([v_update]): denom = tf.sqrt(slots["adam_v"] / (1.0 - beta2_pow)) + params.epsilon # compute momentum if applicable if params.beta1 != 0.0: m_update = tf.assign(slots["adam_m"], slots["adam_m"] * params.beta1 + grad_var * (1.0 - params.beta1)) with tf.control_dependencies([m_update]): grad_var = slots["adam_m"] # update var subtrahend = lrate * grad_var / denom new_val = _quantize(_dequantize(var, params) - subtrahend, params) return tf.assign(var, new_val)
[ "Update the variable and its slots." ]
Please provide a description of the function:def estimator_spec_eval( self, features, logits, labels, loss, restore_hook, use_tpu): hparams = self.hparams problem = hparams.problem if logits.get_shape().ndims == 3: logits = tf.expand_dims(tf.expand_dims(logits, 2), 3) # Support for multiproblem task_list = [problem] if hasattr(problem, "task_list"): task_list = problem.task_list eval_metrics_fns = metrics.create_evaluation_metrics(task_list, hparams) if use_tpu: def metric_fn(tf_logits, labels): with tf.device("cpu:0"), mtf.utils.outside_all_rewrites(): eval_metrics = {} for metric_name, metric_fn in six.iteritems(eval_metrics_fns): if metric_name.split("/")[-1] not in t2t_model.TPU_METRIC_BLACKLIST: eval_metrics[metric_name] = metric_fn( tf_logits, None, tf.identity(labels)) return eval_metrics return tpu_estimator.TPUEstimatorSpec( tf.estimator.ModeKeys.EVAL, evaluation_hooks=[restore_hook], loss=loss, eval_metrics=(metric_fn, [logits, labels])) else: eval_metrics = {} predictions = {"predictions": logits} for metric_name, metric_fn in six.iteritems(eval_metrics_fns): eval_metrics[metric_name] = metric_fn(logits, features, features["targets"]) return tf.estimator.EstimatorSpec( tf.estimator.ModeKeys.EVAL, predictions=predictions, eval_metric_ops=eval_metrics, evaluation_hooks=[restore_hook], loss=loss)
[ "Construct EstimatorSpec for EVAL mode." ]
Please provide a description of the function:def generator_samples(tmp_dir, pb_cst): # Step1: Download dataset (eventually) data_zip_path = generator_utils.maybe_download_from_drive( directory=tmp_dir, filename=_DATASET_FILENAME, url=_DATASET_URL, ) tf.logging.info("Data downloaded in: {}".format(data_zip_path)) # Step2: Extract dataset # We could deduce _DATASET_PB_PATH from the zip file (instead of # hardcoded path) data_rootdir = os.path.join(tmp_dir, _DATASET_PB_PATH) if not tf.gfile.Exists(data_rootdir): with zipfile.ZipFile(data_zip_path, "r") as corpus_zip: corpus_zip.extractall(tmp_dir) # We could remove the extracted __MACOSX folder tf.logging.info("Data extracted in: {}".format(tmp_dir)) else: tf.logging.info("Data already extracted in: {}".format(tmp_dir)) # Step3: Extract the problems list on the extracted folder def contains_samples(subdir, dirs, files): # pylint: disable=unused-argument return ( _DESC_DIR_NAME in dirs and pb_cst.code_dir_name in dirs ) def next_sample(subdir, dirs, files): # pylint: disable=unused-argument # More could be extracted (like the expected inputs/outputs # pairs, the problem difficulty, the names of the algorithmic techniques # needed) desc_file = os.path.join(subdir, _DESC_DIR_NAME, "description.txt") code_files = [] # As the dataset is noisy, the program deduce the language from the file # content. code_pattern = os.path.join(subdir, pb_cst.code_dir_name, "*.txt") for f in tf.gfile.Glob(code_pattern): with tf.gfile.GFile(f, mode="r") as target_file: # Hack to filter C++/Java files. In theory some python comments could # make the file be considered as C++ but in practice the chance of # getting a false negative is low. content = target_file.read() if not any(p in content for p in pb_cst.filter_patterns): code_files.append(f) return CodingPbInfo( desc_file=desc_file, code_files=code_files ) # The dataset contains problem from two different sources (CodeChef # and CodeForces). Due to the limited number of samples, all problems from # both sources are merged for w in tf.gfile.Walk(data_rootdir): if contains_samples(*w): yield next_sample(*w)
[ "Generator for the dataset samples.\n\n If not present, download and extract the dataset.\n\n Args:\n tmp_dir: path to the directory where to download the dataset.\n pb_cst: CodingPbConstants object defining paths\n\n Yields:\n A CodingPbInfo object containing the next challenge informations.\n ", "Check that the folder contains a problem.", "Return the filenames of the problem." ]
Please provide a description of the function:def lstm(inputs, sequence_length, hparams, train, name, initial_state=None): layers = [_dropout_lstm_cell(hparams, train) for _ in range(hparams.num_hidden_layers)] with tf.variable_scope(name): return tf.nn.dynamic_rnn( tf.nn.rnn_cell.MultiRNNCell(layers), inputs, sequence_length, initial_state=initial_state, dtype=tf.float32, time_major=False)
[ "Adds a stack of LSTM layers on top of input.\n\n Args:\n inputs: The input `Tensor`, shaped `[batch_size, time_steps, hidden_size]`.\n sequence_length: Lengths of the actual input sequence, excluding padding; a\n `Tensor` shaped `[batch_size]`.\n hparams: HParams; hyperparameters.\n train: bool; `True` when constructing training graph to enable dropout.\n name: string; Create variable names under this scope.\n initial_state: tuple of `LSTMStateTuple`s; the initial state of each layer.\n\n Returns:\n A tuple (outputs, states), where:\n outputs: The output `Tensor`, shaped `[batch_size, time_steps,\n hidden_size]`.\n states: A tuple of `LSTMStateTuple`s; the final state of each layer.\n Bidirectional LSTM returns a concatenation of last forward and backward\n state, reduced to the original dimensionality.\n " ]
Please provide a description of the function:def lstm_attention_decoder(inputs, hparams, train, name, initial_state, encoder_outputs, encoder_output_length, decoder_input_length): layers = [_dropout_lstm_cell(hparams, train) for _ in range(hparams.num_hidden_layers)] if hparams.attention_mechanism == "luong": attention_mechanism_class = tf.contrib.seq2seq.LuongAttention elif hparams.attention_mechanism == "bahdanau": attention_mechanism_class = tf.contrib.seq2seq.BahdanauAttention else: raise ValueError("Unknown hparams.attention_mechanism = %s, must be " "luong or bahdanau." % hparams.attention_mechanism) if hparams.get("max_area_width", 1) > 1: def _area_key_value_fn(keys, values): tf.logging.info("max_area_width=%d, area_key_mode=%s, area_value_mode=%s", hparams.get("max_area_width", 1), hparams.get("area_key_mode", "none"), hparams.get("area_value_mode", "none")) keys = area_attention.compute_area_key( keys, max_area_width=hparams.get("max_area_width", 1), mode=hparams.get("area_key_mode", "none"), name="decoder_encoder", training=(hparams.mode == tf.estimator.ModeKeys.TRAIN)) if hparams.get("area_value_mode", "none") == "sum": _, _, values, _, _ = area_attention.compute_area_features( values, max_area_width=hparams.get("max_area_width", 1)) elif hparams.get("area_value_mode", "none") == "mean": values, _, _, _, _ = area_attention.compute_area_features( values, max_area_width=hparams.get("max_area_width", 1)) else: raise ValueError( "Unsupported area_value_mode: %s" % hparams.get( "area_value_mode", "none")) return keys, values area_mask = area_attention.lengths_to_area_mask( feature_length=encoder_output_length, length=common_layers.shape_list(encoder_outputs)[1], max_area_size=hparams.get("max_area_width", "1")) def _area_prob_fn(score): alignments = tf.nn.softmax(score) alignments = tf.where(area_mask, alignments, tf.zeros_like(alignments)) alignments = tf.div(alignments, tf.reduce_sum( alignments, axis=-1, keepdims=True)) return alignments attention_mechanism = attention_mechanism_class( hparams.hidden_size, encoder_outputs, memory_sequence_length=None, probability_fn=_area_prob_fn, custom_key_value_fn=_area_key_value_fn) else: attention_mechanism = attention_mechanism_class(hparams.hidden_size, encoder_outputs) cell = tf.contrib.seq2seq.AttentionWrapper( tf.nn.rnn_cell.MultiRNNCell(layers), [attention_mechanism]*hparams.num_heads, attention_layer_size=[hparams.attention_layer_size]*hparams.num_heads, output_attention=(hparams.output_attention == 1)) batch_size = common_layers.shape_list(inputs)[0] initial_state = cell.zero_state(batch_size, tf.float32).clone( cell_state=initial_state) with tf.variable_scope(name): output, _ = tf.nn.dynamic_rnn( cell, inputs, decoder_input_length, initial_state=initial_state, dtype=tf.float32, time_major=False) # output is [batch_size, decoder_steps, attention_size], where # attention_size is either hparams.hidden_size (when # hparams.output_attention is 0) or hparams.attention_layer_size (when # hparams.output_attention is 1) times the number of attention heads. # # For multi-head attention project output back to hidden size. if hparams.output_attention == 1 and hparams.num_heads > 1: output = tf.layers.dense(output, hparams.hidden_size) return output
[ "Run LSTM cell with attention on inputs of shape [batch x time x size].\n\n Args:\n inputs: The decoder input `Tensor`, shaped `[batch_size, decoder_steps,\n hidden_size]`.\n hparams: HParams; hyperparameters.\n train: bool; `True` when constructing training graph to enable dropout.\n name: string; Create variable names under this scope.\n initial_state: Tuple of `LSTMStateTuple`s; the initial state of each layer.\n encoder_outputs: Encoder outputs; a `Tensor` shaped `[batch_size,\n encoder_steps, hidden_size]`.\n encoder_output_length: Lengths of the actual encoder outputs, excluding\n padding; a `Tensor` shaped `[batch_size]`.\n decoder_input_length: Lengths of the actual decoder inputs, excluding\n padding; a `Tensor` shaped `[batch_size]`.\n\n Raises:\n ValueError: If the hparams.attention_mechanism is anything other than\n luong or bahdanau.\n\n Returns:\n The decoder output `Tensor`, shaped `[batch_size, decoder_steps,\n hidden_size]`.\n ", "Custom fn for computing area keys and values." ]
Please provide a description of the function:def lstm_seq2seq_internal(inputs, targets, hparams, train): with tf.variable_scope("lstm_seq2seq"): if inputs is not None: inputs_length = common_layers.length_from_embedding(inputs) # Flatten inputs. inputs = common_layers.flatten4d3d(inputs) # LSTM encoder. inputs = tf.reverse_sequence(inputs, inputs_length, seq_axis=1) _, final_encoder_state = lstm(inputs, inputs_length, hparams, train, "encoder") else: final_encoder_state = None # LSTM decoder. shifted_targets = common_layers.shift_right(targets) # Add 1 to account for the padding added to the left from shift_right targets_length = common_layers.length_from_embedding(shifted_targets) + 1 decoder_outputs, _ = lstm( common_layers.flatten4d3d(shifted_targets), targets_length, hparams, train, "decoder", initial_state=final_encoder_state) return tf.expand_dims(decoder_outputs, axis=2)
[ "The basic LSTM seq2seq model, main step used for training." ]
Please provide a description of the function:def lstm_seq2seq_internal_attention(inputs, targets, hparams, train, inputs_length, targets_length): with tf.variable_scope("lstm_seq2seq_attention"): # Flatten inputs. inputs = common_layers.flatten4d3d(inputs) # LSTM encoder. inputs = tf.reverse_sequence(inputs, inputs_length, seq_axis=1) encoder_outputs, final_encoder_state = lstm( inputs, inputs_length, hparams, train, "encoder") # LSTM decoder with attention. shifted_targets = common_layers.shift_right(targets) # Add 1 to account for the padding added to the left from shift_right targets_length = targets_length + 1 decoder_outputs = lstm_attention_decoder( common_layers.flatten4d3d(shifted_targets), hparams, train, "decoder", final_encoder_state, encoder_outputs, inputs_length, targets_length) return tf.expand_dims(decoder_outputs, axis=2)
[ "LSTM seq2seq model with attention, main step used for training." ]
Please provide a description of the function:def lstm_bid_encoder(inputs, sequence_length, hparams, train, name): with tf.variable_scope(name): cell_fw = tf.nn.rnn_cell.MultiRNNCell( [_dropout_lstm_cell(hparams, train) for _ in range(hparams.num_hidden_layers)]) cell_bw = tf.nn.rnn_cell.MultiRNNCell( [_dropout_lstm_cell(hparams, train) for _ in range(hparams.num_hidden_layers)]) ((encoder_fw_outputs, encoder_bw_outputs), (encoder_fw_state, encoder_bw_state)) = tf.nn.bidirectional_dynamic_rnn( cell_fw, cell_bw, inputs, sequence_length, dtype=tf.float32, time_major=False) encoder_outputs = tf.concat((encoder_fw_outputs, encoder_bw_outputs), 2) encoder_states = [] for i in range(hparams.num_hidden_layers): if isinstance(encoder_fw_state[i], tf.nn.rnn_cell.LSTMStateTuple): encoder_state_c = tf.concat( values=(encoder_fw_state[i].c, encoder_bw_state[i].c), axis=1, name="encoder_fw_state_c") encoder_state_h = tf.concat( values=(encoder_fw_state[i].h, encoder_bw_state[i].h), axis=1, name="encoder_fw_state_h") encoder_state = tf.nn.rnn_cell.LSTMStateTuple( c=encoder_state_c, h=encoder_state_h) elif isinstance(encoder_fw_state[i], tf.Tensor): encoder_state = tf.concat( values=(encoder_fw_state[i], encoder_bw_state[i]), axis=1, name="bidirectional_concat") encoder_states.append(encoder_state) encoder_states = tuple(encoder_states) return encoder_outputs, encoder_states
[ "Bidirectional LSTM for encoding inputs that are [batch x time x size]." ]
Please provide a description of the function:def lstm_seq2seq_internal_bid_encoder(inputs, targets, hparams, train): with tf.variable_scope("lstm_seq2seq_bid_encoder"): if inputs is not None: inputs_length = common_layers.length_from_embedding(inputs) # Flatten inputs. inputs = common_layers.flatten4d3d(inputs) # LSTM encoder. _, final_encoder_state = lstm_bid_encoder( inputs, inputs_length, hparams, train, "encoder") else: inputs_length = None final_encoder_state = None # LSTM decoder. shifted_targets = common_layers.shift_right(targets) # Add 1 to account for the padding added to the left from shift_right targets_length = common_layers.length_from_embedding(shifted_targets) + 1 hparams_decoder = copy.copy(hparams) hparams_decoder.hidden_size = 2 * hparams.hidden_size decoder_outputs, _ = lstm( common_layers.flatten4d3d(shifted_targets), targets_length, hparams_decoder, train, "decoder", initial_state=final_encoder_state) return tf.expand_dims(decoder_outputs, axis=2)
[ "The basic LSTM seq2seq model with bidirectional encoder." ]
Please provide a description of the function:def lstm_seq2seq_internal_attention_bid_encoder(inputs, targets, hparams, train): with tf.variable_scope("lstm_seq2seq_attention_bid_encoder"): inputs_length = common_layers.length_from_embedding(inputs) # Flatten inputs. inputs = common_layers.flatten4d3d(inputs) # LSTM encoder. encoder_outputs, final_encoder_state = lstm_bid_encoder( inputs, inputs_length, hparams, train, "encoder") # LSTM decoder with attention shifted_targets = common_layers.shift_right(targets) # Add 1 to account for the padding added to the left from shift_right targets_length = common_layers.length_from_embedding(shifted_targets) + 1 hparams_decoder = copy.copy(hparams) hparams_decoder.hidden_size = 2 * hparams.hidden_size decoder_outputs = lstm_attention_decoder( common_layers.flatten4d3d(shifted_targets), hparams_decoder, train, "decoder", final_encoder_state, encoder_outputs, inputs_length, targets_length) return tf.expand_dims(decoder_outputs, axis=2)
[ "LSTM seq2seq model with attention, main step used for training." ]
Please provide a description of the function:def lstm_seq2seq(): hparams = common_hparams.basic_params1() hparams.daisy_chain_variables = False hparams.batch_size = 1024 hparams.hidden_size = 128 hparams.num_hidden_layers = 2 hparams.initializer = "uniform_unit_scaling" hparams.initializer_gain = 1.0 hparams.weight_decay = 0.0 return hparams
[ "hparams for LSTM." ]
Please provide a description of the function:def lstm_attention_base(): hparams = lstm_seq2seq() hparams.add_hparam("attention_layer_size", hparams.hidden_size) hparams.add_hparam("output_attention", True) hparams.add_hparam("num_heads", 1) return hparams
[ "Base attention params." ]
Please provide a description of the function:def lstm_asr_v1(): hparams = lstm_bahdanau_attention() hparams.num_hidden_layers = 2 hparams.hidden_size = 256 hparams.batch_size = 36 hparams.max_input_seq_length = 600000 hparams.max_target_seq_length = 350 hparams.max_length = hparams.max_input_seq_length hparams.min_length_bucket = hparams.max_input_seq_length // 2 hparams.learning_rate = 0.05 return hparams
[ "Basic LSTM Params." ]
Please provide a description of the function:def lstm_area_attention_base(): hparams = lstm_luong_attention() hparams.batch_size = 16384 hparams.num_hidden_layers = 2 hparams.hidden_size = 1024 hparams.num_heads = 4 hparams.dropout = 0.2 hparams.learning_rate = 0.1 hparams.max_area_width = 2 hparams.area_key_mode = "mean" hparams.area_value_mode = "sum" return hparams
[ "Hparams for LSTM with area attention." ]
Please provide a description of the function:def create_surrogate_run_config(hp): save_ckpt_steps = max(FLAGS.iterations_per_loop, FLAGS.local_eval_frequency) save_ckpt_secs = FLAGS.save_checkpoints_secs or None if save_ckpt_secs: save_ckpt_steps = None assert FLAGS.surrogate_output_dir # the various custom getters we have written do not play well together yet. # TODO(noam): ask rsepassi for help here. daisy_chain_variables = ( hp.daisy_chain_variables and hp.activation_dtype == "float32" and hp.weight_dtype == "float32") return trainer_lib.create_run_config( model_name=FLAGS.model, model_dir=os.path.expanduser(FLAGS.surrogate_output_dir), master=FLAGS.master, iterations_per_loop=FLAGS.iterations_per_loop, num_shards=FLAGS.tpu_num_shards, log_device_placement=FLAGS.log_device_placement, save_checkpoints_steps=save_ckpt_steps, save_checkpoints_secs=save_ckpt_secs, keep_checkpoint_max=FLAGS.keep_checkpoint_max, keep_checkpoint_every_n_hours=FLAGS.keep_checkpoint_every_n_hours, num_gpus=FLAGS.worker_gpu, gpu_order=FLAGS.gpu_order, num_async_replicas=FLAGS.worker_replicas, gpu_mem_fraction=FLAGS.worker_gpu_memory_fraction, enable_graph_rewriter=FLAGS.enable_graph_rewriter, use_tpu=FLAGS.use_tpu, schedule=FLAGS.schedule, no_data_parallelism=hp.no_data_parallelism, daisy_chain_variables=daisy_chain_variables, ps_replicas=FLAGS.ps_replicas, ps_job=FLAGS.ps_job, ps_gpu=FLAGS.ps_gpu, sync=FLAGS.sync, worker_id=FLAGS.worker_id, worker_job=FLAGS.worker_job, random_seed=FLAGS.random_seed, tpu_infeed_sleep_secs=FLAGS.tpu_infeed_sleep_secs, inter_op_parallelism_threads=FLAGS.inter_op_parallelism_threads, log_step_count_steps=FLAGS.log_step_count_steps, intra_op_parallelism_threads=FLAGS.intra_op_parallelism_threads)
[ "Create a run config.\n\n Args:\n hp: model hyperparameters\n Returns:\n a run config\n " ]
Please provide a description of the function:def prepare_data(problem, hparams, params, config): input_fn = problem.make_estimator_input_fn( tf.estimator.ModeKeys.EVAL, hparams, force_repeat=True) dataset = input_fn(params, config) features, _ = dataset.make_one_shot_iterator().get_next() inputs, labels = features["targets"], features["inputs"] inputs = tf.to_float(inputs) input_shape = inputs.shape.as_list() inputs = tf.reshape(inputs, [hparams.batch_size] + input_shape[1:]) labels = tf.reshape(labels, [hparams.batch_size]) return inputs, labels, features
[ "Construct input pipeline." ]
Please provide a description of the function:def encode(self, s): # Make sure that the data is a single channel, 16bit, 16kHz wave. # TODO(chorowski): the directory may not be writable, this should fallback # to a temp path, and provide instructions for installing sox. if s.endswith(".mp3"): # TODO(dliebling) On Linux, check if libsox-fmt-mp3 is installed. out_filepath = s[:-4] + ".wav" call([ "sox", "--guard", s, "-r", "16k", "-b", "16", "-c", "1", out_filepath ]) s = out_filepath elif not s.endswith(".wav"): out_filepath = s + ".wav" if not os.path.exists(out_filepath): call(["sox", "-r", "16k", "-b", "16", "-c", "1", s, out_filepath]) s = out_filepath rate, data = wavfile.read(s) assert rate == self._sample_rate assert len(data.shape) == 1 if data.dtype not in [np.float32, np.float64]: data = data.astype(np.float32) / np.iinfo(data.dtype).max return data.tolist()
[ "Transform a string with a filename into a list of float32.\n\n Args:\n s: path to the file with a waveform.\n\n Returns:\n samples: list of int16s\n " ]
Please provide a description of the function:def decode(self, ids): _, tmp_file_path = tempfile.mkstemp() wavfile.write(tmp_file_path, self._sample_rate, np.asarray(ids)) return tmp_file_path
[ "Transform a sequence of float32 into a waveform.\n\n Args:\n ids: list of integers to be converted.\n\n Returns:\n Path to the temporary file where the waveform was saved.\n\n Raises:\n ValueError: if the ids are not of the appropriate size.\n " ]
Please provide a description of the function:def new_vertex(self): vertex = Vertex(len(self.vertices)) self.vertices.append(vertex) return vertex
[ "Creates and returns a new vertex.\n\n Returns:\n A new Vertex instance with a unique index.\n " ]
Please provide a description of the function:def get_vertex(self, key): if key in self.vertex_map: return self.vertex_map[key] vertex = self.new_vertex() self.vertex_map[key] = vertex return vertex
[ "Returns or Creates a Vertex mapped by key.\n\n Args:\n key: A string reference for a vertex. May refer to a new Vertex in which\n case it will be created.\n\n Returns:\n A the Vertex mapped to by key.\n " ]
Please provide a description of the function:def add_edge(self, source, target): edge = Edge(len(self.edges)) self.edges.append(edge) source.out_edges.append(edge.idx) target.in_edges.append(edge.idx) edge.source = source.idx edge.target = target.idx return edge
[ "Returns a new edge connecting source and target vertices.\n\n Args:\n source: The source Vertex.\n target: The target Vertex.\n\n Returns:\n A new Edge linking source to target.\n " ]
Please provide a description of the function:def to_dict(self): return { "node": [v.to_dict() for v in self.vertices], "edge": [e.to_dict() for e in self.edges] }
[ "Returns a simplified dictionary representing the Graph.\n\n Returns:\n A dictionary that can easily be serialized to JSON.\n " ]
Please provide a description of the function:def attend(x, source, hparams, name): with tf.variable_scope(name): x = tf.squeeze(x, axis=2) if len(source.get_shape()) > 3: source = tf.squeeze(source, axis=2) source = common_attention.add_timing_signal_1d(source) y = common_attention.multihead_attention( common_layers.layer_preprocess(x, hparams), source, None, hparams.attention_key_channels or hparams.hidden_size, hparams.attention_value_channels or hparams.hidden_size, hparams.hidden_size, hparams.num_heads, hparams.attention_dropout) res = common_layers.layer_postprocess(x, y, hparams) return tf.expand_dims(res, axis=2)
[ "Self-attention layer with source as memory antecedent." ]
Please provide a description of the function:def top_k_softmax(x, k): x = tf.nn.softmax(x) top_x, _ = tf.nn.top_k(x, k=k+1) min_top = tf.reduce_min(top_x, axis=-1, keepdims=True) x = tf.nn.relu((x - min_top) + 1e-12) x /= tf.reduce_sum(x, axis=-1, keepdims=True) return x, tf.reduce_max(top_x, axis=-1)
[ "Calculate softmax(x), select top-k and rescale to sum to 1." ]
Please provide a description of the function:def compress(x, c, is_2d, hparams, name): with tf.variable_scope(name): # Run compression by strided convs. cur = x k1 = (3, 3) if is_2d else (3, 1) k2 = (2, 2) if is_2d else (2, 1) cur = residual_conv(cur, hparams.num_compress_steps, k1, hparams, "rc") if c is not None and hparams.do_attend_compress: cur = attend(cur, c, hparams, "compress_attend") for i in range(hparams.num_compress_steps): if hparams.do_residual_compress: cur = residual_conv(cur, hparams.num_compress_steps, k1, hparams, "rc_%d" % i) cur = common_layers.conv_block( cur, hparams.hidden_size, [((1, 1), k2)], strides=k2, name="compress_%d" % i) return cur
[ "Compress." ]
Please provide a description of the function:def decode_transformer(encoder_output, encoder_decoder_attention_bias, targets, hparams, name, task=None, causal=True): orig_hparams = hparams with tf.variable_scope(name): if task is None: task = hparams.task if task == "translate": targets = common_layers.flatten4d3d(targets) decoder_input, decoder_self_bias = ( transformer.transformer_prepare_decoder(targets, hparams)) decoder_input = tf.nn.dropout(decoder_input, 1.0 - hparams.layer_prepostprocess_dropout) if not causal: decoder_self_bias *= 0. decoder_output = transformer.transformer_decoder( decoder_input, encoder_output, decoder_self_bias, encoder_decoder_attention_bias, hparams) decoder_output = tf.expand_dims(decoder_output, axis=2) else: assert task == "image" inputs = None # have to reshape targets as b, 32, 32, 3 * hidden size] beacuse otherwise # prepare_image will choke targets = tf.reshape(targets, [tf.shape(targets)[0], hparams.img_len, hparams.img_len, hparams.num_channels*hparams.hidden_size]) # Prepare decoder inputs and bias. # TODO(nikip): Make prepare_decoder return bias decoder_input, _, _ = cia.prepare_decoder(targets, hparams) bias = None # Add class label to decoder input. if not hparams.drop_inputs: decoder_input += tf.reshape( inputs, [common_layers.shape_list(targets)[0], 1, 1, hparams.hidden_size]) decoder_output = cia.transformer_decoder_layers( decoder_input, encoder_output=None, num_layers=hparams.num_decoder_layers or hparams.num_hidden_layers, hparams=hparams, self_attention_bias=bias, attention_type=hparams.dec_attention_type, name="decoder") decoder_output_shape = common_layers.shape_list(decoder_output) decoder_output = tf.reshape(decoder_output, [decoder_output_shape[0], -1, 1, hparams.hidden_size]) # Expand since t2t expects 4d tensors. hparams = orig_hparams return decoder_output
[ "Original Transformer decoder." ]
Please provide a description of the function:def ae_latent_softmax(latents_pred, latents_discrete, hparams): vocab_size = 2 ** hparams.z_size if hparams.num_decode_blocks < 2: latents_logits = tf.layers.dense(latents_pred, vocab_size, name="extra_logits") if hparams.logit_normalization: latents_logits *= tf.rsqrt(1e-8 + tf.reduce_mean(tf.square(latents_logits))) loss = None if latents_discrete is not None: if hparams.soft_em: # latents_discrete is actually one-hot of multinomial samples assert hparams.num_decode_blocks == 1 loss = tf.nn.softmax_cross_entropy_with_logits_v2( labels=latents_discrete, logits=latents_logits) else: loss = tf.nn.sparse_softmax_cross_entropy_with_logits( labels=latents_discrete, logits=latents_logits) sample = multinomial_sample( latents_logits, vocab_size, hparams.sampling_temp) return sample, loss # Multi-block case. vocab_bits = int(math.log(vocab_size, 2)) assert vocab_size == 2**vocab_bits assert vocab_bits % hparams.num_decode_blocks == 0 block_vocab_size = 2**(vocab_bits // hparams.num_decode_blocks) latents_logits = [ tf.layers.dense( latents_pred, block_vocab_size, name="extra_logits_%d" % i) for i in range(hparams.num_decode_blocks) ] loss = None if latents_discrete is not None: losses = [] for i in range(hparams.num_decode_blocks): d = tf.floormod(tf.floordiv(latents_discrete, block_vocab_size**i), block_vocab_size) losses.append(tf.nn.sparse_softmax_cross_entropy_with_logits( labels=d, logits=latents_logits[i])) loss = sum(losses) samples = [multinomial_sample(l, block_vocab_size, hparams.sampling_temp) for l in latents_logits] sample = sum([s * block_vocab_size**i for i, s in enumerate(samples)]) return sample, loss
[ "Latent prediction and loss." ]
Please provide a description of the function:def ae_latent_sample(latents_dense, inputs, ed, embed, iters, hparams): if hparams.num_decode_blocks < 2 and hparams.sampling_temp == 0.0: # TODO(lukaszkaiser): beam-search only works in non-blocked mode for now. tf.logging.info("Running beam-search for latents with beam size 1.") return ae_latent_sample_beam(latents_dense, inputs, ed, embed, hparams) latents_pred = decode_transformer(inputs, ed, latents_dense, hparams, "extra") latents_discrete, _ = ae_latent_softmax(latents_pred, None, hparams) def next_bit(latents_discrete, i): latents_discrete_prev = latents_discrete with tf.variable_scope(tf.get_variable_scope(), reuse=True): latents_dense = embed(latents_discrete) latents_pred = decode_transformer( inputs, ed, latents_dense, hparams, "extra") latents_discrete, _ = ae_latent_softmax(latents_pred, None, hparams) return tf.concat([latents_discrete_prev[:, :(i+1), :], latents_discrete[:, (i+1):, :]], axis=1) for i in range(iters): latents_discrete = next_bit(latents_discrete, i) return latents_discrete
[ "Sample from the latent space in the autoencoder." ]
Please provide a description of the function:def ae_transformer_internal(inputs, targets, target_space, hparams, cache=None, predict_mask=1.0): # Summaries break with the do_refine cond, turn them off in that case. global _DO_SUMMARIES if hparams.do_refine: _DO_SUMMARIES = False # Prepare. if inputs is not None: batch_size = common_layers.shape_list(inputs)[0] else: batch_size = common_layers.shape_list(targets)[0] targets = tf.reshape(targets, [batch_size, -1, 1, hparams.hidden_size]) # Encoder. if inputs is not None: inputs = common_layers.flatten4d3d(inputs) inputs, ed = encode(inputs, target_space, hparams, "input_enc") inputs_ex, ed_ex = inputs, ed else: ed, inputs_ex, ed_ex = None, None, None # Autoencoding. losses = {"extra": tf.constant(0.0), "latent_pred": tf.constant(0.0), "neg_q_entropy": tf.constant(0.0)} if hparams.do_ae: # flatten here original_targets = targets original_targets_shape = tf.shape(original_targets) if hparams.task == "image": cia.maybe_reshape_4d_to_3d(targets) if hparams.task == "translate": if inputs is not None: max_targets_len_from_inputs = tf.concat([inputs, inputs], axis=1) else: max_targets_len_from_inputs = targets else: assert hparams.task == "image" max_targets_len_from_inputs = targets if hparams.word_shuffle: tf.logging.info("Using word shuffle with rate = {}".format( hparams.word_shuffle)) targets_idx = tf.range(start=0, limit=common_layers.shape_list(targets)[1], delta=1) targets_idx = tf.to_float(targets_idx) noise = tf.random_uniform(shape=common_layers.shape_list(targets_idx), minval=0, maxval=1 + hparams.word_shuffle) targets_idx += noise permutation = tf.contrib.framework.argsort(targets_idx) targets_permuted = tf.gather(targets, indices=permutation, axis=1) targets = targets_permuted targets, _ = common_layers.pad_to_same_length( targets, max_targets_len_from_inputs, final_length_divisible_by=2**hparams.num_compress_steps) # Add positional information targets_shape = common_layers.shape_list(targets) targets = tf.reshape(targets, [targets_shape[0], targets_shape[1], targets_shape[3]]) targets = common_attention.add_positional_embedding( targets, hparams.max_length, name="targets_position") targets = tf.reshape(targets, shape=targets_shape) if hparams.word_dropout: mask = tf.random_uniform(shape=common_layers.shape_list(targets), minval=0.0, maxval=1.0) targets_noisy = tf.where(mask > hparams.word_dropout, targets, tf.zeros_like(targets)) else: targets_noisy = targets targets_c = compress(targets_noisy, inputs, False, hparams, "compress") if hparams.mode != tf.estimator.ModeKeys.PREDICT: # Compress and bottleneck. latents_dense, latents_discrete, extra_loss, embed, neg_q_entropy = ( hparams.bottleneck(inputs=targets_c, filter_size=hparams.compress_filter_size, mode=hparams.mode, name="vc")) if _DO_SUMMARIES: tf.summary.histogram("b0", tf.reshape(latents_discrete[:, 0, :], [-1])) pc = common_layers.inverse_exp_decay(hparams.startup_steps) pc = pc if hparams.mode == tf.estimator.ModeKeys.TRAIN else 1.0 cond = tf.less(tf.random_uniform([batch_size]), pc) latents_dense = tf.where(cond, latents_dense, targets_c) # TODO(lukaszkaiser): return extra losses batchwise, multiply before mean. losses["extra"] = extra_loss * tf.reduce_mean(tf.to_float(cond)) # Extra loss predicting latent code from input. Discrete only. if hparams.bottleneck_kind not in ["dense", "vae"]: latents_pred = decode_transformer( inputs_ex, ed_ex, embed(latents_discrete), hparams, "extra", task="translate") _, latent_pred_loss = ae_latent_softmax( latents_pred, tf.stop_gradient(latents_discrete), hparams) # Scale by latent dimension for summary so we can compare across # batches. if _DO_SUMMARIES: tf.summary.scalar("latent_pred_loss_mean", tf.reduce_mean(latent_pred_loss)) if hparams.sum_over_latents: latent_pred_loss = tf.reduce_sum(latent_pred_loss, [1, 2]) losses["latent_pred"] = tf.reduce_mean( latent_pred_loss * tf.to_float(cond)) * hparams.prior_scale losses["neg_q_entropy"] = neg_q_entropy * hparams.entropy_scale else: inputs_c = decode_transformer(inputs, ed, targets_c, hparams, "dec_c") losses["latent_pred"] = tf.reduce_mean( tf.squared_difference(inputs_c, targets_c)) * 20 def bn_inputs(): with tf.variable_scope(tf.get_variable_scope(), reuse=True): bn, _, _, _, _ = hparams.bottleneck( inputs=inputs_c, filter_size=hparams.compress_filter_size, mode=hparams.mode, name="vc") return bn inputs_c = bn_inputs() ptc = 1.0 - common_layers.inverse_lin_decay(200000) * 0.5 ptc = ptc if hparams.mode == tf.estimator.ModeKeys.TRAIN else 1.0 latents_dense = tf.where(tf.less(tf.random_uniform([batch_size]), ptc), latents_dense, inputs_c) else: if hparams.bottleneck_kind in ["dense", "vae"]: inputs_c = decode_transformer(inputs, ed, targets_c, hparams, "dec_c") latents_dense, _, _, _, _ = hparams.bottleneck( inputs=inputs_c, filter_size=hparams.compress_filter_size, mode=hparams.mode, name="vc") else: latent_len = common_layers.shape_list(targets_c)[1] _, _, _, embed, _ = hparams.bottleneck( inputs=targets_c, filter_size=hparams.compress_filter_size, name="vc") latents_dense = tf.zeros_like(targets_c[:, :latent_len, :, :]) if cache is None: cache = ae_latent_sample( latents_dense, inputs_ex, ed_ex, embed, 16, hparams) latents_dense = embed(cache) # Postprocess. d = latents_dense d_shape = common_layers.shape_list(d) d = tf.reshape(d, [d_shape[0], d_shape[1], d_shape[3]]) d = common_attention.add_positional_embedding( d, hparams.max_length, name="latents_position") d = tf.reshape(d, shape=d_shape) # decompressing the dense latents for i in range(hparams.num_compress_steps): j = hparams.num_compress_steps - i - 1 d = residual_conv(d, 1, (3, 1), hparams, "decompress_rc_%d" % j) if inputs is not None and hparams.do_attend_decompress: d = attend(d, inputs, hparams, "decompress_attend_%d" % j) d = decompress_step(d, hparams, i > 0, False, "decompress_%d" % j) # Masking. if hparams.do_mask: masking = common_layers.inverse_lin_decay(hparams.mask_startup_steps) masking *= common_layers.inverse_exp_decay( hparams.mask_startup_steps // 4) # Not much at start. if not hparams.do_refine: masking -= tf.random_uniform([]) * hparams.unmasked_percentage masking = tf.minimum(tf.maximum(masking, 0.0), 1.0) if hparams.use_predict_mask: masking = predict_mask if hparams.mode == tf.estimator.ModeKeys.PREDICT: masking = predict_mask mask = tf.less(masking, tf.random_uniform( common_layers.shape_list(targets)[:-1])) mask = tf.expand_dims(tf.to_float(mask), 3) # targets is always [batch, length, 1, depth] targets = mask * targets + (1.0 - mask) * d # reshape back to 4d here if hparams.task == "image": targets = tf.reshape(targets, original_targets_shape) res = decode_transformer(inputs, ed, targets, hparams, "decoder", causal=hparams.causal) if hparams.do_ae: if hparams.do_mask and hparams.do_refine: def refine_res(): # return residual_conv(res, 1, (5, 1), hparams, "refine") r, _ = encode(tf.squeeze(res, axis=[2]), target_space, hparams, "refine_enc") return tf.expand_dims(r, axis=2) masked_batches = tf.reduce_sum(mask, axis=[1, 2, 3]) all_masked = tf.less(masked_batches, 0.1) res = tf.where(all_masked, refine_res(), res) # We'll start training the extra model of latents after mask_startup_steps. nonlatent_steps = hparams.mask_startup_steps latent_time = tf.less(nonlatent_steps, tf.to_int32(tf.train.get_global_step())) losses["latent_pred"] *= tf.to_float(latent_time) # res was generated from padded targets, which means it has some extra # elements. These can cause shape problems when computing loss with respect to # the original (unpadded) targets. So we remove their extra elements here. res = res[:, :original_targets_shape[1], :, :] data_dim = common_layers.shape_list(res)[1] latent_dim = common_layers.shape_list(targets_c)[1] return res, losses, cache, data_dim, latent_dim
[ "AE Transformer, main step used for training." ]
Please provide a description of the function:def transformer_ae_small(): hparams = transformer.transformer_small() hparams.batch_size = 2048 hparams.learning_rate = 0.2 hparams.learning_rate_warmup_steps = 4000 hparams.num_hidden_layers = 3 hparams.hidden_size = 384 hparams.filter_size = 2048 hparams.add_hparam("compress_filter_size", 2048 * 2) hparams.label_smoothing = 0.0 hparams.optimizer = "adam" # Can be unstable, maybe try Adam. hparams.optimizer_adam_epsilon = 1e-9 hparams.optimizer_adam_beta1 = 0.9 hparams.optimizer_adam_beta2 = 0.997 # Needs tuning, try 0.98 to 0.999. hparams.add_hparam("z_size", 14) hparams.add_hparam("noise_dev", 0.5) hparams.add_hparam("d_mix", 0.5) hparams.add_hparam("logit_normalization", True) hparams.add_hparam("word_dropout", 0.) # Bottleneck kinds supported: dense, vae, semhash, gumbel-softmax, dvq. hparams.add_hparam("bottleneck_kind", "semhash") hparams.add_hparam("num_blocks", 1) hparams.add_hparam("num_decode_blocks", 1) # Add an hparam for number of reiduals hparams.add_hparam("num_residuals", 1) # Reshape method for DVQ: slice, project hparams.add_hparam("word_shuffle", 0.5) hparams.add_hparam("causal", True) hparams.add_hparam("reshape_method", "slice") hparams.add_hparam("trainable_projections", False) hparams.add_hparam("unmasked_percentage", 0.1) hparams.add_hparam("do_ae", True) hparams.add_hparam("do_mask", True) hparams.add_hparam("use_predict_mask", True) hparams.add_hparam("do_refine", False) hparams.add_hparam("do_attend_compress", False) hparams.add_hparam("do_attend_decompress", True) hparams.add_hparam("do_residual_compress", False) hparams.add_hparam("drop_inputs", False) hparams.add_hparam("v_size", 1024*64) hparams.add_hparam("max_context_length", 64) hparams.add_hparam("num_compress_steps", 3) hparams.add_hparam("startup_steps", 10000) hparams.add_hparam("mask_startup_steps", 50000) hparams.add_hparam("z_dropout", 0.1) hparams.add_hparam("is_2d", 0) hparams.add_hparam("softmax_k", 0) hparams.add_hparam("decode_autoregressive", True) hparams.add_hparam("do_vae", True) hparams.add_hparam("bit_vae", True) hparams.add_hparam("beta", 0.25) hparams.add_hparam("epsilon", 1e-5) hparams.add_hparam("decay", 0.999) hparams.add_hparam("ema", True) hparams.add_hparam("random_top_k", 1) hparams.add_hparam("soft_em", False) hparams.add_hparam("num_samples", 10) hparams.add_hparam("inv_temp", 1.0) hparams.add_hparam("entropy_scale", 0.0) hparams.add_hparam("prior_scale", 1.0) hparams.add_hparam("do_hard_gumbel_softmax", False) hparams.add_hparam("num_flows", 0) hparams.add_hparam("approximate_gs_entropy", False) hparams.add_hparam("temperature_warmup_steps", 150000) hparams.add_hparam("sum_over_latents", False) hparams.force_full_predict = True # task params hparams.add_hparam("task", "translate") # translate or image tasks supported return hparams
[ "Set of hyperparameters." ]
Please provide a description of the function:def imagetransformer_ae_cifar(): hparams = transformer_ae_small() hparams.filter_size = 512 hparams.num_compress_steps = 3 hparams.startup_steps = 10000 hparams.is_2d = 0 hparams.learning_rate_warmup_steps = 8000 hparams.learning_rate = 0.2 hparams.hidden_size = 512 hparams.batch_size = 1 hparams.max_length = 256 hparams.dropout = 0.0 hparams.clip_grad_norm = 0. # i.e. no gradient clipping hparams.optimizer_adam_epsilon = 1e-9 hparams.learning_rate_decay_scheme = "noam" hparams.learning_rate = 0.1 hparams.initializer_gain = 0.2 hparams.num_hidden_layers = 6 hparams.initializer = "uniform_unit_scaling" hparams.weight_decay = 0.0 hparams.optimizer_adam_beta1 = 0.9 hparams.optimizer_adam_beta2 = 0.98 hparams.label_smoothing = 0.0 hparams.norm_type = "layer" hparams.layer_prepostprocess_dropout = 0.0 hparams.num_heads = 8 hparams.task = "image" hparams.ffn_layer = "conv_hidden_relu" # All hyperparameters ending in "dropout" are automatically set to 0.0 # when not in training mode. hparams.attention_dropout = 0.0 hparams.relu_dropout = 0. hparams.pos = "timing" # timing, none hparams.nbr_decoder_problems = 1 hparams.num_output_layers = 3 # TODO(trandustin): semhash doesn't work if filter_size != hidden_size. For # now, set default to dvq. hparams.bottleneck_kind = "dvq" hparams.add_hparam("block_size", 1) # dilated attention based flags hparams.add_hparam("gap_sizes", [2, 4, 8, 16, 32, 64, 2, 4, 8, 16, 32, 64]) hparams.add_hparam("dilated_attention", False) # image size related flags # assuming that the image has same height and width hparams.add_hparam("img_len", 32) hparams.add_hparam("num_channels", 3) # Local attention params hparams.add_hparam("local_and_global_att", False) hparams.add_hparam("block_length", 256) hparams.add_hparam("block_width", 128) hparams.num_encoder_layers = 4 hparams.num_decoder_layers = 12 hparams.add_hparam("dec_attention_type", cia.AttentionType.LOCAL_1D) hparams.add_hparam("block_raster_scan", False) hparams.add_hparam("shared_rel", False) # multipos attention params hparams.add_hparam("q_filter_width", 1) hparams.add_hparam("kv_filter_width", 1) hparams.add_hparam("unconditional", False) # unconditional generation hparams.bottom["targets"] = modalities.image_channel_embeddings_bottom hparams.top["targets"] = modalities.image_channel_embeddings_top hparams.drop_inputs = True hparams.do_attend_compress = False hparams.do_attend_decompress = False return hparams
[ "Hyperparameters for CIFAR-10 experiments." ]
Please provide a description of the function:def imagetransformer_ae_imagenet(): hparams = imagetransformer_ae_cifar() hparams.max_length = int(64 * 64 * 3) hparams.img_len = 64 hparams.num_heads = 4 # Heads are expensive on TPUs. # Reduce architecture from 32x32 CIFAR-10 in order to fit in memory. hparams.num_decoder_layers = 8 hparams.num_compress_steps = 2 return hparams
[ "For 64x64 ImageNet. ~56M trainable variables." ]
Please provide a description of the function:def transformer_ae_base(): hparams = transformer_ae_small() hparams.batch_size = 2048 hparams.hidden_size = 512 hparams.filter_size = 4096 hparams.num_hidden_layers = 6 return hparams
[ "Set of hyperparameters." ]
Please provide a description of the function:def transformer_ae_a3(): hparams = transformer_ae_base() hparams.batch_size = 4096 hparams.layer_prepostprocess_dropout = 0.3 hparams.optimizer = "Adafactor" hparams.learning_rate = 0.25 hparams.learning_rate_warmup_steps = 10000 return hparams
[ "Set of hyperparameters." ]
Please provide a description of the function:def transformer_ae_base_noatt(): hparams = transformer_ae_base() hparams.reshape_method = "slice" hparams.bottleneck_kind = "dvq" hparams.hidden_size = 512 hparams.num_blocks = 1 hparams.num_decode_blocks = 1 hparams.z_size = 12 hparams.do_attend_decompress = False return hparams
[ "Set of hyperparameters." ]
Please provide a description of the function:def transformer_ae_small_noatt(): hparams = transformer_ae_small() hparams.reshape_method = "slice" hparams.bottleneck_kind = "dvq" hparams.hidden_size = 512 hparams.num_blocks = 1 hparams.num_decode_blocks = 1 hparams.z_size = 12 hparams.do_attend_decompress = False return hparams
[ "Set of hyperparameters." ]
Please provide a description of the function:def transformer_sketch(): hparams = transformer.transformer_small() hparams.num_compress_steps = 4 hparams.batch_size = 32 hparams.clip_grad_norm = 2. hparams.sampling_method = "random" return hparams
[ "Basic transformer_sketch hparams." ]
Please provide a description of the function:def layers(): global _cached_layers if _cached_layers is not None: return _cached_layers layers_module = tf.layers try: from tensorflow.python import tf2 # pylint: disable=g-direct-tensorflow-import,g-import-not-at-top if tf2.enabled(): tf.logging.info("Running in V2 mode, using Keras layers.") layers_module = tf.keras.layers except ImportError: pass _cached_layers = layers_module return layers_module
[ "Get the layers module good for TF 1 and TF 2 work for now." ]
Please provide a description of the function:def dropout_with_broadcast_dims(x, keep_prob, broadcast_dims=None, **kwargs): assert "noise_shape" not in kwargs if broadcast_dims: shape = tf.shape(x) ndims = len(x.get_shape()) # Allow dimensions like "-1" as well. broadcast_dims = [dim + ndims if dim < 0 else dim for dim in broadcast_dims] kwargs["noise_shape"] = [ 1 if i in broadcast_dims else shape[i] for i in range(ndims) ] return tf.nn.dropout(x, keep_prob, **kwargs)
[ "Like tf.nn.dropout but takes broadcast_dims instead of noise_shape.\n\n Instead of specifying noise_shape, this function takes broadcast_dims -\n a list of dimension numbers in which noise_shape should be 1. The random\n keep/drop tensor has dimensionality 1 along these dimensions.\n\n Args:\n x: a floating point tensor.\n keep_prob: A scalar Tensor with the same type as x.\n The probability that each element is kept.\n broadcast_dims: an optional list of integers\n the dimensions along which to broadcast the keep/drop flags.\n **kwargs: keyword arguments to tf.nn.dropout other than \"noise_shape\".\n\n Returns:\n Tensor of the same shape as x.\n " ]
Please provide a description of the function:def saturating_sigmoid(x): with tf.name_scope("saturating_sigmoid", values=[x]): y = tf.sigmoid(x) return tf.minimum(1.0, tf.maximum(0.0, 1.2 * y - 0.1))
[ "Saturating sigmoid: 1.2 * sigmoid(x) - 0.1 cut to [0, 1]." ]
Please provide a description of the function:def inverse_exp_decay(max_step, min_value=0.01, step=None): inv_base = tf.exp(tf.log(min_value) / float(max_step)) if step is None: step = tf.train.get_global_step() if step is None: return 1.0 step = to_float(step) return inv_base**tf.maximum(float(max_step) - step, 0.0)
[ "Inverse-decay exponentially from 0.01 to 1.0 reached at max_step." ]
Please provide a description of the function:def inverse_lin_decay(max_step, min_value=0.01, step=None): if step is None: step = tf.train.get_global_step() if step is None: return 1.0 step = to_float(step) progress = tf.minimum(step / float(max_step), 1.0) return progress * (1.0 - min_value) + min_value
[ "Inverse-decay linearly from 0.01 to 1.0 reached at max_step." ]
Please provide a description of the function:def shakeshake2_py(x, y, equal=False, individual=False): if equal: alpha = 0.5 elif individual: alpha = tf.random_uniform(tf.get_shape(x)[:1]) else: alpha = tf.random_uniform([]) return alpha * x + (1.0 - alpha) * y
[ "The shake-shake sum of 2 tensors, python version." ]
Please provide a description of the function:def shakeshake2_grad(x1, x2, dy): y = shakeshake2_py(x1, x2) dx = tf.gradients(ys=[y], xs=[x1, x2], grad_ys=[dy]) return dx
[ "Overriding gradient for shake-shake of 2 tensors." ]
Please provide a description of the function:def shakeshake2_indiv_grad(x1, x2, dy): y = shakeshake2_py(x1, x2, individual=True) dx = tf.gradients(ys=[y], xs=[x1, x2], grad_ys=[dy]) return dx
[ "Overriding gradient for shake-shake of 2 tensors." ]
Please provide a description of the function:def shakeshake2_equal_grad(x1, x2, dy): y = shakeshake2_py(x1, x2, equal=True) dx = tf.gradients(ys=[y], xs=[x1, x2], grad_ys=[dy]) return dx
[ "Overriding gradient for shake-shake of 2 tensors." ]
Please provide a description of the function:def shakeshake(xs, equal_grad=False): if len(xs) == 1: return xs[0] div = (len(xs) + 1) // 2 arg1 = shakeshake(xs[:div], equal_grad=equal_grad) arg2 = shakeshake(xs[div:], equal_grad=equal_grad) if equal_grad: return shakeshake2_eqgrad(arg1, arg2) return shakeshake2(arg1, arg2)
[ "Multi-argument shake-shake, currently approximated by sums of 2." ]
Please provide a description of the function:def convert_rgb_to_real(x): with tf.name_scope("rgb_to_real", values=[x]): x = to_float(x) x /= 255.0 return x
[ "Conversion of pixel values to real numbers." ]
Please provide a description of the function:def convert_rgb_to_symmetric_real(x): with tf.name_scope("rgb_to_real", values=[x]): x = to_float(x) # Convert each pixel intensity in [0, 1, 2, ..., 255] into a real number in # the range [-1, 1]. x = (x / 127.5) - 1 return x
[ "Conversion of pixel values to real numbers." ]
Please provide a description of the function:def expand_squeeze_to_nd(x, n, squeeze_dim=2, expand_dim=-1): if len(x.shape) > n: while len(x.shape) != n: x = tf.squeeze(x, [squeeze_dim]) else: while len(x.shape) != n: x = tf.expand_dims(x, expand_dim) return x
[ "Make x n-d with squeeze and expand_dims." ]
Please provide a description of the function:def standardize_images(x): with tf.name_scope("standardize_images", values=[x]): x_shape = shape_list(x) x = to_float(tf.reshape(x, [-1] + x_shape[-3:])) x_mean = tf.reduce_mean(x, axis=[1, 2], keepdims=True) x_variance = tf.reduce_mean( tf.squared_difference(x, x_mean), axis=[1, 2], keepdims=True) num_pixels = to_float(x_shape[-2] * x_shape[-3]) x = (x - x_mean) / tf.maximum(tf.sqrt(x_variance), tf.rsqrt(num_pixels)) return tf.reshape(x, x_shape)
[ "Image standardization on batches and videos." ]
Please provide a description of the function:def flatten4d3d(x): xshape = shape_list(x) result = tf.reshape(x, [xshape[0], xshape[1] * xshape[2], xshape[3]]) return result
[ "Flatten a 4d-tensor into a 3d-tensor by joining width and height." ]
Please provide a description of the function:def gather(params, indices, dtype=tf.float32): if not is_xla_compiled(): return tf.gather(params, indices) vocab_size = params.get_shape().as_list()[0] indices_flat = tf.reshape(indices, [-1]) out = tf.matmul(tf.one_hot(indices_flat, vocab_size, dtype=dtype), params) out = reshape_like(out, tf.expand_dims(indices, -1)) return out
[ "Version of tf.gather that works faster on tpu." ]
Please provide a description of the function:def cumsum(x, axis=0, exclusive=False): if not is_xla_compiled(): return tf.cumsum(x, axis=axis, exclusive=exclusive) x_shape = shape_list(x) rank = len(x_shape) length = x_shape[axis] my_range = tf.range(length) comparator = tf.less if exclusive else tf.less_equal mask = tf.cast( comparator(tf.expand_dims(my_range, 1), tf.expand_dims(my_range, 0)), x.dtype) ret = tf.tensordot(x, mask, axes=[[axis], [0]]) if axis != rank - 1: ret = tf.transpose( ret, list(range(axis)) + [rank - 1] + list(range(axis, rank - 1))) return ret
[ "TPU hack for tf.cumsum.\n\n This is equivalent to tf.cumsum and is faster on TPU as of 04/2018 unless\n the axis dimension is very large.\n\n Args:\n x: a Tensor\n axis: an integer\n exclusive: a boolean\n\n Returns:\n Tensor of the same shape as x.\n " ]
Please provide a description of the function:def dropout_no_scaling(x, keep_prob): if keep_prob == 1.0: return x mask = tf.less(tf.random_uniform(tf.shape(x)), keep_prob) return x * cast_like(mask, x)
[ "Like tf.nn.dropout, but does not scale up. Works on integers also.\n\n Args:\n x: a Tensor\n keep_prob: a floating point number\n\n Returns:\n Tensor of the same shape as x.\n " ]