partition
stringclasses
3 values
func_name
stringlengths
1
134
docstring
stringlengths
1
46.9k
path
stringlengths
4
223
original_string
stringlengths
75
104k
code
stringlengths
75
104k
docstring_tokens
listlengths
1
1.97k
repo
stringlengths
7
55
language
stringclasses
1 value
url
stringlengths
87
315
code_tokens
listlengths
19
28.4k
sha
stringlengths
40
40
train
get_padding_value
Returns the padding value given a dtype.
tensor2tensor/trax/rlax/ppo.py
def get_padding_value(dtype): """Returns the padding value given a dtype.""" padding_value = None if dtype == np.uint8: padding_value = np.uint8(0) elif dtype == np.uint16: padding_value = np.uint16(0) elif dtype == np.float32: padding_value = 0.0 else: padding_value = 0 assert padding_value is not None return padding_value
def get_padding_value(dtype): """Returns the padding value given a dtype.""" padding_value = None if dtype == np.uint8: padding_value = np.uint8(0) elif dtype == np.uint16: padding_value = np.uint16(0) elif dtype == np.float32: padding_value = 0.0 else: padding_value = 0 assert padding_value is not None return padding_value
[ "Returns", "the", "padding", "value", "given", "a", "dtype", "." ]
tensorflow/tensor2tensor
python
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/trax/rlax/ppo.py#L283-L295
[ "def", "get_padding_value", "(", "dtype", ")", ":", "padding_value", "=", "None", "if", "dtype", "==", "np", ".", "uint8", ":", "padding_value", "=", "np", ".", "uint8", "(", "0", ")", "elif", "dtype", "==", "np", ".", "uint16", ":", "padding_value", "...
272500b6efe353aeb638d2745ed56e519462ca31
train
pad_trajectories
Pad trajectories to a bucket length that is a multiple of boundary. Args: trajectories: list[(observation, actions, rewards)], where each observation is shaped (t+1,) + OBS and actions & rewards are shaped (t,), with the length of the list being B (batch size). boundary: int, bucket length, the actions and rewards are padded to integer multiples of boundary. Returns: tuple: (padding lengths, reward_mask, padded_observations, padded_actions, padded_rewards) where padded_observations is shaped (B, T+1) + OBS and padded_actions, padded_rewards & reward_mask are shaped (B, T). Where T is max(t) rounded up to an integer multiple of boundary. padded_length is how much padding we've added and reward_mask is 1s for actual rewards and 0s for the padding.
tensor2tensor/trax/rlax/ppo.py
def pad_trajectories(trajectories, boundary=20): """Pad trajectories to a bucket length that is a multiple of boundary. Args: trajectories: list[(observation, actions, rewards)], where each observation is shaped (t+1,) + OBS and actions & rewards are shaped (t,), with the length of the list being B (batch size). boundary: int, bucket length, the actions and rewards are padded to integer multiples of boundary. Returns: tuple: (padding lengths, reward_mask, padded_observations, padded_actions, padded_rewards) where padded_observations is shaped (B, T+1) + OBS and padded_actions, padded_rewards & reward_mask are shaped (B, T). Where T is max(t) rounded up to an integer multiple of boundary. padded_length is how much padding we've added and reward_mask is 1s for actual rewards and 0s for the padding. """ # Let's compute max(t) over all trajectories. t_max = max(r.shape[0] for (_, _, r) in trajectories) # t_max is rounded to the next multiple of `boundary` boundary = int(boundary) bucket_length = boundary * int(np.ceil(float(t_max) / boundary)) # So all obs will be padded to t_max + 1 and actions and rewards to t_max. padded_observations = [] padded_actions = [] padded_rewards = [] padded_lengths = [] reward_masks = [] for (o, a, r) in trajectories: # Determine the amount to pad, this holds true for obs, actions and rewards. num_to_pad = bucket_length + 1 - o.shape[0] padded_lengths.append(num_to_pad) if num_to_pad == 0: padded_observations.append(o) padded_actions.append(a) padded_rewards.append(r) reward_masks.append(onp.ones_like(r, dtype=np.int32)) continue # First pad observations. padding_config = [(0, num_to_pad, 0)] for _ in range(o.ndim - 1): padding_config.append((0, 0, 0)) padding_config = tuple(padding_config) padding_value = get_padding_value(o.dtype) action_padding_value = get_padding_value(a.dtype) reward_padding_value = get_padding_value(r.dtype) padded_obs = lax.pad(o, padding_value, padding_config) padded_observations.append(padded_obs) # Now pad actions and rewards. assert a.ndim == 1 and r.ndim == 1 padding_config = ((0, num_to_pad, 0),) padded_action = lax.pad(a, action_padding_value, padding_config) padded_actions.append(padded_action) padded_reward = lax.pad(r, reward_padding_value, padding_config) padded_rewards.append(padded_reward) # Also create the mask to use later. reward_mask = onp.ones_like(r, dtype=np.int32) reward_masks.append(lax.pad(reward_mask, 0, padding_config)) return padded_lengths, np.stack(reward_masks), np.stack( padded_observations), np.stack(padded_actions), np.stack(padded_rewards)
def pad_trajectories(trajectories, boundary=20): """Pad trajectories to a bucket length that is a multiple of boundary. Args: trajectories: list[(observation, actions, rewards)], where each observation is shaped (t+1,) + OBS and actions & rewards are shaped (t,), with the length of the list being B (batch size). boundary: int, bucket length, the actions and rewards are padded to integer multiples of boundary. Returns: tuple: (padding lengths, reward_mask, padded_observations, padded_actions, padded_rewards) where padded_observations is shaped (B, T+1) + OBS and padded_actions, padded_rewards & reward_mask are shaped (B, T). Where T is max(t) rounded up to an integer multiple of boundary. padded_length is how much padding we've added and reward_mask is 1s for actual rewards and 0s for the padding. """ # Let's compute max(t) over all trajectories. t_max = max(r.shape[0] for (_, _, r) in trajectories) # t_max is rounded to the next multiple of `boundary` boundary = int(boundary) bucket_length = boundary * int(np.ceil(float(t_max) / boundary)) # So all obs will be padded to t_max + 1 and actions and rewards to t_max. padded_observations = [] padded_actions = [] padded_rewards = [] padded_lengths = [] reward_masks = [] for (o, a, r) in trajectories: # Determine the amount to pad, this holds true for obs, actions and rewards. num_to_pad = bucket_length + 1 - o.shape[0] padded_lengths.append(num_to_pad) if num_to_pad == 0: padded_observations.append(o) padded_actions.append(a) padded_rewards.append(r) reward_masks.append(onp.ones_like(r, dtype=np.int32)) continue # First pad observations. padding_config = [(0, num_to_pad, 0)] for _ in range(o.ndim - 1): padding_config.append((0, 0, 0)) padding_config = tuple(padding_config) padding_value = get_padding_value(o.dtype) action_padding_value = get_padding_value(a.dtype) reward_padding_value = get_padding_value(r.dtype) padded_obs = lax.pad(o, padding_value, padding_config) padded_observations.append(padded_obs) # Now pad actions and rewards. assert a.ndim == 1 and r.ndim == 1 padding_config = ((0, num_to_pad, 0),) padded_action = lax.pad(a, action_padding_value, padding_config) padded_actions.append(padded_action) padded_reward = lax.pad(r, reward_padding_value, padding_config) padded_rewards.append(padded_reward) # Also create the mask to use later. reward_mask = onp.ones_like(r, dtype=np.int32) reward_masks.append(lax.pad(reward_mask, 0, padding_config)) return padded_lengths, np.stack(reward_masks), np.stack( padded_observations), np.stack(padded_actions), np.stack(padded_rewards)
[ "Pad", "trajectories", "to", "a", "bucket", "length", "that", "is", "a", "multiple", "of", "boundary", "." ]
tensorflow/tensor2tensor
python
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/trax/rlax/ppo.py#L299-L369
[ "def", "pad_trajectories", "(", "trajectories", ",", "boundary", "=", "20", ")", ":", "# Let's compute max(t) over all trajectories.", "t_max", "=", "max", "(", "r", ".", "shape", "[", "0", "]", "for", "(", "_", ",", "_", ",", "r", ")", "in", "trajectories...
272500b6efe353aeb638d2745ed56e519462ca31
train
rewards_to_go
r"""Computes rewards to go. Reward to go is defined as follows, the discounted reward that we have to yet collect, going forward from this point, i.e.: r2g_t = \sum_{l=0}^{\infty} (\gamma^{l} * reward_{t+l}) Args: rewards: np.ndarray of shape (B, T) of rewards. mask: np.ndarray of shape (B, T) of mask for the rewards. gamma: float, discount factor. Returns: rewards to go, np.ndarray of shape (B, T).
tensor2tensor/trax/rlax/ppo.py
def rewards_to_go(rewards, mask, gamma=0.99): r"""Computes rewards to go. Reward to go is defined as follows, the discounted reward that we have to yet collect, going forward from this point, i.e.: r2g_t = \sum_{l=0}^{\infty} (\gamma^{l} * reward_{t+l}) Args: rewards: np.ndarray of shape (B, T) of rewards. mask: np.ndarray of shape (B, T) of mask for the rewards. gamma: float, discount factor. Returns: rewards to go, np.ndarray of shape (B, T). """ B, T = rewards.shape # pylint: disable=invalid-name,unused-variable masked_rewards = rewards * mask # (B, T) # We use the following recurrence relation, derived from the equation above: # # r2g[t+1] = (r2g[t] - r[t]) / gamma # # This means we'll need to calculate r2g[0] first and then r2g[1] and so on .. # # **However** this leads to overflows for long sequences: r2g[t] - r[t] > 0 # and gamma < 1.0, so the division keeps increasing. # # So we just run the recurrence in reverse, i.e. # # r2g[t] = r[t] + (gamma*r2g[t+1]) # # This is much better, but might have lost updates since the (small) rewards # at earlier time-steps may get added to a (very?) large sum. # Compute r2g_{T-1} at the start and then compute backwards in time. r2gs = [masked_rewards[:, -1]] # Go from T-2 down to 0. for t in reversed(range(T - 1)): r2gs.append(masked_rewards[:, t] + (gamma * r2gs[-1])) # The list should have length T. assert T == len(r2gs) # First we stack them in the correct way to make it (B, T), but these are # still from newest (T-1) to oldest (0), so then we flip it on time axis. return np.flip(np.stack(r2gs, axis=1), axis=1)
def rewards_to_go(rewards, mask, gamma=0.99): r"""Computes rewards to go. Reward to go is defined as follows, the discounted reward that we have to yet collect, going forward from this point, i.e.: r2g_t = \sum_{l=0}^{\infty} (\gamma^{l} * reward_{t+l}) Args: rewards: np.ndarray of shape (B, T) of rewards. mask: np.ndarray of shape (B, T) of mask for the rewards. gamma: float, discount factor. Returns: rewards to go, np.ndarray of shape (B, T). """ B, T = rewards.shape # pylint: disable=invalid-name,unused-variable masked_rewards = rewards * mask # (B, T) # We use the following recurrence relation, derived from the equation above: # # r2g[t+1] = (r2g[t] - r[t]) / gamma # # This means we'll need to calculate r2g[0] first and then r2g[1] and so on .. # # **However** this leads to overflows for long sequences: r2g[t] - r[t] > 0 # and gamma < 1.0, so the division keeps increasing. # # So we just run the recurrence in reverse, i.e. # # r2g[t] = r[t] + (gamma*r2g[t+1]) # # This is much better, but might have lost updates since the (small) rewards # at earlier time-steps may get added to a (very?) large sum. # Compute r2g_{T-1} at the start and then compute backwards in time. r2gs = [masked_rewards[:, -1]] # Go from T-2 down to 0. for t in reversed(range(T - 1)): r2gs.append(masked_rewards[:, t] + (gamma * r2gs[-1])) # The list should have length T. assert T == len(r2gs) # First we stack them in the correct way to make it (B, T), but these are # still from newest (T-1) to oldest (0), so then we flip it on time axis. return np.flip(np.stack(r2gs, axis=1), axis=1)
[ "r", "Computes", "rewards", "to", "go", "." ]
tensorflow/tensor2tensor
python
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/trax/rlax/ppo.py#L373-L421
[ "def", "rewards_to_go", "(", "rewards", ",", "mask", ",", "gamma", "=", "0.99", ")", ":", "B", ",", "T", "=", "rewards", ".", "shape", "# pylint: disable=invalid-name,unused-variable", "masked_rewards", "=", "rewards", "*", "mask", "# (B, T)", "# We use the follow...
272500b6efe353aeb638d2745ed56e519462ca31
train
value_loss
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.
tensor2tensor/trax/rlax/ppo.py
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)
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", "." ]
tensorflow/tensor2tensor
python
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/trax/rlax/ppo.py#L425-L454
[ "def", "value_loss", "(", "value_net_apply", ",", "value_net_params", ",", "observations", ",", "rewards", ",", "reward_mask", ",", "gamma", "=", "0.99", ")", ":", "B", ",", "T", "=", "rewards", ".", "shape", "# pylint: disable=invalid-name", "assert", "(", "B...
272500b6efe353aeb638d2745ed56e519462ca31
train
value_loss_given_predictions
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.
tensor2tensor/trax/rlax/ppo.py
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)
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)
[ "Computes", "the", "value", "loss", "given", "the", "prediction", "of", "the", "value", "function", "." ]
tensorflow/tensor2tensor
python
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/trax/rlax/ppo.py#L458-L484
[ "def", "value_loss_given_predictions", "(", "value_prediction", ",", "rewards", ",", "reward_mask", ",", "gamma", "=", "0.99", ")", ":", "B", ",", "T", "=", "rewards", ".", "shape", "# pylint: disable=invalid-name", "assert", "(", "B", ",", "T", ")", "==", "...
272500b6efe353aeb638d2745ed56e519462ca31
train
deltas
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.
tensor2tensor/trax/rlax/ppo.py
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
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", "TD", "-", "residuals", "from", "V", "(", "s", ")", "and", "rewards", "." ]
tensorflow/tensor2tensor
python
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/trax/rlax/ppo.py#L488-L513
[ "def", "deltas", "(", "predicted_values", ",", "rewards", ",", "mask", ",", "gamma", "=", "0.99", ")", ":", "# `d`s are basically one-step TD residuals.", "d", "=", "[", "]", "_", ",", "T", "=", "rewards", ".", "shape", "# pylint: disable=invalid-name", "for", ...
272500b6efe353aeb638d2745ed56e519462ca31
train
gae_advantages
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.
tensor2tensor/trax/rlax/ppo.py
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)
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)
[ "r", "Computes", "the", "GAE", "advantages", "given", "the", "one", "step", "TD", "-", "residuals", "." ]
tensorflow/tensor2tensor
python
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/trax/rlax/ppo.py#L516-L537
[ "def", "gae_advantages", "(", "td_deltas", ",", "mask", ",", "lambda_", "=", "0.95", ",", "gamma", "=", "0.99", ")", ":", "return", "rewards_to_go", "(", "td_deltas", ",", "mask", ",", "lambda_", "*", "gamma", ")" ]
272500b6efe353aeb638d2745ed56e519462ca31
train
chosen_probabs
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.
tensor2tensor/trax/rlax/ppo.py
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]
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]
[ "Picks", "out", "the", "probabilities", "of", "the", "actions", "along", "batch", "and", "time", "-", "steps", "." ]
tensorflow/tensor2tensor
python
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/trax/rlax/ppo.py#L540-L555
[ "def", "chosen_probabs", "(", "probab_observations", ",", "actions", ")", ":", "B", ",", "T", "=", "actions", ".", "shape", "# pylint: disable=invalid-name", "assert", "(", "B", ",", "T", "+", "1", ")", "==", "probab_observations", ".", "shape", "[", ":", ...
272500b6efe353aeb638d2745ed56e519462ca31
train
compute_probab_ratios
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}}
tensor2tensor/trax/rlax/ppo.py
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
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
[ "Computes", "the", "probability", "ratios", "for", "each", "time", "-", "step", "in", "a", "trajectory", "." ]
tensorflow/tensor2tensor
python
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/trax/rlax/ppo.py#L558-L588
[ "def", "compute_probab_ratios", "(", "p_new", ",", "p_old", ",", "actions", ",", "reward_mask", ")", ":", "B", ",", "T", "=", "actions", ".", "shape", "# pylint: disable=invalid-name", "assert", "(", "B", ",", "T", "+", "1", ")", "==", "p_old", ".", "sha...
272500b6efe353aeb638d2745ed56e519462ca31
train
ppo_loss
PPO objective, with an eventual minus sign, given observations.
tensor2tensor/trax/rlax/ppo.py
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)
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", "observations", "." ]
tensorflow/tensor2tensor
python
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/trax/rlax/ppo.py#L602-L645
[ "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", ",...
272500b6efe353aeb638d2745ed56e519462ca31
train
ppo_loss_given_predictions
PPO objective, with an eventual minus sign, given predictions.
tensor2tensor/trax/rlax/ppo.py
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
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
[ "PPO", "objective", "with", "an", "eventual", "minus", "sign", "given", "predictions", "." ]
tensorflow/tensor2tensor
python
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/trax/rlax/ppo.py#L649-L695
[ "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", "=", ...
272500b6efe353aeb638d2745ed56e519462ca31
train
combined_loss_given_predictions
Computes the combined (clipped loss + value loss) given predictions.
tensor2tensor/trax/rlax/ppo.py
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)
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", "predictions", "." ]
tensorflow/tensor2tensor
python
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/trax/rlax/ppo.py#L699-L726
[ "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", "...
272500b6efe353aeb638d2745ed56e519462ca31
train
combined_loss
Computes the combined (clipped loss + value loss) given observations.
tensor2tensor/trax/rlax/ppo.py
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)
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)
[ "Computes", "the", "combined", "(", "clipped", "loss", "+", "value", "loss", ")", "given", "observations", "." ]
tensorflow/tensor2tensor
python
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/trax/rlax/ppo.py#L731-L761
[ "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", "...
272500b6efe353aeb638d2745ed56e519462ca31
train
ppo_opt_step
PPO optimizer step.
tensor2tensor/trax/rlax/ppo.py
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)
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)
[ "PPO", "optimizer", "step", "." ]
tensorflow/tensor2tensor
python
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/trax/rlax/ppo.py#L765-L795
[ "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", ...
272500b6efe353aeb638d2745ed56e519462ca31
train
value_opt_step
Value optimizer step.
tensor2tensor/trax/rlax/ppo.py
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)
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)
[ "Value", "optimizer", "step", "." ]
tensorflow/tensor2tensor
python
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/trax/rlax/ppo.py#L799-L816
[ "def", "value_opt_step", "(", "i", ",", "opt_state", ",", "opt_update", ",", "value_net_apply", ",", "padded_observations", ",", "padded_rewards", ",", "reward_mask", ",", "gamma", "=", "0.99", ")", ":", "value_params", "=", "trax_opt", ".", "get_params", "(", ...
272500b6efe353aeb638d2745ed56e519462ca31
train
policy_and_value_opt_step
Policy and Value optimizer step.
tensor2tensor/trax/rlax/ppo.py
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)
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)
[ "Policy", "and", "Value", "optimizer", "step", "." ]
tensorflow/tensor2tensor
python
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/trax/rlax/ppo.py#L820-L855
[ "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", ...
272500b6efe353aeb638d2745ed56e519462ca31
train
training_loop
Runs the training loop for PPO, with fixed policy and value nets.
tensor2tensor/trax/rlax/ppo.py
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))
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))
[ "Runs", "the", "training", "loop", "for", "PPO", "with", "fixed", "policy", "and", "value", "nets", "." ]
tensorflow/tensor2tensor
python
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/trax/rlax/ppo.py#L864-L1215
[ "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_fu...
272500b6efe353aeb638d2745ed56e519462ca31
train
_maybe_download_corpora
Download corpora for multinli. Args: tmp_dir: a string Returns: a string
tensor2tensor/data_generators/multinli.py
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
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
[ "Download", "corpora", "for", "multinli", "." ]
tensorflow/tensor2tensor
python
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/data_generators/multinli.py#L42-L59
[ "def", "_maybe_download_corpora", "(", "tmp_dir", ")", ":", "mnli_filename", "=", "\"MNLI.zip\"", "mnli_finalpath", "=", "os", ".", "path", ".", "join", "(", "tmp_dir", ",", "\"MNLI\"", ")", "if", "not", "tf", ".", "gfile", ".", "Exists", "(", "mnli_finalpat...
272500b6efe353aeb638d2745ed56e519462ca31
train
_example_generator
Generate mnli examples. Args: filename: a string Yields: dictionaries containing "premise", "hypothesis" and "label" strings
tensor2tensor/data_generators/multinli.py
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] }
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] }
[ "Generate", "mnli", "examples", "." ]
tensorflow/tensor2tensor
python
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/data_generators/multinli.py#L62-L79
[ "def", "_example_generator", "(", "filename", ")", ":", "for", "idx", ",", "line", "in", "enumerate", "(", "tf", ".", "gfile", ".", "Open", "(", "filename", ",", "\"rb\"", ")", ")", ":", "if", "idx", "==", "0", ":", "continue", "# skip header", "line",...
272500b6efe353aeb638d2745ed56e519462ca31
train
shake_shake_skip_connection
Adds a residual connection to the filter x for the shake-shake model.
tensor2tensor/models/shake_shake.py
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
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
[ "Adds", "a", "residual", "connection", "to", "the", "filter", "x", "for", "the", "shake", "-", "shake", "model", "." ]
tensorflow/tensor2tensor
python
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/models/shake_shake.py#L30-L52
[ "def", "shake_shake_skip_connection", "(", "x", ",", "output_filters", ",", "stride", ",", "is_training", ")", ":", "curr_filters", "=", "common_layers", ".", "shape_list", "(", "x", ")", "[", "-", "1", "]", "if", "curr_filters", "==", "output_filters", ":", ...
272500b6efe353aeb638d2745ed56e519462ca31
train
shake_shake_branch
Building a 2 branching convnet.
tensor2tensor/models/shake_shake.py
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
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
[ "Building", "a", "2", "branching", "convnet", "." ]
tensorflow/tensor2tensor
python
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/models/shake_shake.py#L55-L75
[ "def", "shake_shake_branch", "(", "x", ",", "output_filters", ",", "stride", ",", "rand_forward", ",", "rand_backward", ",", "hparams", ")", ":", "is_training", "=", "hparams", ".", "mode", "==", "tf", ".", "estimator", ".", "ModeKeys", ".", "TRAIN", "x", ...
272500b6efe353aeb638d2745ed56e519462ca31
train
shake_shake_block
Builds a full shake-shake sub layer.
tensor2tensor/models/shake_shake.py
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)
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", "a", "full", "shake", "-", "shake", "sub", "layer", "." ]
tensorflow/tensor2tensor
python
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/models/shake_shake.py#L78-L119
[ "def", "shake_shake_block", "(", "x", ",", "output_filters", ",", "stride", ",", "hparams", ")", ":", "is_training", "=", "hparams", ".", "mode", "==", "tf", ".", "estimator", ".", "ModeKeys", ".", "TRAIN", "batch_size", "=", "common_layers", ".", "shape_lis...
272500b6efe353aeb638d2745ed56e519462ca31
train
shake_shake_layer
Builds many sub layers into one full layer.
tensor2tensor/models/shake_shake.py
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
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
[ "Builds", "many", "sub", "layers", "into", "one", "full", "layer", "." ]
tensorflow/tensor2tensor
python
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/models/shake_shake.py#L122-L128
[ "def", "shake_shake_layer", "(", "x", ",", "output_filters", ",", "num_blocks", ",", "stride", ",", "hparams", ")", ":", "for", "block_num", "in", "range", "(", "num_blocks", ")", ":", "curr_stride", "=", "stride", "if", "(", "block_num", "==", "0", ")", ...
272500b6efe353aeb638d2745ed56e519462ca31
train
shakeshake_small
Parameters for CIFAR-10. Gets to about 96% accuracy@700K steps, 1 GPU.
tensor2tensor/models/shake_shake.py
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
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
[ "Parameters", "for", "CIFAR", "-", "10", ".", "Gets", "to", "about", "96%", "accuracy" ]
tensorflow/tensor2tensor
python
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/models/shake_shake.py#L165-L187
[ "def", "shakeshake_small", "(", ")", ":", "hparams", "=", "common_hparams", ".", "basic_params1", "(", ")", "hparams", ".", "batch_size", "=", "128", "hparams", ".", "hidden_size", "=", "32", "hparams", ".", "layer_prepostprocess_dropout", "=", "0.0", "hparams",...
272500b6efe353aeb638d2745ed56e519462ca31
train
has_metric_plateaued
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.
tensor2tensor/utils/metrics_hook.py
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)
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)
[ "Check", "if", "metric", "has", "plateaued", "." ]
tensorflow/tensor2tensor
python
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/utils/metrics_hook.py#L249-L290
[ "def", "has_metric_plateaued", "(", "steps", ",", "values", ",", "num_steps", "=", "100", ",", "delta", "=", "0.1", ",", "decrease", "=", "True", ")", ":", "assert", "num_steps", ">", "0", "if", "len", "(", "steps", ")", "<", "2", ":", "return", "Fal...
272500b6efe353aeb638d2745ed56e519462ca31
train
next_frame_savp
SAVP model hparams.
tensor2tensor/models/video/savp_params.py
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
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", "model", "hparams", "." ]
tensorflow/tensor2tensor
python
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/models/video/savp_params.py#L27-L56
[ "def", "next_frame_savp", "(", ")", ":", "hparams", "=", "sv2p_params", ".", "next_frame_sv2p", "(", ")", "hparams", ".", "add_hparam", "(", "\"z_dim\"", ",", "8", ")", "hparams", ".", "add_hparam", "(", "\"num_discriminator_filters\"", ",", "32", ")", "hparam...
272500b6efe353aeb638d2745ed56e519462ca31
train
next_frame_savp_vae
SAVP - VAE only model.
tensor2tensor/models/video/savp_params.py
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
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
[ "SAVP", "-", "VAE", "only", "model", "." ]
tensorflow/tensor2tensor
python
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/models/video/savp_params.py#L70-L77
[ "def", "next_frame_savp_vae", "(", ")", ":", "hparams", "=", "next_frame_savp", "(", ")", "hparams", ".", "use_vae", "=", "True", "hparams", ".", "use_gan", "=", "False", "hparams", ".", "latent_loss_multiplier", "=", "1e-3", "hparams", ".", "latent_loss_multipl...
272500b6efe353aeb638d2745ed56e519462ca31
train
next_frame_savp_gan
SAVP - GAN only model.
tensor2tensor/models/video/savp_params.py
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
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
[ "SAVP", "-", "GAN", "only", "model", "." ]
tensorflow/tensor2tensor
python
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/models/video/savp_params.py#L81-L92
[ "def", "next_frame_savp_gan", "(", ")", ":", "hparams", "=", "next_frame_savp", "(", ")", "hparams", ".", "use_gan", "=", "True", "hparams", ".", "use_vae", "=", "False", "hparams", ".", "gan_loss_multiplier", "=", "0.001", "hparams", ".", "optimizer_adam_beta1"...
272500b6efe353aeb638d2745ed56e519462ca31
train
diet_adam_optimizer_params
Default hyperparameters for a DietAdamOptimizer. Returns: a hyperparameters object.
tensor2tensor/utils/diet.py
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 )
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 )
[ "Default", "hyperparameters", "for", "a", "DietAdamOptimizer", "." ]
tensorflow/tensor2tensor
python
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/utils/diet.py#L34-L51
[ "def", "diet_adam_optimizer_params", "(", ")", ":", "return", "hparam", ".", "HParams", "(", "quantize", "=", "True", ",", "# use 16-bit fixed-point", "quantization_scale", "=", "10.0", "/", "tf", ".", "int16", ".", "max", ",", "optimizer", "=", "\"DietAdam\"", ...
272500b6efe353aeb638d2745ed56e519462ca31
train
diet_expert
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]
tensor2tensor/utils/diet.py
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)
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)
[ "A", "two", "-", "layer", "feed", "-", "forward", "network", "with", "relu", "activation", "on", "hidden", "layer", "." ]
tensorflow/tensor2tensor
python
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/utils/diet.py#L54-L77
[ "def", "diet_expert", "(", "x", ",", "hidden_size", ",", "params", ")", ":", "@", "fn_with_diet_vars", "(", "params", ")", "def", "diet_expert_internal", "(", "x", ")", ":", "dim", "=", "x", ".", "get_shape", "(", ")", ".", "as_list", "(", ")", "[", ...
272500b6efe353aeb638d2745ed56e519462ca31
train
_quantize
Quantize x according to params, optionally randomizing the rounding.
tensor2tensor/utils/diet.py
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
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
[ "Quantize", "x", "according", "to", "params", "optionally", "randomizing", "the", "rounding", "." ]
tensorflow/tensor2tensor
python
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/utils/diet.py#L235-L250
[ "def", "_quantize", "(", "x", ",", "params", ",", "randomize", "=", "True", ")", ":", "if", "not", "params", ".", "quantize", ":", "return", "x", "if", "not", "randomize", ":", "return", "tf", ".", "bitcast", "(", "tf", ".", "cast", "(", "x", "/", ...
272500b6efe353aeb638d2745ed56e519462ca31
train
_dequantize
Dequantize q according to params.
tensor2tensor/utils/diet.py
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
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
[ "Dequantize", "q", "according", "to", "params", "." ]
tensorflow/tensor2tensor
python
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/utils/diet.py#L253-L257
[ "def", "_dequantize", "(", "q", ",", "params", ")", ":", "if", "not", "params", ".", "quantize", ":", "return", "q", "return", "tf", ".", "to_float", "(", "tf", ".", "bitcast", "(", "q", ",", "tf", ".", "int16", ")", ")", "*", "params", ".", "qua...
272500b6efe353aeb638d2745ed56e519462ca31
train
make_diet_var_getter
Create a custom variable getter for diet variables according to params.
tensor2tensor/utils/diet.py
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
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
[ "Create", "a", "custom", "variable", "getter", "for", "diet", "variables", "according", "to", "params", "." ]
tensorflow/tensor2tensor
python
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/utils/diet.py#L260-L293
[ "def", "make_diet_var_getter", "(", "params", ")", ":", "def", "diet_var_initializer", "(", "shape", ",", "dtype", ",", "partition_info", "=", "None", ")", ":", "\"\"\"Initializer for a diet variable.\"\"\"", "del", "dtype", "del", "partition_info", "with", "common_la...
272500b6efe353aeb638d2745ed56e519462ca31
train
_fn_with_diet_vars
Call function with args; use diet variables according to params.
tensor2tensor/utils/diet.py
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
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
[ "Call", "function", "with", "args", ";", "use", "diet", "variables", "according", "to", "params", "." ]
tensorflow/tensor2tensor
python
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/utils/diet.py#L296-L349
[ "def", "_fn_with_diet_vars", "(", "fn", ",", "args", ",", "params", ")", ":", "vs_ctr", "=", "[", "]", "def", "grad_fn", "(", "inputs", ",", "variables", ",", "outputs", ",", "output_grads", ")", ":", "\"\"\"Custom gradient function.\"\"\"", "del", "outputs", ...
272500b6efe353aeb638d2745ed56e519462ca31
train
fn_with_diet_vars
Decorator for graph-building function to use diet variables.
tensor2tensor/utils/diet.py
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
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
[ "Decorator", "for", "graph", "-", "building", "function", "to", "use", "diet", "variables", "." ]
tensorflow/tensor2tensor
python
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/utils/diet.py#L352-L363
[ "def", "fn_with_diet_vars", "(", "params", ")", ":", "params", "=", "copy", ".", "copy", "(", "params", ")", "def", "dec", "(", "fn", ")", ":", "def", "wrapped", "(", "*", "args", ")", ":", "return", "_fn_with_diet_vars", "(", "fn", ",", "args", ",",...
272500b6efe353aeb638d2745ed56e519462ca31
train
DietAdamOptimizer.create_slots
Create the factorized Adam accumulators for diet variables.
tensor2tensor/utils/diet.py
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())
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())
[ "Create", "the", "factorized", "Adam", "accumulators", "for", "diet", "variables", "." ]
tensorflow/tensor2tensor
python
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/utils/diet.py#L144-L175
[ "def", "create_slots", "(", "self", ",", "var", ")", ":", "params", "=", "self", ".", "params", "shape", "=", "var", ".", "get_shape", "(", ")", ".", "as_list", "(", ")", "if", "not", "hasattr", "(", "params", ",", "\"slots\"", ")", ":", "params", ...
272500b6efe353aeb638d2745ed56e519462ca31
train
DietAdamOptimizer.update_variable
Update the variable and its slots.
tensor2tensor/utils/diet.py
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)
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)
[ "Update", "the", "variable", "and", "its", "slots", "." ]
tensorflow/tensor2tensor
python
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/utils/diet.py#L177-L225
[ "def", "update_variable", "(", "self", ",", "var", ",", "grad_var", ")", ":", "params", "=", "self", ".", "params", "global_step", "=", "tf", ".", "to_float", "(", "self", ".", "global_step", ")", "+", "1", "# compute learning rate", "lrate", "=", "params"...
272500b6efe353aeb638d2745ed56e519462ca31
train
MtfModel.estimator_spec_eval
Construct EstimatorSpec for EVAL mode.
tensor2tensor/utils/mtf_model.py
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)
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)
[ "Construct", "EstimatorSpec", "for", "EVAL", "mode", "." ]
tensorflow/tensor2tensor
python
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/utils/mtf_model.py#L188-L229
[ "def", "estimator_spec_eval", "(", "self", ",", "features", ",", "logits", ",", "labels", ",", "loss", ",", "restore_hook", ",", "use_tpu", ")", ":", "hparams", "=", "self", ".", "hparams", "problem", "=", "hparams", ".", "problem", "if", "logits", ".", ...
272500b6efe353aeb638d2745ed56e519462ca31
train
generator_samples
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.
tensor2tensor/data_generators/desc2code.py
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)
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)
[ "Generator", "for", "the", "dataset", "samples", "." ]
tensorflow/tensor2tensor
python
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/data_generators/desc2code.py#L240-L308
[ "def", "generator_samples", "(", "tmp_dir", ",", "pb_cst", ")", ":", "# Step1: Download dataset (eventually)", "data_zip_path", "=", "generator_utils", ".", "maybe_download_from_drive", "(", "directory", "=", "tmp_dir", ",", "filename", "=", "_DATASET_FILENAME", ",", "u...
272500b6efe353aeb638d2745ed56e519462ca31
train
lstm
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.
tensor2tensor/models/lstm.py
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)
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)
[ "Adds", "a", "stack", "of", "LSTM", "layers", "on", "top", "of", "input", "." ]
tensorflow/tensor2tensor
python
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/models/lstm.py#L38-L67
[ "def", "lstm", "(", "inputs", ",", "sequence_length", ",", "hparams", ",", "train", ",", "name", ",", "initial_state", "=", "None", ")", ":", "layers", "=", "[", "_dropout_lstm_cell", "(", "hparams", ",", "train", ")", "for", "_", "in", "range", "(", "...
272500b6efe353aeb638d2745ed56e519462ca31
train
lstm_attention_decoder
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]`.
tensor2tensor/models/lstm.py
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
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
[ "Run", "LSTM", "cell", "with", "attention", "on", "inputs", "of", "shape", "[", "batch", "x", "time", "x", "size", "]", "." ]
tensorflow/tensor2tensor
python
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/models/lstm.py#L70-L174
[ "def", "lstm_attention_decoder", "(", "inputs", ",", "hparams", ",", "train", ",", "name", ",", "initial_state", ",", "encoder_outputs", ",", "encoder_output_length", ",", "decoder_input_length", ")", ":", "layers", "=", "[", "_dropout_lstm_cell", "(", "hparams", ...
272500b6efe353aeb638d2745ed56e519462ca31
train
lstm_seq2seq_internal
The basic LSTM seq2seq model, main step used for training.
tensor2tensor/models/lstm.py
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)
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)
[ "The", "basic", "LSTM", "seq2seq", "model", "main", "step", "used", "for", "training", "." ]
tensorflow/tensor2tensor
python
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/models/lstm.py#L177-L203
[ "def", "lstm_seq2seq_internal", "(", "inputs", ",", "targets", ",", "hparams", ",", "train", ")", ":", "with", "tf", ".", "variable_scope", "(", "\"lstm_seq2seq\"", ")", ":", "if", "inputs", "is", "not", "None", ":", "inputs_length", "=", "common_layers", "....
272500b6efe353aeb638d2745ed56e519462ca31
train
lstm_seq2seq_internal_attention
LSTM seq2seq model with attention, main step used for training.
tensor2tensor/models/lstm.py
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)
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)
[ "LSTM", "seq2seq", "model", "with", "attention", "main", "step", "used", "for", "training", "." ]
tensorflow/tensor2tensor
python
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/models/lstm.py#L206-L225
[ "def", "lstm_seq2seq_internal_attention", "(", "inputs", ",", "targets", ",", "hparams", ",", "train", ",", "inputs_length", ",", "targets_length", ")", ":", "with", "tf", ".", "variable_scope", "(", "\"lstm_seq2seq_attention\"", ")", ":", "# Flatten inputs.", "inpu...
272500b6efe353aeb638d2745ed56e519462ca31
train
lstm_bid_encoder
Bidirectional LSTM for encoding inputs that are [batch x time x size].
tensor2tensor/models/lstm.py
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
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
[ "Bidirectional", "LSTM", "for", "encoding", "inputs", "that", "are", "[", "batch", "x", "time", "x", "size", "]", "." ]
tensorflow/tensor2tensor
python
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/models/lstm.py#L228-L273
[ "def", "lstm_bid_encoder", "(", "inputs", ",", "sequence_length", ",", "hparams", ",", "train", ",", "name", ")", ":", "with", "tf", ".", "variable_scope", "(", "name", ")", ":", "cell_fw", "=", "tf", ".", "nn", ".", "rnn_cell", ".", "MultiRNNCell", "(",...
272500b6efe353aeb638d2745ed56e519462ca31
train
lstm_seq2seq_internal_bid_encoder
The basic LSTM seq2seq model with bidirectional encoder.
tensor2tensor/models/lstm.py
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)
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)
[ "The", "basic", "LSTM", "seq2seq", "model", "with", "bidirectional", "encoder", "." ]
tensorflow/tensor2tensor
python
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/models/lstm.py#L276-L302
[ "def", "lstm_seq2seq_internal_bid_encoder", "(", "inputs", ",", "targets", ",", "hparams", ",", "train", ")", ":", "with", "tf", ".", "variable_scope", "(", "\"lstm_seq2seq_bid_encoder\"", ")", ":", "if", "inputs", "is", "not", "None", ":", "inputs_length", "=",...
272500b6efe353aeb638d2745ed56e519462ca31
train
lstm_seq2seq_internal_attention_bid_encoder
LSTM seq2seq model with attention, main step used for training.
tensor2tensor/models/lstm.py
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)
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)
[ "LSTM", "seq2seq", "model", "with", "attention", "main", "step", "used", "for", "training", "." ]
tensorflow/tensor2tensor
python
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/models/lstm.py#L305-L325
[ "def", "lstm_seq2seq_internal_attention_bid_encoder", "(", "inputs", ",", "targets", ",", "hparams", ",", "train", ")", ":", "with", "tf", ".", "variable_scope", "(", "\"lstm_seq2seq_attention_bid_encoder\"", ")", ":", "inputs_length", "=", "common_layers", ".", "leng...
272500b6efe353aeb638d2745ed56e519462ca31
train
lstm_seq2seq
hparams for LSTM.
tensor2tensor/models/lstm.py
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
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
[ "hparams", "for", "LSTM", "." ]
tensorflow/tensor2tensor
python
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/models/lstm.py#L410-L420
[ "def", "lstm_seq2seq", "(", ")", ":", "hparams", "=", "common_hparams", ".", "basic_params1", "(", ")", "hparams", ".", "daisy_chain_variables", "=", "False", "hparams", ".", "batch_size", "=", "1024", "hparams", ".", "hidden_size", "=", "128", "hparams", ".",...
272500b6efe353aeb638d2745ed56e519462ca31
train
lstm_attention_base
Base attention params.
tensor2tensor/models/lstm.py
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
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
[ "Base", "attention", "params", "." ]
tensorflow/tensor2tensor
python
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/models/lstm.py#L423-L429
[ "def", "lstm_attention_base", "(", ")", ":", "hparams", "=", "lstm_seq2seq", "(", ")", "hparams", ".", "add_hparam", "(", "\"attention_layer_size\"", ",", "hparams", ".", "hidden_size", ")", "hparams", ".", "add_hparam", "(", "\"output_attention\"", ",", "True", ...
272500b6efe353aeb638d2745ed56e519462ca31
train
lstm_asr_v1
Basic LSTM Params.
tensor2tensor/models/lstm.py
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
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
[ "Basic", "LSTM", "Params", "." ]
tensorflow/tensor2tensor
python
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/models/lstm.py#L471-L482
[ "def", "lstm_asr_v1", "(", ")", ":", "hparams", "=", "lstm_bahdanau_attention", "(", ")", "hparams", ".", "num_hidden_layers", "=", "2", "hparams", ".", "hidden_size", "=", "256", "hparams", ".", "batch_size", "=", "36", "hparams", ".", "max_input_seq_length", ...
272500b6efe353aeb638d2745ed56e519462ca31
train
lstm_area_attention_base
Hparams for LSTM with area attention.
tensor2tensor/models/lstm.py
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
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
[ "Hparams", "for", "LSTM", "with", "area", "attention", "." ]
tensorflow/tensor2tensor
python
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/models/lstm.py#L486-L498
[ "def", "lstm_area_attention_base", "(", ")", ":", "hparams", "=", "lstm_luong_attention", "(", ")", "hparams", ".", "batch_size", "=", "16384", "hparams", ".", "num_hidden_layers", "=", "2", "hparams", ".", "hidden_size", "=", "1024", "hparams", ".", "num_heads"...
272500b6efe353aeb638d2745ed56e519462ca31
train
create_surrogate_run_config
Create a run config. Args: hp: model hyperparameters Returns: a run config
tensor2tensor/bin/t2t_attack.py
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)
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)
[ "Create", "a", "run", "config", "." ]
tensorflow/tensor2tensor
python
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/bin/t2t_attack.py#L83-L131
[ "def", "create_surrogate_run_config", "(", "hp", ")", ":", "save_ckpt_steps", "=", "max", "(", "FLAGS", ".", "iterations_per_loop", ",", "FLAGS", ".", "local_eval_frequency", ")", "save_ckpt_secs", "=", "FLAGS", ".", "save_checkpoints_secs", "or", "None", "if", "s...
272500b6efe353aeb638d2745ed56e519462ca31
train
prepare_data
Construct input pipeline.
tensor2tensor/bin/t2t_attack.py
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
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
[ "Construct", "input", "pipeline", "." ]
tensorflow/tensor2tensor
python
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/bin/t2t_attack.py#L134-L145
[ "def", "prepare_data", "(", "problem", ",", "hparams", ",", "params", ",", "config", ")", ":", "input_fn", "=", "problem", ".", "make_estimator_input_fn", "(", "tf", ".", "estimator", ".", "ModeKeys", ".", "EVAL", ",", "hparams", ",", "force_repeat", "=", ...
272500b6efe353aeb638d2745ed56e519462ca31
train
AudioEncoder.encode
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
tensor2tensor/data_generators/audio_encoder.py
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()
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", "string", "with", "a", "filename", "into", "a", "list", "of", "float32", "." ]
tensorflow/tensor2tensor
python
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/data_generators/audio_encoder.py#L36-L65
[ "def", "encode", "(", "self", ",", "s", ")", ":", "# Make sure that the data is a single channel, 16bit, 16kHz wave.", "# TODO(chorowski): the directory may not be writable, this should fallback", "# to a temp path, and provide instructions for installing sox.", "if", "s", ".", "endswith...
272500b6efe353aeb638d2745ed56e519462ca31
train
AudioEncoder.decode
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.
tensor2tensor/data_generators/audio_encoder.py
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
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
[ "Transform", "a", "sequence", "of", "float32", "into", "a", "waveform", "." ]
tensorflow/tensor2tensor
python
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/data_generators/audio_encoder.py#L67-L81
[ "def", "decode", "(", "self", ",", "ids", ")", ":", "_", ",", "tmp_file_path", "=", "tempfile", ".", "mkstemp", "(", ")", "wavfile", ".", "write", "(", "tmp_file_path", ",", "self", ".", "_sample_rate", ",", "np", ".", "asarray", "(", "ids", ")", ")"...
272500b6efe353aeb638d2745ed56e519462ca31
train
Graph.new_vertex
Creates and returns a new vertex. Returns: A new Vertex instance with a unique index.
tensor2tensor/insights/graph.py
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
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
[ "Creates", "and", "returns", "a", "new", "vertex", "." ]
tensorflow/tensor2tensor
python
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/insights/graph.py#L102-L110
[ "def", "new_vertex", "(", "self", ")", ":", "vertex", "=", "Vertex", "(", "len", "(", "self", ".", "vertices", ")", ")", "self", ".", "vertices", ".", "append", "(", "vertex", ")", "return", "vertex" ]
272500b6efe353aeb638d2745ed56e519462ca31
train
Graph.get_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.
tensor2tensor/insights/graph.py
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
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", "or", "Creates", "a", "Vertex", "mapped", "by", "key", "." ]
tensorflow/tensor2tensor
python
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/insights/graph.py#L112-L126
[ "def", "get_vertex", "(", "self", ",", "key", ")", ":", "if", "key", "in", "self", ".", "vertex_map", ":", "return", "self", ".", "vertex_map", "[", "key", "]", "vertex", "=", "self", ".", "new_vertex", "(", ")", "self", ".", "vertex_map", "[", "key"...
272500b6efe353aeb638d2745ed56e519462ca31
train
Graph.add_edge
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.
tensor2tensor/insights/graph.py
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
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", "new", "edge", "connecting", "source", "and", "target", "vertices", "." ]
tensorflow/tensor2tensor
python
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/insights/graph.py#L128-L144
[ "def", "add_edge", "(", "self", ",", "source", ",", "target", ")", ":", "edge", "=", "Edge", "(", "len", "(", "self", ".", "edges", ")", ")", "self", ".", "edges", ".", "append", "(", "edge", ")", "source", ".", "out_edges", ".", "append", "(", "...
272500b6efe353aeb638d2745ed56e519462ca31
train
Graph.to_dict
Returns a simplified dictionary representing the Graph. Returns: A dictionary that can easily be serialized to JSON.
tensor2tensor/insights/graph.py
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] }
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] }
[ "Returns", "a", "simplified", "dictionary", "representing", "the", "Graph", "." ]
tensorflow/tensor2tensor
python
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/insights/graph.py#L146-L155
[ "def", "to_dict", "(", "self", ")", ":", "return", "{", "\"node\"", ":", "[", "v", ".", "to_dict", "(", ")", "for", "v", "in", "self", ".", "vertices", "]", ",", "\"edge\"", ":", "[", "e", ".", "to_dict", "(", ")", "for", "e", "in", "self", "."...
272500b6efe353aeb638d2745ed56e519462ca31
train
attend
Self-attention layer with source as memory antecedent.
tensor2tensor/models/research/transformer_vae.py
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)
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)
[ "Self", "-", "attention", "layer", "with", "source", "as", "memory", "antecedent", "." ]
tensorflow/tensor2tensor
python
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/models/research/transformer_vae.py#L62-L76
[ "def", "attend", "(", "x", ",", "source", ",", "hparams", ",", "name", ")", ":", "with", "tf", ".", "variable_scope", "(", "name", ")", ":", "x", "=", "tf", ".", "squeeze", "(", "x", ",", "axis", "=", "2", ")", "if", "len", "(", "source", ".", ...
272500b6efe353aeb638d2745ed56e519462ca31
train
top_k_softmax
Calculate softmax(x), select top-k and rescale to sum to 1.
tensor2tensor/models/research/transformer_vae.py
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)
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)
[ "Calculate", "softmax", "(", "x", ")", "select", "top", "-", "k", "and", "rescale", "to", "sum", "to", "1", "." ]
tensorflow/tensor2tensor
python
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/models/research/transformer_vae.py#L93-L100
[ "def", "top_k_softmax", "(", "x", ",", "k", ")", ":", "x", "=", "tf", ".", "nn", ".", "softmax", "(", "x", ")", "top_x", ",", "_", "=", "tf", ".", "nn", ".", "top_k", "(", "x", ",", "k", "=", "k", "+", "1", ")", "min_top", "=", "tf", ".",...
272500b6efe353aeb638d2745ed56e519462ca31
train
compress
Compress.
tensor2tensor/models/research/transformer_vae.py
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
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
[ "Compress", "." ]
tensorflow/tensor2tensor
python
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/models/research/transformer_vae.py#L115-L132
[ "def", "compress", "(", "x", ",", "c", ",", "is_2d", ",", "hparams", ",", "name", ")", ":", "with", "tf", ".", "variable_scope", "(", "name", ")", ":", "# Run compression by strided convs.", "cur", "=", "x", "k1", "=", "(", "3", ",", "3", ")", "if", ...
272500b6efe353aeb638d2745ed56e519462ca31
train
decode_transformer
Original Transformer decoder.
tensor2tensor/models/research/transformer_vae.py
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
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
[ "Original", "Transformer", "decoder", "." ]
tensorflow/tensor2tensor
python
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/models/research/transformer_vae.py#L145-L208
[ "def", "decode_transformer", "(", "encoder_output", ",", "encoder_decoder_attention_bias", ",", "targets", ",", "hparams", ",", "name", ",", "task", "=", "None", ",", "causal", "=", "True", ")", ":", "orig_hparams", "=", "hparams", "with", "tf", ".", "variable...
272500b6efe353aeb638d2745ed56e519462ca31
train
ae_latent_softmax
Latent prediction and loss.
tensor2tensor/models/research/transformer_vae.py
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
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
[ "Latent", "prediction", "and", "loss", "." ]
tensorflow/tensor2tensor
python
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/models/research/transformer_vae.py#L221-L267
[ "def", "ae_latent_softmax", "(", "latents_pred", ",", "latents_discrete", ",", "hparams", ")", ":", "vocab_size", "=", "2", "**", "hparams", ".", "z_size", "if", "hparams", ".", "num_decode_blocks", "<", "2", ":", "latents_logits", "=", "tf", ".", "layers", ...
272500b6efe353aeb638d2745ed56e519462ca31
train
ae_latent_sample
Sample from the latent space in the autoencoder.
tensor2tensor/models/research/transformer_vae.py
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
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
[ "Sample", "from", "the", "latent", "space", "in", "the", "autoencoder", "." ]
tensorflow/tensor2tensor
python
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/models/research/transformer_vae.py#L301-L322
[ "def", "ae_latent_sample", "(", "latents_dense", ",", "inputs", ",", "ed", ",", "embed", ",", "iters", ",", "hparams", ")", ":", "if", "hparams", ".", "num_decode_blocks", "<", "2", "and", "hparams", ".", "sampling_temp", "==", "0.0", ":", "# TODO(lukaszkais...
272500b6efe353aeb638d2745ed56e519462ca31
train
ae_transformer_internal
AE Transformer, main step used for training.
tensor2tensor/models/research/transformer_vae.py
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
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
[ "AE", "Transformer", "main", "step", "used", "for", "training", "." ]
tensorflow/tensor2tensor
python
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/models/research/transformer_vae.py#L325-L536
[ "def", "ae_transformer_internal", "(", "inputs", ",", "targets", ",", "target_space", ",", "hparams", ",", "cache", "=", "None", ",", "predict_mask", "=", "1.0", ")", ":", "# Summaries break with the do_refine cond, turn them off in that case.", "global", "_DO_SUMMARIES",...
272500b6efe353aeb638d2745ed56e519462ca31
train
transformer_ae_small
Set of hyperparameters.
tensor2tensor/models/research/transformer_vae.py
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
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
[ "Set", "of", "hyperparameters", "." ]
tensorflow/tensor2tensor
python
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/models/research/transformer_vae.py#L760-L830
[ "def", "transformer_ae_small", "(", ")", ":", "hparams", "=", "transformer", ".", "transformer_small", "(", ")", "hparams", ".", "batch_size", "=", "2048", "hparams", ".", "learning_rate", "=", "0.2", "hparams", ".", "learning_rate_warmup_steps", "=", "4000", "h...
272500b6efe353aeb638d2745ed56e519462ca31
train
imagetransformer_ae_cifar
Hyperparameters for CIFAR-10 experiments.
tensor2tensor/models/research/transformer_vae.py
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
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
[ "Hyperparameters", "for", "CIFAR", "-", "10", "experiments", "." ]
tensorflow/tensor2tensor
python
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/models/research/transformer_vae.py#L834-L904
[ "def", "imagetransformer_ae_cifar", "(", ")", ":", "hparams", "=", "transformer_ae_small", "(", ")", "hparams", ".", "filter_size", "=", "512", "hparams", ".", "num_compress_steps", "=", "3", "hparams", ".", "startup_steps", "=", "10000", "hparams", ".", "is_2d"...
272500b6efe353aeb638d2745ed56e519462ca31
train
imagetransformer_ae_imagenet
For 64x64 ImageNet. ~56M trainable variables.
tensor2tensor/models/research/transformer_vae.py
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
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
[ "For", "64x64", "ImageNet", ".", "~56M", "trainable", "variables", "." ]
tensorflow/tensor2tensor
python
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/models/research/transformer_vae.py#L907-L916
[ "def", "imagetransformer_ae_imagenet", "(", ")", ":", "hparams", "=", "imagetransformer_ae_cifar", "(", ")", "hparams", ".", "max_length", "=", "int", "(", "64", "*", "64", "*", "3", ")", "hparams", ".", "img_len", "=", "64", "hparams", ".", "num_heads", "...
272500b6efe353aeb638d2745ed56e519462ca31
train
transformer_ae_base
Set of hyperparameters.
tensor2tensor/models/research/transformer_vae.py
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
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", "." ]
tensorflow/tensor2tensor
python
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/models/research/transformer_vae.py#L920-L927
[ "def", "transformer_ae_base", "(", ")", ":", "hparams", "=", "transformer_ae_small", "(", ")", "hparams", ".", "batch_size", "=", "2048", "hparams", ".", "hidden_size", "=", "512", "hparams", ".", "filter_size", "=", "4096", "hparams", ".", "num_hidden_layers", ...
272500b6efe353aeb638d2745ed56e519462ca31
train
transformer_ae_a3
Set of hyperparameters.
tensor2tensor/models/research/transformer_vae.py
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
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", "." ]
tensorflow/tensor2tensor
python
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/models/research/transformer_vae.py#L931-L939
[ "def", "transformer_ae_a3", "(", ")", ":", "hparams", "=", "transformer_ae_base", "(", ")", "hparams", ".", "batch_size", "=", "4096", "hparams", ".", "layer_prepostprocess_dropout", "=", "0.3", "hparams", ".", "optimizer", "=", "\"Adafactor\"", "hparams", ".", ...
272500b6efe353aeb638d2745ed56e519462ca31
train
transformer_ae_base_noatt
Set of hyperparameters.
tensor2tensor/models/research/transformer_vae.py
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
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", "." ]
tensorflow/tensor2tensor
python
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/models/research/transformer_vae.py#L970-L980
[ "def", "transformer_ae_base_noatt", "(", ")", ":", "hparams", "=", "transformer_ae_base", "(", ")", "hparams", ".", "reshape_method", "=", "\"slice\"", "hparams", ".", "bottleneck_kind", "=", "\"dvq\"", "hparams", ".", "hidden_size", "=", "512", "hparams", ".", ...
272500b6efe353aeb638d2745ed56e519462ca31
train
transformer_ae_small_noatt
Set of hyperparameters.
tensor2tensor/models/research/transformer_vae.py
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
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
[ "Set", "of", "hyperparameters", "." ]
tensorflow/tensor2tensor
python
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/models/research/transformer_vae.py#L984-L994
[ "def", "transformer_ae_small_noatt", "(", ")", ":", "hparams", "=", "transformer_ae_small", "(", ")", "hparams", ".", "reshape_method", "=", "\"slice\"", "hparams", ".", "bottleneck_kind", "=", "\"dvq\"", "hparams", ".", "hidden_size", "=", "512", "hparams", ".", ...
272500b6efe353aeb638d2745ed56e519462ca31
train
transformer_sketch
Basic transformer_sketch hparams.
tensor2tensor/models/research/transformer_sketch.py
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
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
[ "Basic", "transformer_sketch", "hparams", "." ]
tensorflow/tensor2tensor
python
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/models/research/transformer_sketch.py#L55-L62
[ "def", "transformer_sketch", "(", ")", ":", "hparams", "=", "transformer", ".", "transformer_small", "(", ")", "hparams", ".", "num_compress_steps", "=", "4", "hparams", ".", "batch_size", "=", "32", "hparams", ".", "clip_grad_norm", "=", "2.", "hparams", ".",...
272500b6efe353aeb638d2745ed56e519462ca31
train
layers
Get the layers module good for TF 1 and TF 2 work for now.
tensor2tensor/layers/common_layers.py
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
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
[ "Get", "the", "layers", "module", "good", "for", "TF", "1", "and", "TF", "2", "work", "for", "now", "." ]
tensorflow/tensor2tensor
python
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/layers/common_layers.py#L42-L56
[ "def", "layers", "(", ")", ":", "global", "_cached_layers", "if", "_cached_layers", "is", "not", "None", ":", "return", "_cached_layers", "layers_module", "=", "tf", ".", "layers", "try", ":", "from", "tensorflow", ".", "python", "import", "tf2", "# pylint: di...
272500b6efe353aeb638d2745ed56e519462ca31
train
dropout_with_broadcast_dims
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.
tensor2tensor/layers/common_layers.py
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)
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)
[ "Like", "tf", ".", "nn", ".", "dropout", "but", "takes", "broadcast_dims", "instead", "of", "noise_shape", "." ]
tensorflow/tensor2tensor
python
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/layers/common_layers.py#L103-L130
[ "def", "dropout_with_broadcast_dims", "(", "x", ",", "keep_prob", ",", "broadcast_dims", "=", "None", ",", "*", "*", "kwargs", ")", ":", "assert", "\"noise_shape\"", "not", "in", "kwargs", "if", "broadcast_dims", ":", "shape", "=", "tf", ".", "shape", "(", ...
272500b6efe353aeb638d2745ed56e519462ca31
train
saturating_sigmoid
Saturating sigmoid: 1.2 * sigmoid(x) - 0.1 cut to [0, 1].
tensor2tensor/layers/common_layers.py
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))
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))
[ "Saturating", "sigmoid", ":", "1", ".", "2", "*", "sigmoid", "(", "x", ")", "-", "0", ".", "1", "cut", "to", "[", "0", "1", "]", "." ]
tensorflow/tensor2tensor
python
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/layers/common_layers.py#L137-L141
[ "def", "saturating_sigmoid", "(", "x", ")", ":", "with", "tf", ".", "name_scope", "(", "\"saturating_sigmoid\"", ",", "values", "=", "[", "x", "]", ")", ":", "y", "=", "tf", ".", "sigmoid", "(", "x", ")", "return", "tf", ".", "minimum", "(", "1.0", ...
272500b6efe353aeb638d2745ed56e519462ca31
train
inverse_exp_decay
Inverse-decay exponentially from 0.01 to 1.0 reached at max_step.
tensor2tensor/layers/common_layers.py
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)
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", "exponentially", "from", "0", ".", "01", "to", "1", ".", "0", "reached", "at", "max_step", "." ]
tensorflow/tensor2tensor
python
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/layers/common_layers.py#L155-L163
[ "def", "inverse_exp_decay", "(", "max_step", ",", "min_value", "=", "0.01", ",", "step", "=", "None", ")", ":", "inv_base", "=", "tf", ".", "exp", "(", "tf", ".", "log", "(", "min_value", ")", "/", "float", "(", "max_step", ")", ")", "if", "step", ...
272500b6efe353aeb638d2745ed56e519462ca31
train
inverse_lin_decay
Inverse-decay linearly from 0.01 to 1.0 reached at max_step.
tensor2tensor/layers/common_layers.py
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
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
[ "Inverse", "-", "decay", "linearly", "from", "0", ".", "01", "to", "1", ".", "0", "reached", "at", "max_step", "." ]
tensorflow/tensor2tensor
python
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/layers/common_layers.py#L166-L174
[ "def", "inverse_lin_decay", "(", "max_step", ",", "min_value", "=", "0.01", ",", "step", "=", "None", ")", ":", "if", "step", "is", "None", ":", "step", "=", "tf", ".", "train", ".", "get_global_step", "(", ")", "if", "step", "is", "None", ":", "retu...
272500b6efe353aeb638d2745ed56e519462ca31
train
shakeshake2_py
The shake-shake sum of 2 tensors, python version.
tensor2tensor/layers/common_layers.py
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
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
[ "The", "shake", "-", "shake", "sum", "of", "2", "tensors", "python", "version", "." ]
tensorflow/tensor2tensor
python
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/layers/common_layers.py#L177-L186
[ "def", "shakeshake2_py", "(", "x", ",", "y", ",", "equal", "=", "False", ",", "individual", "=", "False", ")", ":", "if", "equal", ":", "alpha", "=", "0.5", "elif", "individual", ":", "alpha", "=", "tf", ".", "random_uniform", "(", "tf", ".", "get_sh...
272500b6efe353aeb638d2745ed56e519462ca31
train
shakeshake2_grad
Overriding gradient for shake-shake of 2 tensors.
tensor2tensor/layers/common_layers.py
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
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", "." ]
tensorflow/tensor2tensor
python
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/layers/common_layers.py#L190-L194
[ "def", "shakeshake2_grad", "(", "x1", ",", "x2", ",", "dy", ")", ":", "y", "=", "shakeshake2_py", "(", "x1", ",", "x2", ")", "dx", "=", "tf", ".", "gradients", "(", "ys", "=", "[", "y", "]", ",", "xs", "=", "[", "x1", ",", "x2", "]", ",", "...
272500b6efe353aeb638d2745ed56e519462ca31
train
shakeshake2_indiv_grad
Overriding gradient for shake-shake of 2 tensors.
tensor2tensor/layers/common_layers.py
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
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", "." ]
tensorflow/tensor2tensor
python
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/layers/common_layers.py#L198-L202
[ "def", "shakeshake2_indiv_grad", "(", "x1", ",", "x2", ",", "dy", ")", ":", "y", "=", "shakeshake2_py", "(", "x1", ",", "x2", ",", "individual", "=", "True", ")", "dx", "=", "tf", ".", "gradients", "(", "ys", "=", "[", "y", "]", ",", "xs", "=", ...
272500b6efe353aeb638d2745ed56e519462ca31
train
shakeshake2_equal_grad
Overriding gradient for shake-shake of 2 tensors.
tensor2tensor/layers/common_layers.py
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
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
[ "Overriding", "gradient", "for", "shake", "-", "shake", "of", "2", "tensors", "." ]
tensorflow/tensor2tensor
python
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/layers/common_layers.py#L206-L210
[ "def", "shakeshake2_equal_grad", "(", "x1", ",", "x2", ",", "dy", ")", ":", "y", "=", "shakeshake2_py", "(", "x1", ",", "x2", ",", "equal", "=", "True", ")", "dx", "=", "tf", ".", "gradients", "(", "ys", "=", "[", "y", "]", ",", "xs", "=", "[",...
272500b6efe353aeb638d2745ed56e519462ca31
train
shakeshake
Multi-argument shake-shake, currently approximated by sums of 2.
tensor2tensor/layers/common_layers.py
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)
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)
[ "Multi", "-", "argument", "shake", "-", "shake", "currently", "approximated", "by", "sums", "of", "2", "." ]
tensorflow/tensor2tensor
python
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/layers/common_layers.py#L230-L239
[ "def", "shakeshake", "(", "xs", ",", "equal_grad", "=", "False", ")", ":", "if", "len", "(", "xs", ")", "==", "1", ":", "return", "xs", "[", "0", "]", "div", "=", "(", "len", "(", "xs", ")", "+", "1", ")", "//", "2", "arg1", "=", "shakeshake"...
272500b6efe353aeb638d2745ed56e519462ca31
train
convert_rgb_to_real
Conversion of pixel values to real numbers.
tensor2tensor/layers/common_layers.py
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
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", "." ]
tensorflow/tensor2tensor
python
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/layers/common_layers.py#L242-L247
[ "def", "convert_rgb_to_real", "(", "x", ")", ":", "with", "tf", ".", "name_scope", "(", "\"rgb_to_real\"", ",", "values", "=", "[", "x", "]", ")", ":", "x", "=", "to_float", "(", "x", ")", "x", "/=", "255.0", "return", "x" ]
272500b6efe353aeb638d2745ed56e519462ca31
train
convert_rgb_to_symmetric_real
Conversion of pixel values to real numbers.
tensor2tensor/layers/common_layers.py
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
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
[ "Conversion", "of", "pixel", "values", "to", "real", "numbers", "." ]
tensorflow/tensor2tensor
python
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/layers/common_layers.py#L250-L257
[ "def", "convert_rgb_to_symmetric_real", "(", "x", ")", ":", "with", "tf", ".", "name_scope", "(", "\"rgb_to_real\"", ",", "values", "=", "[", "x", "]", ")", ":", "x", "=", "to_float", "(", "x", ")", "# Convert each pixel intensity in [0, 1, 2, ..., 255] into a rea...
272500b6efe353aeb638d2745ed56e519462ca31
train
expand_squeeze_to_nd
Make x n-d with squeeze and expand_dims.
tensor2tensor/layers/common_layers.py
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
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
[ "Make", "x", "n", "-", "d", "with", "squeeze", "and", "expand_dims", "." ]
tensorflow/tensor2tensor
python
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/layers/common_layers.py#L267-L275
[ "def", "expand_squeeze_to_nd", "(", "x", ",", "n", ",", "squeeze_dim", "=", "2", ",", "expand_dim", "=", "-", "1", ")", ":", "if", "len", "(", "x", ".", "shape", ")", ">", "n", ":", "while", "len", "(", "x", ".", "shape", ")", "!=", "n", ":", ...
272500b6efe353aeb638d2745ed56e519462ca31
train
standardize_images
Image standardization on batches and videos.
tensor2tensor/layers/common_layers.py
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)
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)
[ "Image", "standardization", "on", "batches", "and", "videos", "." ]
tensorflow/tensor2tensor
python
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/layers/common_layers.py#L278-L288
[ "def", "standardize_images", "(", "x", ")", ":", "with", "tf", ".", "name_scope", "(", "\"standardize_images\"", ",", "values", "=", "[", "x", "]", ")", ":", "x_shape", "=", "shape_list", "(", "x", ")", "x", "=", "to_float", "(", "tf", ".", "reshape", ...
272500b6efe353aeb638d2745ed56e519462ca31
train
flatten4d3d
Flatten a 4d-tensor into a 3d-tensor by joining width and height.
tensor2tensor/layers/common_layers.py
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
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
[ "Flatten", "a", "4d", "-", "tensor", "into", "a", "3d", "-", "tensor", "by", "joining", "width", "and", "height", "." ]
tensorflow/tensor2tensor
python
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/layers/common_layers.py#L291-L295
[ "def", "flatten4d3d", "(", "x", ")", ":", "xshape", "=", "shape_list", "(", "x", ")", "result", "=", "tf", ".", "reshape", "(", "x", ",", "[", "xshape", "[", "0", "]", ",", "xshape", "[", "1", "]", "*", "xshape", "[", "2", "]", ",", "xshape", ...
272500b6efe353aeb638d2745ed56e519462ca31
train
gather
Version of tf.gather that works faster on tpu.
tensor2tensor/layers/common_layers.py
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
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
[ "Version", "of", "tf", ".", "gather", "that", "works", "faster", "on", "tpu", "." ]
tensorflow/tensor2tensor
python
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/layers/common_layers.py#L299-L307
[ "def", "gather", "(", "params", ",", "indices", ",", "dtype", "=", "tf", ".", "float32", ")", ":", "if", "not", "is_xla_compiled", "(", ")", ":", "return", "tf", ".", "gather", "(", "params", ",", "indices", ")", "vocab_size", "=", "params", ".", "ge...
272500b6efe353aeb638d2745ed56e519462ca31
train
cumsum
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.
tensor2tensor/layers/common_layers.py
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
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
[ "TPU", "hack", "for", "tf", ".", "cumsum", "." ]
tensorflow/tensor2tensor
python
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/layers/common_layers.py#L311-L340
[ "def", "cumsum", "(", "x", ",", "axis", "=", "0", ",", "exclusive", "=", "False", ")", ":", "if", "not", "is_xla_compiled", "(", ")", ":", "return", "tf", ".", "cumsum", "(", "x", ",", "axis", "=", "axis", ",", "exclusive", "=", "exclusive", ")", ...
272500b6efe353aeb638d2745ed56e519462ca31
train
dropout_no_scaling
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.
tensor2tensor/layers/common_layers.py
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)
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)
[ "Like", "tf", ".", "nn", ".", "dropout", "but", "does", "not", "scale", "up", ".", "Works", "on", "integers", "also", "." ]
tensorflow/tensor2tensor
python
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/layers/common_layers.py#L343-L356
[ "def", "dropout_no_scaling", "(", "x", ",", "keep_prob", ")", ":", "if", "keep_prob", "==", "1.0", ":", "return", "x", "mask", "=", "tf", ".", "less", "(", "tf", ".", "random_uniform", "(", "tf", ".", "shape", "(", "x", ")", ")", ",", "keep_prob", ...
272500b6efe353aeb638d2745ed56e519462ca31
train
embedding
Embed x of type int64 into dense vectors, reducing to max 4 dimensions.
tensor2tensor/layers/common_layers.py
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)
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)
[ "Embed", "x", "of", "type", "int64", "into", "dense", "vectors", "reducing", "to", "max", "4", "dimensions", "." ]
tensorflow/tensor2tensor
python
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/layers/common_layers.py#L359-L387
[ "def", "embedding", "(", "x", ",", "vocab_size", ",", "dense_size", ",", "name", "=", "None", ",", "reuse", "=", "None", ",", "multiplier", "=", "1.0", ",", "symbol_dropout_rate", "=", "0.0", ",", "embedding_var", "=", "None", ",", "dtype", "=", "tf", ...
272500b6efe353aeb638d2745ed56e519462ca31
train
shift_right
Shift the second dimension of x right by one.
tensor2tensor/layers/common_layers.py
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
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", "." ]
tensorflow/tensor2tensor
python
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/layers/common_layers.py#L390-L396
[ "def", "shift_right", "(", "x", ",", "pad_value", "=", "None", ")", ":", "if", "pad_value", "is", "None", ":", "shifted_targets", "=", "tf", ".", "pad", "(", "x", ",", "[", "[", "0", ",", "0", "]", ",", "[", "1", ",", "0", "]", ",", "[", "0",...
272500b6efe353aeb638d2745ed56e519462ca31
train
shift_right_3d
Shift the second dimension of x right by one.
tensor2tensor/layers/common_layers.py
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
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", "." ]
tensorflow/tensor2tensor
python
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/layers/common_layers.py#L399-L405
[ "def", "shift_right_3d", "(", "x", ",", "pad_value", "=", "None", ")", ":", "if", "pad_value", "is", "None", ":", "shifted_targets", "=", "tf", ".", "pad", "(", "x", ",", "[", "[", "0", ",", "0", "]", ",", "[", "1", ",", "0", "]", ",", "[", "...
272500b6efe353aeb638d2745ed56e519462ca31
train
shift_right_2d
Shift the second dimension of x right by one.
tensor2tensor/layers/common_layers.py
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
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
[ "Shift", "the", "second", "dimension", "of", "x", "right", "by", "one", "." ]
tensorflow/tensor2tensor
python
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/layers/common_layers.py#L408-L414
[ "def", "shift_right_2d", "(", "x", ",", "pad_value", "=", "None", ")", ":", "if", "pad_value", "is", "None", ":", "shifted_targets", "=", "tf", ".", "pad", "(", "x", ",", "[", "[", "0", ",", "0", "]", ",", "[", "1", ",", "0", "]", "]", ")", "...
272500b6efe353aeb638d2745ed56e519462ca31
train
conv_stride2_multistep
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]`
tensor2tensor/layers/common_layers.py
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
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", "strided", "convolution", "to", "downsample", "x", "by", "2", "nbr_steps", "times", "." ]
tensorflow/tensor2tensor
python
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/layers/common_layers.py#L417-L450
[ "def", "conv_stride2_multistep", "(", "x", ",", "nbr_steps", ",", "output_filters", ",", "name", "=", "None", ",", "reuse", "=", "None", ")", ":", "with", "tf", ".", "variable_scope", "(", "name", ",", "default_name", "=", "\"conv_stride2_multistep\"", ",", ...
272500b6efe353aeb638d2745ed56e519462ca31
train
deconv_stride2_multistep
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]`
tensor2tensor/layers/common_layers.py
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
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
[ "Use", "a", "deconvolution", "to", "upsample", "x", "by", "2", "**", "nbr_steps", "." ]
tensorflow/tensor2tensor
python
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/layers/common_layers.py#L453-L513
[ "def", "deconv_stride2_multistep", "(", "x", ",", "nbr_steps", ",", "output_filters", ",", "name", "=", "None", ",", "reuse", "=", "None", ")", ":", "with", "tf", ".", "variable_scope", "(", "name", ",", "default_name", "=", "\"deconv_stride2_multistep\"", ","...
272500b6efe353aeb638d2745ed56e519462ca31
train
conv_internal
Conditional conv_fn making kernel 1d or 2d depending on inputs shape.
tensor2tensor/layers/common_layers.py
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")
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")
[ "Conditional", "conv_fn", "making", "kernel", "1d", "or", "2d", "depending", "on", "inputs", "shape", "." ]
tensorflow/tensor2tensor
python
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/layers/common_layers.py#L516-L551
[ "def", "conv_internal", "(", "conv_fn", ",", "inputs", ",", "filters", ",", "kernel_size", ",", "*", "*", "kwargs", ")", ":", "static_shape", "=", "inputs", ".", "get_shape", "(", ")", "if", "not", "static_shape", "or", "len", "(", "static_shape", ")", "...
272500b6efe353aeb638d2745ed56e519462ca31
train
subseparable_conv
Sub-separable convolution. If separability == 0 it's a separable_conv.
tensor2tensor/layers/common_layers.py
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)
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)
[ "Sub", "-", "separable", "convolution", ".", "If", "separability", "==", "0", "it", "s", "a", "separable_conv", "." ]
tensorflow/tensor2tensor
python
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/layers/common_layers.py#L579-L614
[ "def", "subseparable_conv", "(", "inputs", ",", "filters", ",", "kernel_size", ",", "*", "*", "kwargs", ")", ":", "def", "conv_fn", "(", "inputs", ",", "filters", ",", "kernel_size", ",", "*", "*", "kwargs", ")", ":", "\"\"\"Sub-separable convolution, splits i...
272500b6efe353aeb638d2745ed56e519462ca31
train
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].
tensor2tensor/layers/common_layers.py
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
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
[ "Version", "of", "conv1d", "that", "works", "on", "TPU", "(", "as", "of", "11", "/", "2017", ")", "." ]
tensorflow/tensor2tensor
python
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/layers/common_layers.py#L617-L648
[ "def", "tpu_conv1d", "(", "inputs", ",", "filters", ",", "kernel_size", ",", "padding", "=", "\"SAME\"", ",", "name", "=", "\"tpu_conv1d\"", ")", ":", "if", "kernel_size", "==", "1", ":", "return", "dense", "(", "inputs", ",", "filters", ",", "name", "="...
272500b6efe353aeb638d2745ed56e519462ca31