INSTRUCTION
stringlengths 1
46.3k
| RESPONSE
stringlengths 75
80.2k
|
---|---|
Computes the value loss.
Args:
value_net_apply: value net apply function with signature (params, ndarray of
shape (B, T+1) + OBS) -> ndarray(B, T+1, 1)
value_net_params: params of value_net_apply.
observations: np.ndarray of shape (B, T+1) + OBS
rewards: np.ndarray of shape (B, T) of rewards.
reward_mask: np.ndarray of shape (B, T), the mask over rewards.
gamma: float, discount factor.
Returns:
The average L2 value loss, averaged over instances where reward_mask is 1. | def value_loss(value_net_apply,
value_net_params,
observations,
rewards,
reward_mask,
gamma=0.99):
"""Computes the value loss.
Args:
value_net_apply: value net apply function with signature (params, ndarray of
shape (B, T+1) + OBS) -> ndarray(B, T+1, 1)
value_net_params: params of value_net_apply.
observations: np.ndarray of shape (B, T+1) + OBS
rewards: np.ndarray of shape (B, T) of rewards.
reward_mask: np.ndarray of shape (B, T), the mask over rewards.
gamma: float, discount factor.
Returns:
The average L2 value loss, averaged over instances where reward_mask is 1.
"""
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 given the prediction of the value function.
Args:
value_prediction: np.ndarray of shape (B, T+1, 1)
rewards: np.ndarray of shape (B, T) of rewards.
reward_mask: np.ndarray of shape (B, T), the mask over rewards.
gamma: float, discount factor.
Returns:
The average L2 value loss, averaged over instances where reward_mask is 1. | def value_loss_given_predictions(value_prediction,
rewards,
reward_mask,
gamma=0.99):
"""Computes the value loss given the prediction of the value function.
Args:
value_prediction: np.ndarray of shape (B, T+1, 1)
rewards: np.ndarray of shape (B, T) of rewards.
reward_mask: np.ndarray of shape (B, T), the mask over rewards.
gamma: float, discount factor.
Returns:
The average L2 value loss, averaged over instances where reward_mask is 1.
"""
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) |
r"""Computes TD-residuals from V(s) and rewards.
Where a `delta`, i.e. a td-residual is defined as:
delta_{b,t} = r_{b,t} + \gamma * v_{b,t+1} - v_{b,t}.
Args:
predicted_values: ndarray of shape (B, T+1). NOTE: Expects axis 2 was
squeezed. These represent V(s_bt) for b < B and t < T+1
rewards: ndarray of shape (B, T) of rewards.
mask: ndarray of shape (B, T) of mask for rewards.
gamma: float, discount factor.
Returns:
ndarray of shape (B, T) of one-step TD-residuals. | def deltas(predicted_values, rewards, mask, gamma=0.99):
r"""Computes TD-residuals from V(s) and rewards.
Where a `delta`, i.e. a td-residual is defined as:
delta_{b,t} = r_{b,t} + \gamma * v_{b,t+1} - v_{b,t}.
Args:
predicted_values: ndarray of shape (B, T+1). NOTE: Expects axis 2 was
squeezed. These represent V(s_bt) for b < B and t < T+1
rewards: ndarray of shape (B, T) of rewards.
mask: ndarray of shape (B, T) of mask for rewards.
gamma: float, discount factor.
Returns:
ndarray of shape (B, T) of one-step TD-residuals.
"""
# `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 |
r"""Computes the GAE advantages given the one step TD-residuals.
The formula for a GAE advantage estimator is as follows:
A_{bt} = \sum_{l=0}^{\infty}(\gamma * \lambda)^{l}(\delta_{b,t+l}).
Internally we just call rewards_to_go, since it is the same computation.
Args:
td_deltas: np.ndarray of shape (B, T) of one step TD-residuals.
mask: np.ndarray of shape (B, T) of mask for the residuals. It maybe the
case that the `td_deltas` are already masked correctly since they are
produced by `deltas(...)`
lambda_: float, lambda parameter for GAE estimators.
gamma: float, lambda parameter for GAE estimators.
Returns:
GAE advantage estimates. | def gae_advantages(td_deltas, mask, lambda_=0.95, gamma=0.99):
r"""Computes the GAE advantages given the one step TD-residuals.
The formula for a GAE advantage estimator is as follows:
A_{bt} = \sum_{l=0}^{\infty}(\gamma * \lambda)^{l}(\delta_{b,t+l}).
Internally we just call rewards_to_go, since it is the same computation.
Args:
td_deltas: np.ndarray of shape (B, T) of one step TD-residuals.
mask: np.ndarray of shape (B, T) of mask for the residuals. It maybe the
case that the `td_deltas` are already masked correctly since they are
produced by `deltas(...)`
lambda_: float, lambda parameter for GAE estimators.
gamma: float, lambda parameter for GAE estimators.
Returns:
GAE advantage estimates.
"""
return rewards_to_go(td_deltas, mask, lambda_ * gamma) |
Picks out the probabilities of the actions along batch and time-steps.
Args:
probab_observations: ndarray of shape `[B, T+1, A]`, where
probab_observations[b, t, i] contains the log-probability of action = i at
the t^th time-step in the b^th trajectory.
actions: ndarray of shape `[B, T]`, with each entry in [0, A) denoting which
action was chosen in the b^th trajectory's t^th time-step.
Returns:
`[B, T]` ndarray with the log-probabilities of the chosen actions. | def chosen_probabs(probab_observations, actions):
"""Picks out the probabilities of the actions along batch and time-steps.
Args:
probab_observations: ndarray of shape `[B, T+1, A]`, where
probab_observations[b, t, i] contains the log-probability of action = i at
the t^th time-step in the b^th trajectory.
actions: ndarray of shape `[B, T]`, with each entry in [0, A) denoting which
action was chosen in the b^th trajectory's t^th time-step.
Returns:
`[B, T]` ndarray with the log-probabilities of the chosen 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] |
Computes the probability ratios for each time-step in a trajectory.
Args:
p_new: ndarray of shape [B, T+1, A] of the log-probabilities that the policy
network assigns to all the actions at each time-step in each batch using
the old parameters.
p_old: ndarray of shape [B, T+1, A], same as above, but using old policy
network parameters.
actions: ndarray of shape [B, T] where each element is from [0, A).
reward_mask: ndarray of shape [B, T] masking over probabilities.
Returns:
probab_ratios: ndarray of shape [B, T], where
probab_ratios_{b,t} = p_new_{b,t,action_{b,t}} / p_old_{b,t,action_{b,t}} | def compute_probab_ratios(p_new, p_old, actions, reward_mask):
"""Computes the probability ratios for each time-step in a trajectory.
Args:
p_new: ndarray of shape [B, T+1, A] of the log-probabilities that the policy
network assigns to all the actions at each time-step in each batch using
the old parameters.
p_old: ndarray of shape [B, T+1, A], same as above, but using old policy
network parameters.
actions: ndarray of shape [B, T] where each element is from [0, A).
reward_mask: ndarray of shape [B, T] masking over probabilities.
Returns:
probab_ratios: ndarray of shape [B, T], where
probab_ratios_{b,t} = p_new_{b,t,action_{b,t}} / p_old_{b,t,action_{b,t}}
"""
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 |
PPO objective, with an eventual minus sign, given observations. | 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):
"""PPO objective, with an eventual minus sign, given observations."""
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 predictions. | 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):
"""PPO objective, with an eventual minus sign, given predictions."""
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 |
Computes the combined (clipped loss + value loss) given predictions. | 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):
"""Computes the combined (clipped loss + value loss) given predictions."""
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 observations. | 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):
"""Computes the combined (clipped loss + value loss) given observations."""
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) |
PPO optimizer step. | 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):
"""PPO optimizer step."""
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) |
Value optimizer step. | def value_opt_step(i,
opt_state,
opt_update,
value_net_apply,
padded_observations,
padded_rewards,
reward_mask,
gamma=0.99):
"""Value optimizer step."""
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) |
Policy and Value optimizer step. | 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):
"""Policy and Value optimizer step."""
# Combined loss function given the new params.
def policy_and_value_loss(params):
"""Returns the combined loss given just parameters."""
(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) |
Runs the training loop for PPO, with fixed policy and value nets. | 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):
"""Runs the training loop for PPO, with fixed policy and value nets."""
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)) |
Download corpora for multinli.
Args:
tmp_dir: a string
Returns:
a string | def _maybe_download_corpora(tmp_dir):
"""Download corpora for multinli.
Args:
tmp_dir: a string
Returns:
a string
"""
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 |
Generate mnli examples.
Args:
filename: a string
Yields:
dictionaries containing "premise", "hypothesis" and "label" strings | def _example_generator(filename):
"""Generate mnli examples.
Args:
filename: a string
Yields:
dictionaries containing "premise", "hypothesis" and "label" strings
"""
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]
} |
Adds a residual connection to the filter x for the shake-shake model. | def shake_shake_skip_connection(x, output_filters, stride, is_training):
"""Adds a residual connection to the filter x for the shake-shake model."""
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 |
Building a 2 branching convnet. | def shake_shake_branch(x, output_filters, stride, rand_forward, rand_backward,
hparams):
"""Building a 2 branching convnet."""
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 |
Builds a full shake-shake sub layer. | def shake_shake_block(x, output_filters, stride, hparams):
"""Builds a full shake-shake sub layer."""
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 many sub layers into one full layer. | def shake_shake_layer(x, output_filters, num_blocks, stride, hparams):
"""Builds many sub layers into one full layer."""
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 |
Parameters for CIFAR-10. Gets to about 96% accuracy@700K steps, 1 GPU. | def shakeshake_small():
"""Parameters for CIFAR-10. Gets to about 96% accuracy@700K steps, 1 GPU."""
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 |
Check if metric has plateaued.
A metric has plateaued if the value has not increased/decreased (depending on
`decrease`) by `delta` for at least `num_steps`.
Args:
steps: list<int> list of global steps for values.
values: list<float> list of metric values.
num_steps: int, number of steps the metric has to have been plateaued for.
delta: float, how much the metric should have changed by over num_steps.
decrease: bool, whether to check if the metric has decreased by delta or
increased by delta.
Returns:
bool, whether the metric has plateaued. | def has_metric_plateaued(steps, values, num_steps=100, delta=0.1,
decrease=True):
"""Check if metric has plateaued.
A metric has plateaued if the value has not increased/decreased (depending on
`decrease`) by `delta` for at least `num_steps`.
Args:
steps: list<int> list of global steps for values.
values: list<float> list of metric values.
num_steps: int, number of steps the metric has to have been plateaued for.
delta: float, how much the metric should have changed by over num_steps.
decrease: bool, whether to check if the metric has decreased by delta or
increased by delta.
Returns:
bool, whether the metric has plateaued.
"""
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) |
SAVP model hparams. | def next_frame_savp():
"""SAVP model hparams."""
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 - VAE only model. | def next_frame_savp_vae():
"""SAVP - VAE only model."""
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 |
Default hyperparameters for a DietAdamOptimizer.
Returns:
a hyperparameters object. | def diet_adam_optimizer_params():
"""Default hyperparameters for a DietAdamOptimizer.
Returns:
a hyperparameters object.
"""
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
) |
SAVP - GAN only model. | def next_frame_savp_gan():
"""SAVP - GAN only model."""
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 |
A two-layer feed-forward network with relu activation on hidden layer.
Uses diet variables.
Recomputes hidden layer on backprop to save activation memory.
Args:
x: a Tensor with shape [batch, io_size]
hidden_size: an integer
params: a diet variable HParams object.
Returns:
a Tensor with shape [batch, io_size] | def diet_expert(x, hidden_size, params):
"""A two-layer feed-forward network with relu activation on hidden layer.
Uses diet variables.
Recomputes hidden layer on backprop to save activation memory.
Args:
x: a Tensor with shape [batch, io_size]
hidden_size: an integer
params: a diet variable HParams object.
Returns:
a Tensor with shape [batch, io_size]
"""
@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) |
Quantize x according to params, optionally randomizing the rounding. | def _quantize(x, params, randomize=True):
"""Quantize x according to params, optionally randomizing the rounding."""
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 |
Dequantize q according to params. | def _dequantize(q, params):
"""Dequantize q according to params."""
if not params.quantize:
return q
return tf.to_float(tf.bitcast(q, tf.int16)) * params.quantization_scale |
Create a custom variable getter for diet variables according to params. | def make_diet_var_getter(params):
"""Create a custom variable getter for diet variables according to params."""
def diet_var_initializer(shape, dtype, partition_info=None):
"""Initializer for a diet variable."""
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):
"""Get diet variable and return it dequantized."""
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 |
Call function with args; use diet variables according to params. | def _fn_with_diet_vars(fn, args, params):
"""Call function with args; use diet variables according to params."""
vs_ctr = []
def grad_fn(inputs, variables, outputs, output_grads):
"""Custom gradient function."""
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 |
Decorator for graph-building function to use diet variables. | def fn_with_diet_vars(params):
"""Decorator for graph-building function to use diet variables."""
params = copy.copy(params)
def dec(fn):
def wrapped(*args):
return _fn_with_diet_vars(fn, args, params)
return wrapped
return dec |
Create the factorized Adam accumulators for diet variables. | def create_slots(self, var):
"""Create the factorized Adam accumulators for diet variables."""
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()) |
Update the variable and its slots. | def update_variable(self, var, grad_var):
"""Update the variable and its slots."""
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) |
Construct EstimatorSpec for EVAL mode. | def estimator_spec_eval(
self, features, logits, labels, loss, restore_hook, use_tpu):
"""Construct EstimatorSpec for EVAL mode."""
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) |
Generator for the dataset samples.
If not present, download and extract the dataset.
Args:
tmp_dir: path to the directory where to download the dataset.
pb_cst: CodingPbConstants object defining paths
Yields:
A CodingPbInfo object containing the next challenge informations. | def generator_samples(tmp_dir, pb_cst):
"""Generator for the dataset samples.
If not present, download and extract the dataset.
Args:
tmp_dir: path to the directory where to download the dataset.
pb_cst: CodingPbConstants object defining paths
Yields:
A CodingPbInfo object containing the next challenge informations.
"""
# 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
"""Check that the folder contains a problem."""
return (
_DESC_DIR_NAME in dirs and
pb_cst.code_dir_name in dirs
)
def next_sample(subdir, dirs, files): # pylint: disable=unused-argument
"""Return the filenames of the problem."""
# 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) |
Adds a stack of LSTM layers on top of input.
Args:
inputs: The input `Tensor`, shaped `[batch_size, time_steps, hidden_size]`.
sequence_length: Lengths of the actual input sequence, excluding padding; a
`Tensor` shaped `[batch_size]`.
hparams: HParams; hyperparameters.
train: bool; `True` when constructing training graph to enable dropout.
name: string; Create variable names under this scope.
initial_state: tuple of `LSTMStateTuple`s; the initial state of each layer.
Returns:
A tuple (outputs, states), where:
outputs: The output `Tensor`, shaped `[batch_size, time_steps,
hidden_size]`.
states: A tuple of `LSTMStateTuple`s; the final state of each layer.
Bidirectional LSTM returns a concatenation of last forward and backward
state, reduced to the original dimensionality. | def lstm(inputs, sequence_length, hparams, train, name, initial_state=None):
"""Adds a stack of LSTM layers on top of input.
Args:
inputs: The input `Tensor`, shaped `[batch_size, time_steps, hidden_size]`.
sequence_length: Lengths of the actual input sequence, excluding padding; a
`Tensor` shaped `[batch_size]`.
hparams: HParams; hyperparameters.
train: bool; `True` when constructing training graph to enable dropout.
name: string; Create variable names under this scope.
initial_state: tuple of `LSTMStateTuple`s; the initial state of each layer.
Returns:
A tuple (outputs, states), where:
outputs: The output `Tensor`, shaped `[batch_size, time_steps,
hidden_size]`.
states: A tuple of `LSTMStateTuple`s; the final state of each layer.
Bidirectional LSTM returns a concatenation of last forward and backward
state, reduced to the original dimensionality.
"""
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) |
Run LSTM cell with attention on inputs of shape [batch x time x size].
Args:
inputs: The decoder input `Tensor`, shaped `[batch_size, decoder_steps,
hidden_size]`.
hparams: HParams; hyperparameters.
train: bool; `True` when constructing training graph to enable dropout.
name: string; Create variable names under this scope.
initial_state: Tuple of `LSTMStateTuple`s; the initial state of each layer.
encoder_outputs: Encoder outputs; a `Tensor` shaped `[batch_size,
encoder_steps, hidden_size]`.
encoder_output_length: Lengths of the actual encoder outputs, excluding
padding; a `Tensor` shaped `[batch_size]`.
decoder_input_length: Lengths of the actual decoder inputs, excluding
padding; a `Tensor` shaped `[batch_size]`.
Raises:
ValueError: If the hparams.attention_mechanism is anything other than
luong or bahdanau.
Returns:
The decoder output `Tensor`, shaped `[batch_size, decoder_steps,
hidden_size]`. | def lstm_attention_decoder(inputs, hparams, train, name, initial_state,
encoder_outputs, encoder_output_length,
decoder_input_length):
"""Run LSTM cell with attention on inputs of shape [batch x time x size].
Args:
inputs: The decoder input `Tensor`, shaped `[batch_size, decoder_steps,
hidden_size]`.
hparams: HParams; hyperparameters.
train: bool; `True` when constructing training graph to enable dropout.
name: string; Create variable names under this scope.
initial_state: Tuple of `LSTMStateTuple`s; the initial state of each layer.
encoder_outputs: Encoder outputs; a `Tensor` shaped `[batch_size,
encoder_steps, hidden_size]`.
encoder_output_length: Lengths of the actual encoder outputs, excluding
padding; a `Tensor` shaped `[batch_size]`.
decoder_input_length: Lengths of the actual decoder inputs, excluding
padding; a `Tensor` shaped `[batch_size]`.
Raises:
ValueError: If the hparams.attention_mechanism is anything other than
luong or bahdanau.
Returns:
The decoder output `Tensor`, shaped `[batch_size, decoder_steps,
hidden_size]`.
"""
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):
"""Custom fn for computing area keys and 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 |
The basic LSTM seq2seq model, main step used for training. | def lstm_seq2seq_internal(inputs, targets, hparams, train):
"""The basic LSTM seq2seq model, main step used for training."""
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) |
LSTM seq2seq model with attention, main step used for training. | def lstm_seq2seq_internal_attention(inputs, targets, hparams, train,
inputs_length, targets_length):
"""LSTM seq2seq model with attention, main step used for training."""
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) |
Bidirectional LSTM for encoding inputs that are [batch x time x size]. | def lstm_bid_encoder(inputs, sequence_length, hparams, train, name):
"""Bidirectional LSTM for encoding inputs that are [batch x time x size]."""
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 |
The basic LSTM seq2seq model with bidirectional encoder. | def lstm_seq2seq_internal_bid_encoder(inputs, targets, hparams, train):
"""The basic LSTM seq2seq model with bidirectional encoder."""
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) |
LSTM seq2seq model with attention, main step used for training. | def lstm_seq2seq_internal_attention_bid_encoder(inputs, targets, hparams,
train):
"""LSTM seq2seq model with attention, main step used for training."""
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) |
hparams for LSTM. | def lstm_seq2seq():
"""hparams for LSTM."""
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 |
Base attention params. | def lstm_attention_base():
"""Base attention params."""
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 |
Basic LSTM Params. | def lstm_asr_v1():
"""Basic LSTM Params."""
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 |
Hparams for LSTM with area attention. | def lstm_area_attention_base():
"""Hparams for LSTM with area attention."""
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 |
Create a run config.
Args:
hp: model hyperparameters
Returns:
a run config | def create_surrogate_run_config(hp):
"""Create a run config.
Args:
hp: model hyperparameters
Returns:
a run config
"""
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) |
Construct input pipeline. | def prepare_data(problem, hparams, params, config):
"""Construct input pipeline."""
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 |
Transform a string with a filename into a list of float32.
Args:
s: path to the file with a waveform.
Returns:
samples: list of int16s | def encode(self, s):
"""Transform a string with a filename into a list of float32.
Args:
s: path to the file with a waveform.
Returns:
samples: list of int16s
"""
# 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 sequence of float32 into a waveform.
Args:
ids: list of integers to be converted.
Returns:
Path to the temporary file where the waveform was saved.
Raises:
ValueError: if the ids are not of the appropriate size. | def decode(self, ids):
"""Transform a sequence of float32 into a waveform.
Args:
ids: list of integers to be converted.
Returns:
Path to the temporary file where the waveform was saved.
Raises:
ValueError: if the ids are not of the appropriate size.
"""
_, tmp_file_path = tempfile.mkstemp()
wavfile.write(tmp_file_path, self._sample_rate, np.asarray(ids))
return tmp_file_path |
Creates and returns a new vertex.
Returns:
A new Vertex instance with a unique index. | def new_vertex(self):
"""Creates and returns a new vertex.
Returns:
A new Vertex instance with a unique index.
"""
vertex = Vertex(len(self.vertices))
self.vertices.append(vertex)
return vertex |
Returns or Creates a Vertex mapped by key.
Args:
key: A string reference for a vertex. May refer to a new Vertex in which
case it will be created.
Returns:
A the Vertex mapped to by key. | def get_vertex(self, key):
"""Returns or Creates a Vertex mapped by key.
Args:
key: A string reference for a vertex. May refer to a new Vertex in which
case it will be created.
Returns:
A the Vertex mapped to by key.
"""
if key in self.vertex_map:
return self.vertex_map[key]
vertex = self.new_vertex()
self.vertex_map[key] = vertex
return vertex |
Returns a new edge connecting source and target vertices.
Args:
source: The source Vertex.
target: The target Vertex.
Returns:
A new Edge linking source to target. | def add_edge(self, source, target):
"""Returns a new edge connecting source and target vertices.
Args:
source: The source Vertex.
target: The target Vertex.
Returns:
A new Edge linking source to 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 simplified dictionary representing the Graph.
Returns:
A dictionary that can easily be serialized to JSON. | def to_dict(self):
"""Returns a simplified dictionary representing the Graph.
Returns:
A dictionary that can easily be serialized to JSON.
"""
return {
"node": [v.to_dict() for v in self.vertices],
"edge": [e.to_dict() for e in self.edges]
} |
Self-attention layer with source as memory antecedent. | def attend(x, source, hparams, name):
"""Self-attention layer with source as memory antecedent."""
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) |
Calculate softmax(x), select top-k and rescale to sum to 1. | def top_k_softmax(x, k):
"""Calculate softmax(x), select top-k and rescale to sum to 1."""
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) |
Compress. | def compress(x, c, is_2d, hparams, name):
"""Compress."""
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 |
Original Transformer decoder. | def decode_transformer(encoder_output,
encoder_decoder_attention_bias,
targets,
hparams,
name,
task=None,
causal=True):
"""Original Transformer decoder."""
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 |
Latent prediction and loss. | def ae_latent_softmax(latents_pred, latents_discrete, hparams):
"""Latent prediction and loss."""
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 |
Sample from the latent space in the autoencoder. | def ae_latent_sample(latents_dense, inputs, ed, embed, iters, hparams):
"""Sample from the latent space in the autoencoder."""
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 |
AE Transformer, main step used for training. | def ae_transformer_internal(inputs,
targets,
target_space,
hparams,
cache=None,
predict_mask=1.0):
"""AE Transformer, main step used for training."""
# 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 |
Set of hyperparameters. | def transformer_ae_small():
"""Set of hyperparameters."""
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 |
Hyperparameters for CIFAR-10 experiments. | def imagetransformer_ae_cifar():
"""Hyperparameters for CIFAR-10 experiments."""
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 |
For 64x64 ImageNet. ~56M trainable variables. | def imagetransformer_ae_imagenet():
"""For 64x64 ImageNet. ~56M trainable variables."""
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 |
Set of hyperparameters. | def transformer_ae_base():
"""Set of hyperparameters."""
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. | def transformer_ae_a3():
"""Set of hyperparameters."""
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. | def transformer_ae_base_noatt():
"""Set of hyperparameters."""
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. | def transformer_ae_small_noatt():
"""Set of hyperparameters."""
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 |
Basic transformer_sketch hparams. | def transformer_sketch():
"""Basic transformer_sketch hparams."""
hparams = transformer.transformer_small()
hparams.num_compress_steps = 4
hparams.batch_size = 32
hparams.clip_grad_norm = 2.
hparams.sampling_method = "random"
return hparams |
Get the layers module good for TF 1 and TF 2 work for now. | def layers():
"""Get the layers module good for TF 1 and TF 2 work for now."""
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 |
Like tf.nn.dropout but takes broadcast_dims instead of noise_shape.
Instead of specifying noise_shape, this function takes broadcast_dims -
a list of dimension numbers in which noise_shape should be 1. The random
keep/drop tensor has dimensionality 1 along these dimensions.
Args:
x: a floating point tensor.
keep_prob: A scalar Tensor with the same type as x.
The probability that each element is kept.
broadcast_dims: an optional list of integers
the dimensions along which to broadcast the keep/drop flags.
**kwargs: keyword arguments to tf.nn.dropout other than "noise_shape".
Returns:
Tensor of the same shape as x. | def dropout_with_broadcast_dims(x, keep_prob, broadcast_dims=None, **kwargs):
"""Like tf.nn.dropout but takes broadcast_dims instead of noise_shape.
Instead of specifying noise_shape, this function takes broadcast_dims -
a list of dimension numbers in which noise_shape should be 1. The random
keep/drop tensor has dimensionality 1 along these dimensions.
Args:
x: a floating point tensor.
keep_prob: A scalar Tensor with the same type as x.
The probability that each element is kept.
broadcast_dims: an optional list of integers
the dimensions along which to broadcast the keep/drop flags.
**kwargs: keyword arguments to tf.nn.dropout other than "noise_shape".
Returns:
Tensor of the same shape as x.
"""
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) |
Saturating sigmoid: 1.2 * sigmoid(x) - 0.1 cut to [0, 1]. | def saturating_sigmoid(x):
"""Saturating sigmoid: 1.2 * sigmoid(x) - 0.1 cut to [0, 1]."""
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)) |
Inverse-decay exponentially from 0.01 to 1.0 reached at max_step. | def inverse_exp_decay(max_step, min_value=0.01, step=None):
"""Inverse-decay exponentially from 0.01 to 1.0 reached at max_step."""
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 linearly from 0.01 to 1.0 reached at max_step. | def inverse_lin_decay(max_step, min_value=0.01, step=None):
"""Inverse-decay linearly from 0.01 to 1.0 reached at max_step."""
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 |
The shake-shake sum of 2 tensors, python version. | def shakeshake2_py(x, y, equal=False, individual=False):
"""The shake-shake sum of 2 tensors, python version."""
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 |
Overriding gradient for shake-shake of 2 tensors. | def shakeshake2_grad(x1, x2, dy):
"""Overriding gradient for shake-shake of 2 tensors."""
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. | def shakeshake2_indiv_grad(x1, x2, dy):
"""Overriding gradient for shake-shake of 2 tensors."""
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. | def shakeshake2_equal_grad(x1, x2, dy):
"""Overriding gradient for shake-shake of 2 tensors."""
y = shakeshake2_py(x1, x2, equal=True)
dx = tf.gradients(ys=[y], xs=[x1, x2], grad_ys=[dy])
return dx |
Multi-argument shake-shake, currently approximated by sums of 2. | def shakeshake(xs, equal_grad=False):
"""Multi-argument shake-shake, currently approximated by sums of 2."""
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) |
Conversion of pixel values to real numbers. | def convert_rgb_to_real(x):
"""Conversion of pixel values to real numbers."""
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. | def convert_rgb_to_symmetric_real(x):
"""Conversion of pixel values to real numbers."""
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 |
Make x n-d with squeeze and expand_dims. | def expand_squeeze_to_nd(x, n, squeeze_dim=2, expand_dim=-1):
"""Make x n-d with squeeze and expand_dims."""
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 |
Image standardization on batches and videos. | def standardize_images(x):
"""Image standardization on batches and videos."""
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) |
Flatten a 4d-tensor into a 3d-tensor by joining width and height. | def flatten4d3d(x):
"""Flatten a 4d-tensor into a 3d-tensor by joining width and height."""
xshape = shape_list(x)
result = tf.reshape(x, [xshape[0], xshape[1] * xshape[2], xshape[3]])
return result |
Version of tf.gather that works faster on tpu. | def gather(params, indices, dtype=tf.float32):
"""Version of tf.gather that works faster on tpu."""
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 |
TPU hack for tf.cumsum.
This is equivalent to tf.cumsum and is faster on TPU as of 04/2018 unless
the axis dimension is very large.
Args:
x: a Tensor
axis: an integer
exclusive: a boolean
Returns:
Tensor of the same shape as x. | def cumsum(x, axis=0, exclusive=False):
"""TPU hack for tf.cumsum.
This is equivalent to tf.cumsum and is faster on TPU as of 04/2018 unless
the axis dimension is very large.
Args:
x: a Tensor
axis: an integer
exclusive: a boolean
Returns:
Tensor of the same shape as x.
"""
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 |
Like tf.nn.dropout, but does not scale up. Works on integers also.
Args:
x: a Tensor
keep_prob: a floating point number
Returns:
Tensor of the same shape as x. | def dropout_no_scaling(x, keep_prob):
"""Like tf.nn.dropout, but does not scale up. Works on integers also.
Args:
x: a Tensor
keep_prob: a floating point number
Returns:
Tensor of the same shape as x.
"""
if keep_prob == 1.0:
return x
mask = tf.less(tf.random_uniform(tf.shape(x)), keep_prob)
return x * cast_like(mask, x) |
Embed x of type int64 into dense vectors, reducing to max 4 dimensions. | def embedding(x,
vocab_size,
dense_size,
name=None,
reuse=None,
multiplier=1.0,
symbol_dropout_rate=0.0,
embedding_var=None,
dtype=tf.float32):
"""Embed x of type int64 into dense vectors, reducing to max 4 dimensions."""
with tf.variable_scope(
name, default_name="embedding", values=[x], reuse=reuse, dtype=dtype):
if embedding_var is None:
embedding_var = tf.get_variable("kernel", [vocab_size, dense_size])
# On the backwards pass, we want to convert the gradient from
# an indexed-slices to a regular tensor before sending it back to the
# parameter server. This avoids excess computation on the parameter server.
if not tf.executing_eagerly():
embedding_var = convert_gradient_to_tensor(embedding_var)
x = dropout_no_scaling(x, 1.0 - symbol_dropout_rate)
emb_x = gather(embedding_var, x, dtype)
if multiplier != 1.0:
emb_x *= multiplier
static_shape = emb_x.shape.as_list()
if len(static_shape) < 5:
return emb_x
assert len(static_shape) == 5
# If we had an extra channel dimension, assume it's 1, i.e. shape[3] == 1.
return tf.squeeze(emb_x, 3) |
Shift the second dimension of x right by one. | def shift_right(x, pad_value=None):
"""Shift the second dimension of x right by one."""
if pad_value is None:
shifted_targets = tf.pad(x, [[0, 0], [1, 0], [0, 0], [0, 0]])[:, :-1, :, :]
else:
shifted_targets = tf.concat([pad_value, x], axis=1)[:, :-1, :, :]
return shifted_targets |
Shift the second dimension of x right by one. | def shift_right_3d(x, pad_value=None):
"""Shift the second dimension of x right by one."""
if pad_value is None:
shifted_targets = tf.pad(x, [[0, 0], [1, 0], [0, 0]])[:, :-1, :]
else:
shifted_targets = tf.concat([pad_value, x], axis=1)[:, :-1, :]
return shifted_targets |
Shift the second dimension of x right by one. | def shift_right_2d(x, pad_value=None):
"""Shift the second dimension of x right by one."""
if pad_value is None:
shifted_targets = tf.pad(x, [[0, 0], [1, 0]])[:, :-1]
else:
shifted_targets = tf.concat([pad_value, x], axis=1)[:, :-1]
return shifted_targets |
Use a strided convolution to downsample x by 2, `nbr_steps` times.
We use stride and filter size 2 to avoid the checkerboard problem of deconvs.
As detailed in http://distill.pub/2016/deconv-checkerboard/.
Args:
x: a `Tensor` with shape `[batch, spatial, depth]` or
`[batch, spatial_1, spatial_2, depth]`
nbr_steps: number of halving downsample rounds to apply
output_filters: an int specifying the filter count for the convolutions
name: a string
reuse: a boolean
Returns:
a `Tensor` with shape `[batch, spatial / (2**nbr_steps), output_filters]` or
`[batch, spatial_1 / (2**nbr_steps), spatial_2 / (2**nbr_steps),
output_filters]` | def conv_stride2_multistep(x, nbr_steps, output_filters, name=None, reuse=None):
"""Use a strided convolution to downsample x by 2, `nbr_steps` times.
We use stride and filter size 2 to avoid the checkerboard problem of deconvs.
As detailed in http://distill.pub/2016/deconv-checkerboard/.
Args:
x: a `Tensor` with shape `[batch, spatial, depth]` or
`[batch, spatial_1, spatial_2, depth]`
nbr_steps: number of halving downsample rounds to apply
output_filters: an int specifying the filter count for the convolutions
name: a string
reuse: a boolean
Returns:
a `Tensor` with shape `[batch, spatial / (2**nbr_steps), output_filters]` or
`[batch, spatial_1 / (2**nbr_steps), spatial_2 / (2**nbr_steps),
output_filters]`
"""
with tf.variable_scope(
name, default_name="conv_stride2_multistep", values=[x], reuse=reuse):
if nbr_steps == 0:
out = conv(x, output_filters, (1, 1))
return out, [out]
hidden_layers = [x]
for i in range(nbr_steps):
hidden_layers.append(
conv(
hidden_layers[-1],
output_filters, (2, 2),
strides=2,
activation=tf.nn.relu,
name="conv" + str(i)))
return hidden_layers[-1], hidden_layers |
Use a deconvolution to upsample x by 2**`nbr_steps`.
Args:
x: a `Tensor` with shape `[batch, spatial, depth]` or
`[batch, spatial_1, spatial_2, depth]`
nbr_steps: an int specifying the number of doubling upsample rounds to
apply.
output_filters: an int specifying the filter count for the deconvolutions
name: a string
reuse: a boolean
Returns:
a `Tensor` with shape `[batch, spatial * (2**nbr_steps), output_filters]` or
`[batch, spatial_1 * (2**nbr_steps), spatial_2 * (2**nbr_steps),
output_filters]` | def deconv_stride2_multistep(x,
nbr_steps,
output_filters,
name=None,
reuse=None):
"""Use a deconvolution to upsample x by 2**`nbr_steps`.
Args:
x: a `Tensor` with shape `[batch, spatial, depth]` or
`[batch, spatial_1, spatial_2, depth]`
nbr_steps: an int specifying the number of doubling upsample rounds to
apply.
output_filters: an int specifying the filter count for the deconvolutions
name: a string
reuse: a boolean
Returns:
a `Tensor` with shape `[batch, spatial * (2**nbr_steps), output_filters]` or
`[batch, spatial_1 * (2**nbr_steps), spatial_2 * (2**nbr_steps),
output_filters]`
"""
with tf.variable_scope(
name, default_name="deconv_stride2_multistep", values=[x], reuse=reuse):
def deconv1d(cur, i):
cur_shape = shape_list(cur)
thicker = conv(
cur,
output_filters * 2, (1, 1),
padding="SAME",
activation=tf.nn.relu,
name="deconv1d" + str(i))
return tf.reshape(thicker,
[cur_shape[0], cur_shape[1] * 2, 1, output_filters])
def deconv2d(cur, i):
thicker = conv(
cur,
output_filters * 4, (1, 1),
padding="SAME",
activation=tf.nn.relu,
name="deconv2d" + str(i))
return tf.depth_to_space(thicker, 2)
cur = x
for i in range(nbr_steps):
if cur.get_shape()[2] == 1:
cur = deconv1d(cur, i)
else:
cur_dim = shape_list(cur)[2]
if isinstance(cur_dim, int):
if cur_dim == 1:
cur = deconv1d(cur, i)
else:
cur = deconv2d(cur, i)
else:
cur = tf.cond(
tf.equal(cur_dim, 1),
lambda idx=i: deconv1d(cur, idx),
lambda idx=i: deconv2d(cur, idx))
return cur |
Conditional conv_fn making kernel 1d or 2d depending on inputs shape. | def conv_internal(conv_fn, inputs, filters, kernel_size, **kwargs):
"""Conditional conv_fn making kernel 1d or 2d depending on inputs shape."""
static_shape = inputs.get_shape()
if not static_shape or len(static_shape) != 4:
raise ValueError("Inputs to conv must have statically known rank 4. "
"Shape: " + str(static_shape))
# Add support for left padding.
if kwargs.get("padding") == "LEFT":
dilation_rate = (1, 1)
if "dilation_rate" in kwargs:
dilation_rate = kwargs["dilation_rate"]
assert kernel_size[0] % 2 == 1 and kernel_size[1] % 2 == 1
height_padding = 2 * (kernel_size[0] // 2) * dilation_rate[0]
cond_padding = tf.cond(
tf.equal(shape_list(inputs)[2], 1), lambda: tf.constant(0),
lambda: tf.constant(2 * (kernel_size[1] // 2) * dilation_rate[1]))
width_padding = 0 if static_shape[2] == 1 else cond_padding
padding = [[0, 0], [height_padding, 0], [width_padding, 0], [0, 0]]
inputs = tf.pad(inputs, padding)
# Set middle two dimensions to None to prevent convolution from complaining
inputs.set_shape([static_shape[0], None, None, static_shape[3]])
kwargs["padding"] = "VALID"
def conv2d_kernel(kernel_size_arg, name_suffix):
"""Call conv2d but add suffix to name."""
name = "{}_{}".format(kwargs.get("name", "conv"), name_suffix)
original_name = kwargs.pop("name", None)
original_force2d = kwargs.pop("force2d", None)
result = conv_fn(inputs, filters, kernel_size_arg, name=name, **kwargs)
if original_name is not None:
kwargs["name"] = original_name # Restore for other calls.
if original_force2d is not None:
kwargs["force2d"] = original_force2d
return result
return conv2d_kernel(kernel_size, "single") |
Sub-separable convolution. If separability == 0 it's a separable_conv. | def subseparable_conv(inputs, filters, kernel_size, **kwargs):
"""Sub-separable convolution. If separability == 0 it's a separable_conv."""
def conv_fn(inputs, filters, kernel_size, **kwargs):
"""Sub-separable convolution, splits into separability-many blocks."""
separability = None
if "separability" in kwargs:
separability = kwargs.pop("separability")
if separability:
parts = []
abs_sep = separability if separability > 0 else -1 * separability
for split_idx, split in enumerate(tf.split(inputs, abs_sep, axis=3)):
with tf.variable_scope("part_%d" % split_idx):
if separability > 0:
parts.append(
layers().Conv2D(filters // separability, kernel_size,
**kwargs)(split))
else:
parts.append(
layers().SeparableConv2D(filters // abs_sep,
kernel_size, **kwargs)(split))
if separability > 1:
result = layers().Conv2D(filters, (1, 1))(tf.concat(parts, axis=3))
elif abs_sep == 1: # If we have just one block, return it.
assert len(parts) == 1
result = parts[0]
else:
result = tf.concat(parts, axis=3)
else:
result = layers().SeparableConv2D(filters, kernel_size,
**kwargs)(inputs)
if separability is not None:
kwargs["separability"] = separability
return result
return conv_internal(conv_fn, inputs, filters, kernel_size, **kwargs) |
Version of conv1d that works on TPU (as of 11/2017).
Args:
inputs: a Tensor with shape [batch, length, input_depth].
filters: an integer.
kernel_size: an integer.
padding: a string - "SAME" or "LEFT".
name: a string.
Returns:
a Tensor with shape [batch, length, filters]. | def tpu_conv1d(inputs, filters, kernel_size, padding="SAME", name="tpu_conv1d"):
"""Version of conv1d that works on TPU (as of 11/2017).
Args:
inputs: a Tensor with shape [batch, length, input_depth].
filters: an integer.
kernel_size: an integer.
padding: a string - "SAME" or "LEFT".
name: a string.
Returns:
a Tensor with shape [batch, length, filters].
"""
if kernel_size == 1:
return dense(inputs, filters, name=name, use_bias=True)
if padding == "SAME":
assert kernel_size % 2 == 1
first_offset = -((kernel_size - 1) // 2)
else:
assert padding == "LEFT"
first_offset = -(kernel_size - 1)
last_offset = first_offset + kernel_size - 1
results = []
padded = tf.pad(inputs, [[0, 0], [-first_offset, last_offset], [0, 0]])
for i in range(kernel_size):
shifted = tf.slice(padded, [0, i, 0], tf.shape(inputs)) if i else inputs
shifted.set_shape(inputs.get_shape())
results.append(
dense(shifted, filters, use_bias=(i == 0), name=name + "_%d" % i))
ret = tf.add_n(results)
ret *= kernel_size**-0.5
return ret |
Create Variables for layer norm. | def layer_norm_vars(filters):
"""Create Variables for layer norm."""
scale = tf.get_variable(
"layer_norm_scale", [filters], initializer=tf.ones_initializer())
bias = tf.get_variable(
"layer_norm_bias", [filters], initializer=tf.zeros_initializer())
return scale, bias |
Layer norm raw computation. | def layer_norm_compute(x, epsilon, scale, bias, layer_collection=None):
"""Layer norm raw computation."""
# Save these before they get converted to tensors by the casting below
params = (scale, bias)
epsilon, scale, bias = [cast_like(t, x) for t in [epsilon, scale, bias]]
mean = tf.reduce_mean(x, axis=[-1], keepdims=True)
variance = tf.reduce_mean(
tf.squared_difference(x, mean), axis=[-1], keepdims=True)
norm_x = (x - mean) * tf.rsqrt(variance + epsilon)
output = norm_x * scale + bias
return output |
Layer normalize the tensor x, averaging over the last dimension. | def layer_norm(x,
filters=None,
epsilon=1e-6,
name=None,
reuse=None,
layer_collection=None):
"""Layer normalize the tensor x, averaging over the last dimension."""
if filters is None:
filters = shape_list(x)[-1]
with tf.variable_scope(
name, default_name="layer_norm", values=[x], reuse=reuse):
scale, bias = layer_norm_vars(filters)
return layer_norm_compute(x, epsilon, scale, bias,
layer_collection=layer_collection) |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.