本文整理汇总了Python中tensorflow.compat.v1.boolean_mask方法的典型用法代码示例。如果您正苦于以下问题:Python v1.boolean_mask方法的具体用法?Python v1.boolean_mask怎么用?Python v1.boolean_mask使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类tensorflow.compat.v1
的用法示例。
在下文中一共展示了v1.boolean_mask方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_actnorm
# 需要导入模块: from tensorflow.compat import v1 [as 别名]
# 或者: from tensorflow.compat.v1 import boolean_mask [as 别名]
def test_actnorm(self):
_, x_mask, _ = self.get_data()
x = tf.random_normal((BATCH_SIZE, TARGET_LENGTH, N_CHANNELS),
mean=50.0, stddev=10.0, dtype=DTYPE)
x_act, logabsdet = glow.actnorm(
"actnorm", x, x_mask, inverse=False, init=True)
x_act_nopad = tf.boolean_mask(x_act, x_mask)
x_mean, x_var = tf.nn.moments(x_act_nopad, axes=[0])
self.evaluate(tf.global_variables_initializer())
x, x_act, logabsdet, x_mean, x_var = (
self.evaluate([x, x_act, logabsdet, x_mean, x_var]))
self.assertEqual(x_act.shape, (BATCH_SIZE, TARGET_LENGTH, N_CHANNELS))
self.assertEqual(logabsdet.shape, (BATCH_SIZE,))
self.assertTrue(np.allclose(x_mean, 0.0, atol=1e-5))
self.assertTrue(np.allclose(x_var, 1.0, atol=1e-5))
示例2: __init__
# 需要导入模块: from tensorflow.compat import v1 [as 别名]
# 或者: from tensorflow.compat.v1 import boolean_mask [as 别名]
def __init__(self, c, d=None, prune_irrelevant=True, collapse=True):
"""Builds a linear specification module."""
super(LinearSpecification, self).__init__(name='specs', collapse=collapse)
# c has shape [batch_size, num_specifications, num_outputs]
# d has shape [batch_size, num_specifications]
# Some specifications may be irrelevant (not a function of the output).
# We automatically remove them for clarity. We expect the number of
# irrelevant specs to be equal for all elements of a batch.
# Shape is [batch_size, num_specifications]
if prune_irrelevant:
irrelevant = tf.equal(tf.reduce_sum(
tf.cast(tf.abs(c) > 1e-6, tf.int32), axis=-1, keepdims=True), 0)
batch_size = tf.shape(c)[0]
num_outputs = tf.shape(c)[2]
irrelevant = tf.tile(irrelevant, [1, 1, num_outputs])
self._c = tf.reshape(
tf.boolean_mask(c, tf.logical_not(irrelevant)),
[batch_size, -1, num_outputs])
else:
self._c = c
self._d = d
示例3: sparse_reduce
# 需要导入模块: from tensorflow.compat import v1 [as 别名]
# 或者: from tensorflow.compat.v1 import boolean_mask [as 别名]
def sparse_reduce(sp_tensor, rank, agg_fn="sum", axis=-1):
"""Reduce SparseTensor along the given axis.
Args:
sp_tensor: SparseTensor of arbitrary rank.
rank: Integer rank of the sparse tensor.
agg_fn: Reduce function for aggregation.
axis: Integer specifying axis to sum over.
Returns:
sp_tensor: SparseTensor of one less rank.
"""
if axis < 0:
axis = rank + axis
axes_to_keep = tf.one_hot(
axis, rank, on_value=False, off_value=True, dtype=tf.bool)
indices_to_keep = tf.boolean_mask(sp_tensor.indices, axes_to_keep, axis=1)
new_shape = tf.boolean_mask(sp_tensor.dense_shape, axes_to_keep)
indices_to_keep.set_shape([None, rank - 1])
indices, values = aggregate_sparse_indices(
indices_to_keep, sp_tensor.values, new_shape, agg_fn=agg_fn)
return tf.sparse.reorder(tf.SparseTensor(indices, values, new_shape))
示例4: noise_span_to_sentinel
# 需要导入模块: from tensorflow.compat import v1 [as 别名]
# 或者: from tensorflow.compat.v1 import boolean_mask [as 别名]
def noise_span_to_sentinel(tokens, noise_mask, vocabulary):
"""Replace each run of consecutive noise tokens with a single sentinel.
Args:
tokens: a 1d integer Tensor
noise_mask: a boolean Tensor with the same shape as tokens
vocabulary: a vocabulary.Vocabulary
Returns:
a Tensor with the same shape and dtype as tokens
"""
tokens = tf.where_v2(noise_mask,
tf.cast(sentinel_id(vocabulary), tokens.dtype),
tokens)
prev_token_is_noise = tf.pad(noise_mask[:-1], [[1, 0]])
subsequent_noise_tokens = tf.logical_and(noise_mask, prev_token_is_noise)
return tf.boolean_mask(tokens, tf.logical_not(subsequent_noise_tokens))
示例5: permute_noise_tokens
# 需要导入模块: from tensorflow.compat import v1 [as 别名]
# 或者: from tensorflow.compat.v1 import boolean_mask [as 别名]
def permute_noise_tokens(tokens, noise_mask, unused_vocabulary):
"""Permute the noise tokens, keeping the non-noise tokens where they are.
Args:
tokens: a 1d integer Tensor
noise_mask: a boolean Tensor with the same shape as tokens
unused_vocabulary: a vocabulary.Vocabulary
Returns:
a Tensor with the same shape and dtype as tokens
"""
masked_only = tf.boolean_mask(tokens, noise_mask)
permuted = tf.random.shuffle(masked_only)
# pad to avoid errors when it has size 0
permuted = tf.pad(permuted, [[0, 1]])
indices = tf.cumsum(tf.cast(noise_mask, tf.int32), exclusive=True)
return tf.where_v2(noise_mask,
tf.gather(permuted, indices),
tokens)
示例6: _get_refined_encodings_for_postitive_class
# 需要导入模块: from tensorflow.compat import v1 [as 别名]
# 或者: from tensorflow.compat.v1 import boolean_mask [as 别名]
def _get_refined_encodings_for_postitive_class(
self, refined_box_encodings, flat_cls_targets_with_background,
batch_size):
# We only predict refined location encodings for the non background
# classes, but we now pad it to make it compatible with the class
# predictions
refined_box_encodings_with_background = tf.pad(refined_box_encodings,
[[0, 0], [1, 0], [0, 0]])
refined_box_encodings_masked_by_class_targets = (
box_list_ops.boolean_mask(
box_list.BoxList(
tf.reshape(refined_box_encodings_with_background,
[-1, self._box_coder.code_size])),
tf.reshape(tf.greater(flat_cls_targets_with_background, 0), [-1]),
use_static_shapes=self._use_static_shapes,
indicator_sum=batch_size * self.max_num_proposals
if self._use_static_shapes else None).get())
return tf.reshape(
refined_box_encodings_masked_by_class_targets, [
batch_size, self.max_num_proposals,
self._box_coder.code_size
])
示例7: additive_coupling
# 需要导入模块: from tensorflow.compat import v1 [as 别名]
# 或者: from tensorflow.compat.v1 import boolean_mask [as 别名]
def additive_coupling(
name, x, x_mask, inverse, split_dim, identity_first, init,
decoder_self_attention_bias=None, **kwargs):
"""Additive coupling transform layer."""
hparams = kwargs["hparams"]
batch_size, length, n_channels = common_layers.shape_list(x)
assert hparams.scale_width > 0.0 and hparams.scale_width < 1.0
with tf.variable_scope(name, reuse=tf.AUTO_REUSE):
x_id, x_tr, _, n_transform, bias, mask = gops.split_coupling(
x, x_mask, split_dim, identity_first, decoder_self_attention_bias)
z_id = x_id
loc = gops.transformer_decoder_block(
"theta_tr",
n_layers=hparams.n_layers_transform_params,
x=x_id,
x_mask=mask,
output_size=n_transform,
init=init,
decoder_self_attention_bias=bias,
**kwargs)
if not inverse:
z_tr = x_tr + loc
else:
z_tr = x_tr - loc
logabsdet = tf.constant(0.0, dtype=tf.float32)
tf.summary.histogram("_loc", tf.boolean_mask(loc, mask))
result = gops.join_coupling(z_id, z_tr, split_dim, identity_first)
result = tf.reshape(result, [batch_size, length, n_channels])
return result, logabsdet
示例8: test_dense_weightnorm
# 需要导入模块: from tensorflow.compat import v1 [as 别名]
# 或者: from tensorflow.compat.v1 import boolean_mask [as 别名]
def test_dense_weightnorm(self):
x, x_mask = self.get_data()
x = tf.random_normal((BATCH_SIZE, TARGET_LENGTH, HIDDEN_SIZE),
mean=0.0, stddev=1.0)
y = gops.dense_weightnorm("wn", x, N_CHANNELS, x_mask,
init_scale=1.0, init=True)
y_nopad = tf.boolean_mask(y, x_mask)
mean, var = tf.nn.moments(y_nopad, axes=[0])
self.evaluate(tf.global_variables_initializer())
x, x_mask, y, y_nopad, mean, var = (
self.evaluate([x, x_mask, y, y_nopad, mean, var]))
self.assertEqual(y.shape, (BATCH_SIZE, TARGET_LENGTH, N_CHANNELS))
self.assertTrue(np.allclose(mean, 0.0, atol=1e-5))
self.assertTrue(np.allclose(var, 1.0, atol=1e-5))
示例9: test_empty_x_results_in_empty_output
# 需要导入模块: from tensorflow.compat import v1 [as 别名]
# 或者: from tensorflow.compat.v1 import boolean_mask [as 别名]
def test_empty_x_results_in_empty_output(self):
# Empty x is common if someone masks their input with tf.boolean_mask in
# order to drop missing entries, and in a particular batch all entries are
# missing.
with self.cached_session():
x = np.array([]).reshape(0, 3)
self.assertEqual(0, array_ops.size(x).eval())
y = _layers.legacy_fully_connected(x, 2, activation_fn=nn_ops.softmax)
variables_lib.global_variables_initializer().run()
expected_y = np.array([]).reshape(0, 2)
np.testing.assert_array_equal(expected_y, y.eval())
示例10: select_slate_optimal
# 需要导入模块: from tensorflow.compat import v1 [as 别名]
# 或者: from tensorflow.compat.v1 import boolean_mask [as 别名]
def select_slate_optimal(slate_size, s_no_click, s, q):
"""Selects the slate using exhaustive search.
This algorithm corresponds to the method "OS" in
Ie et al. https://arxiv.org/abs/1905.12767.
Args:
slate_size: int, the size of the recommendation slate.
s_no_click: float tensor, the score for not clicking any document.
s: [num_of_documents] tensor, the scores for clicking documents.
q: [num_of_documents] tensor, the predicted q values for documents.
Returns:
[slate_size] tensor, the selected slate.
"""
num_candidates = s.shape.as_list()[0]
# Obtain all possible slates given current docs in the candidate set.
mesh_args = [list(range(num_candidates))] * slate_size
slates = tf.stack(tf.meshgrid(*mesh_args), axis=-1)
slates = tf.reshape(slates, shape=(-1, slate_size))
# Filter slates that include duplicates to ensure each document is picked
# at most once.
unique_mask = tf.map_fn(
lambda x: tf.equal(tf.size(input=x), tf.size(input=tf.unique(x)[0])),
slates,
dtype=tf.bool)
slates = tf.boolean_mask(tensor=slates, mask=unique_mask)
slate_q_values = tf.gather(s * q, slates)
slate_scores = tf.gather(s, slates)
slate_normalizer = tf.reduce_sum(
input_tensor=slate_scores, axis=1) + s_no_click
slate_q_values = slate_q_values / tf.expand_dims(slate_normalizer, 1)
slate_sum_q_values = tf.reduce_sum(input_tensor=slate_q_values, axis=1)
max_q_slate_index = tf.argmax(input=slate_sum_q_values)
return tf.gather(slates, max_q_slate_index, axis=0)
示例11: reduce_concat_tokens
# 需要导入模块: from tensorflow.compat import v1 [as 别名]
# 或者: from tensorflow.compat.v1 import boolean_mask [as 别名]
def reduce_concat_tokens(dataset,
feature_key='targets',
batch_size=128,
**unused_kwargs):
"""Token-preprocessor to concatenate multiple unrelated documents.
If we want to generate examples of exactly the right length,
(to avoid wasting space on padding), then we use this function, folowed by
split_tokens.
Args:
dataset: a tf.data.Dataset with dictionaries containing the key feature_key.
feature_key: an string
batch_size: an integer - how many documents to concatenate into one
Returns:
a dataset
"""
dataset = dataset.map(lambda x: {feature_key: x[feature_key]},
num_parallel_calls=tf.data.experimental.AUTOTUNE)
dataset = dataset.padded_batch(batch_size, padded_shapes={feature_key: [-1]})
def _my_fn(x):
tokens = tf.reshape(x[feature_key], [-1])
# strip padding
tokens = tf.boolean_mask(tokens, tf.cast(tokens, tf.bool))
return {feature_key: tokens}
return dataset.map(_my_fn, num_parallel_calls=tf.data.experimental.AUTOTUNE)
示例12: noise_span_to_unique_sentinel
# 需要导入模块: from tensorflow.compat import v1 [as 别名]
# 或者: from tensorflow.compat.v1 import boolean_mask [as 别名]
def noise_span_to_unique_sentinel(tokens, noise_mask, vocabulary):
"""Replace each run of consecutive noise tokens with a different sentinel.
The idea here is to be able to align the dropped spans in the inputs
with the markers in the targets.
We want to generate training examples like
"We hold X to be Y that" -> "X these truths Y self evident Z"
Sentinels assigned in decreasing order within the sequence starting at
vocabulary.size - 1. That is, we appropriate the last tokens in the
vocabulary for additional use as sentinels.
TODO(noam): we may want to try enlarging the vocabulary and leaving room
for the sentinels instead. However, this requires enlarging the embedding
tables in the model, so that is a bigger change.
Args:
tokens: a 1d integer Tensor
noise_mask: a boolean Tensor with the same shape as tokens
vocabulary: a vocabulary.Vocabulary
Returns:
a Tensor with the same shape and dtype as tokens
"""
vocab_size = vocabulary.vocab_size
prev_token_is_noise = tf.pad(noise_mask[:-1], [[1, 0]])
first_noise_tokens = tf.logical_and(
noise_mask, tf.logical_not(prev_token_is_noise))
subsequent_noise_tokens = tf.logical_and(noise_mask, prev_token_is_noise)
sentinel = vocab_size - tf.cumsum(tf.cast(first_noise_tokens, tokens.dtype))
tokens = tf.where_v2(first_noise_tokens, sentinel, tokens)
return tf.boolean_mask(tokens, tf.logical_not(subsequent_noise_tokens))
示例13: drop_noise_tokens
# 需要导入模块: from tensorflow.compat import v1 [as 别名]
# 或者: from tensorflow.compat.v1 import boolean_mask [as 别名]
def drop_noise_tokens(tokens, noise_mask, unused_vocabulary):
"""Drop noise tokens without inserting a sentinel.
Args:
tokens: a 1d integer Tensor
noise_mask: a boolean Tensor with the same shape as tokens
unused_vocabulary: a vocabulary.Vocabulary
Returns:
a Tensor with the same shape and dtype as tokens
"""
return tf.boolean_mask(tokens, tf.logical_not(noise_mask))
示例14: trim_zeros_graph
# 需要导入模块: from tensorflow.compat import v1 [as 别名]
# 或者: from tensorflow.compat.v1 import boolean_mask [as 别名]
def trim_zeros_graph(boxes, name='trim_zeros'):
"""Often boxes are represented with matrices of shape [N, 4] and
are padded with zeros. This removes zero boxes.
boxes: [N, 4] matrix of boxes.
non_zeros: [N] a 1D boolean mask identifying the rows to keep
"""
non_zeros = tf.cast(tf.reduce_sum(tf.abs(boxes), axis=1), tf.bool)
boxes = tf.boolean_mask(boxes, non_zeros, name=name)
return boxes, non_zeros
示例15: _slice_with_actions
# 需要导入模块: from tensorflow.compat import v1 [as 别名]
# 或者: from tensorflow.compat.v1 import boolean_mask [as 别名]
def _slice_with_actions(embeddings, actions):
"""Slice a Tensor.
Take embeddings of the form [batch_size, num_actions, embed_dim]
and actions of the form [batch_size, 1], and return the sliced embeddings
like embeddings[:, actions, :].
Args:
embeddings: Tensor of embeddings to index.
actions: int Tensor to use as index into embeddings
Returns:
Tensor of embeddings indexed by actions
"""
batch_size, num_actions = embeddings.get_shape()[:2]
# Values are the 'values' in a sparse tensor we will be setting
act_indx = tf.cast(actions, tf.int64)[:, None]
values = tf.reshape(tf.cast(tf.ones(tf.shape(actions)), tf.bool), [-1])
# Create a range for each index into the batch
act_range = tf.range(0, batch_size, dtype=tf.int64)[:, None]
# Combine this into coordinates with the action indices
indices = tf.concat([act_range, act_indx], 1)
actions_mask = tf.SparseTensor(indices, values, [batch_size, num_actions])
actions_mask = tf.stop_gradient(
tf.sparse_tensor_to_dense(actions_mask, default_value=False))
sliced_emb = tf.boolean_mask(embeddings, actions_mask)
return sliced_emb