本文整理汇总了Python中tensorflow.compat.v1.gather方法的典型用法代码示例。如果您正苦于以下问题:Python v1.gather方法的具体用法?Python v1.gather怎么用?Python v1.gather使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类tensorflow.compat.v1
的用法示例。
在下文中一共展示了v1.gather方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: simulate
# 需要导入模块: from tensorflow.compat import v1 [as 别名]
# 或者: from tensorflow.compat.v1 import gather [as 别名]
def simulate(self, action):
reward, done = self._batch_env.simulate(action)
with tf.control_dependencies([reward, done]):
new_observ = tf.expand_dims(self._batch_env.observ, axis=1)
# If we shouldn't stack, i.e. self.history == 1, then just assign
# new_observ to self._observ and return from here.
if self.history == 1:
with tf.control_dependencies([self._observ.assign(new_observ)]):
return tf.identity(reward), tf.identity(done)
# If we should stack, then do the required work.
old_observ = tf.gather(
self._observ.read_value(),
list(range(1, self.history)),
axis=1)
with tf.control_dependencies([new_observ, old_observ]):
with tf.control_dependencies([self._observ.assign(
tf.concat([old_observ, new_observ], axis=1))]):
return tf.identity(reward), tf.identity(done)
示例2: __init__
# 需要导入模块: from tensorflow.compat import v1 [as 别名]
# 或者: from tensorflow.compat.v1 import gather [as 别名]
def __init__(self, num_experts, gates):
"""Create a SparseDispatcher.
Args:
num_experts: an integer.
gates: a `Tensor` of shape `[batch_size, num_experts]`.
Returns:
a SparseDispatcher
"""
self._gates = gates
self._num_experts = num_experts
where = tf.to_int32(tf.where(tf.transpose(gates) > 0))
self._expert_index, self._batch_index = tf.unstack(where, num=2, axis=1)
self._part_sizes_tensor = tf.reduce_sum(tf.to_int32(gates > 0), [0])
self._nonzero_gates = tf.gather(
tf.reshape(self._gates, [-1]),
self._batch_index * num_experts + self._expert_index)
示例3: shuffle_layer
# 需要导入模块: from tensorflow.compat import v1 [as 别名]
# 或者: from tensorflow.compat.v1 import gather [as 别名]
def shuffle_layer(inputs, shuffle_fn=rol):
"""Shuffles the elements according to bitwise left or right rotation.
Args:
inputs: Tensor input from previous layer
shuffle_fn: Shift function rol or ror
Returns:
tf.Tensor: Inputs shifted according to shuffle_fn
"""
length = tf.shape(inputs)[1]
n_bits = tf.log(tf.cast(length - 1, tf.float32)) / tf.log(2.0)
n_bits = tf.cast(n_bits, tf.int32) + 1
indices = tf.range(0, length)
rev_indices = shuffle_fn(indices, n_bits)
return tf.gather(inputs, rev_indices, axis=1)
示例4: post_attention
# 需要导入模块: from tensorflow.compat import v1 [as 别名]
# 或者: from tensorflow.compat.v1 import gather [as 别名]
def post_attention(self, token, x):
"""Called after self-attention. The memory can be updated here.
Args:
token: Data returned by pre_attention, which can be used to carry over
state related to the current memory operation.
x: a Tensor of data after self-attention and feed-forward
Returns:
a (possibly modified) version of the input x
"""
with tf.variable_scope(self.name + "/post_attention", reuse=tf.AUTO_REUSE):
depth = common_layers.shape_list(x)[-1]
actual_batch_size = common_layers.shape_list(x)[0]
memory_output = tf.gather(token["retrieved_mem"],
tf.range(actual_batch_size))
output = tf.add(tf.layers.dense(x, depth, use_bias=False),
tf.layers.dense(memory_output, depth))
with tf.control_dependencies([output]):
with tf.control_dependencies([
self.write(token["x"], token["access_logits"])]):
return tf.identity(output)
示例5: _compute_auxiliary_structure
# 需要导入模块: from tensorflow.compat import v1 [as 别名]
# 或者: from tensorflow.compat.v1 import gather [as 别名]
def _compute_auxiliary_structure(self, contents_and_mask):
"""Compute segment and position metadata."""
contents = contents_and_mask[:, :self._num_sequences]
start_mask = tf.cast(contents_and_mask[:, self._num_sequences:],
dtype=INDEX_DTYPE)
segment = tf.cumsum(start_mask, axis=0)
uniform_count = tf.ones_like(segment[:, 0])
position = []
for i in range(self._num_sequences):
segment_slice = segment[:, i]
counts = tf.math.segment_sum(uniform_count, segment[:, i])
position.append(tf.range(self._packed_length) - tf.cumsum(
tf.gather(counts, segment_slice - 1) * start_mask[:, i]))
position = tf.concat([i[:, tf.newaxis] for i in position], axis=1)
# Correct for padding tokens.
pad_mask = tf.cast(tf.not_equal(contents, 0), dtype=INDEX_DTYPE)
segment *= pad_mask
position *= pad_mask
return segment, position
示例6: testGather
# 需要导入模块: from tensorflow.compat import v1 [as 别名]
# 或者: from tensorflow.compat.v1 import gather [as 别名]
def testGather(self):
gather_index = [5, 6, 7, 8, 9, 0, 1, 2, 3, 4]
with arg_scope(self._batch_norm_scope()):
inputs = tf.zeros([2, 4, 4, 3])
c1 = layers.conv2d(inputs, num_outputs=10, kernel_size=3, scope='conv1')
gather = tf.gather(c1, gather_index, axis=3)
manager = orm.OpRegularizerManager(
[gather.op], self._default_op_handler_dict)
c1_reg = manager.get_regularizer(_get_op('conv1/Conv2D'))
gather_reg = manager.get_regularizer(_get_op('GatherV2'))
# Check regularizer indices.
self.assertAllEqual(list(range(10)), c1_reg.regularization_vector)
# This fails due to gather not being supported. Once gather is supported,
# this test can be enabled to verify that the regularization vector is
# gathered in the same ordering as the tensor.
# self.assertAllEqual(
# gather_index, gather_reg.regularization_vector)
# This test shows that gather is not supported. The regularization vector
# has the same initial ordering after the gather op scrambled the
# channels. Remove this once gather is supported.
self.assertAllEqual(list(range(10)), gather_reg.regularization_vector)
示例7: _build_classification_specification
# 需要导入模块: from tensorflow.compat import v1 [as 别名]
# 或者: from tensorflow.compat.v1 import gather [as 别名]
def _build_classification_specification(label, num_classes, collapse):
"""Returns a LinearSpecification for adversarial classification."""
# Pre-construct the specifications of the different classes.
eye = np.eye(num_classes - 1)
specifications = []
for i in range(num_classes):
specifications.append(np.concatenate(
[eye[:, :i], -np.ones((num_classes - 1, 1)), eye[:, i:]], axis=1))
specifications = np.array(specifications, dtype=np.float32)
specifications = tf.constant(specifications)
# We can then use gather.
c = tf.gather(specifications, label)
# By construction all specifications are relevant.
d = tf.zeros(shape=(tf.shape(label)[0], num_classes - 1))
return ibp.LinearSpecification(c, d, prune_irrelevant=False,
collapse=collapse)
示例8: targeted_objective
# 需要导入模块: from tensorflow.compat import v1 [as 别名]
# 或者: from tensorflow.compat.v1 import gather [as 别名]
def targeted_objective(final_w, final_b, labels):
"""Determines final layer weights for attacks targeting each class.
Args:
final_w: 2D tensor of shape (last_hidden_layer_size, num_classes)
containing the weights for the final linear layer.
final_b: 1D tensor of shape (num_classes) containing the biases for the
final hidden layer.
labels: 1D integer tensor of shape (batch_size) of labels for each
input example.
Returns:
obj_w: Tensor of shape (num_classes, batch_size, last_hidden_layer_size)
containing weights (to use in place of final linear layer weights)
for targeted attacks.
obj_b: Tensor of shape (num_classes, batch_size) containing bias
(to use in place of final linear layer biases) for targeted attacks.
"""
# Elide objective with final linear layer.
final_wt = tf.transpose(final_w)
obj_w = tf.expand_dims(final_wt, axis=1) - tf.gather(final_wt, labels, axis=0)
obj_b = tf.expand_dims(final_b, axis=1) - tf.gather(final_b, labels, axis=0)
return obj_w, obj_b
示例9: slice
# 需要导入模块: from tensorflow.compat import v1 [as 别名]
# 或者: from tensorflow.compat.v1 import gather [as 别名]
def slice(self, tf_tensor, tensor_shape):
""""Slice out the corresponding part of tensor given the pnum variable."""
tensor_layout = self.tensor_layout(tensor_shape)
if tensor_layout.is_fully_replicated:
return self.LaidOutTensor([tf_tensor])
else:
slice_shape = self.slice_shape(tensor_shape)
slice_begins = [
self.slice_begin(tensor_shape, pnum) for pnum in xrange(self.size)
]
slice_begins_tensor = tf.stack(slice_begins)
# slice on source device
selected_slice_begin = tf.gather(slice_begins_tensor, self.pnum_tensor)
return self.LaidOutTensor(
[tf.slice(tf_tensor, selected_slice_begin, slice_shape)])
示例10: entity_emb
# 需要导入模块: from tensorflow.compat import v1 [as 别名]
# 或者: from tensorflow.compat.v1 import gather [as 别名]
def entity_emb(entity_ind, entity_word_ids, entity_word_masks, word_emb_table,
word_weights):
"""Get BOW embeddings for entities."""
# [NNZ, max_entity_len]
batch_entity_word_ids = tf.gather(entity_word_ids, entity_ind)
batch_entity_word_masks = tf.gather(entity_word_masks, entity_ind)
# [NNZ, max_entity_len, dim]
batch_entity_word_emb = tf.gather(word_emb_table, batch_entity_word_ids)
# [NNZ, max_entity_len, 1]
batch_entity_word_weights = tf.gather(word_weights, batch_entity_word_ids)
# [NNZ, dim]
batch_entity_emb = tf.reduce_sum(
batch_entity_word_emb * batch_entity_word_weights *
tf.expand_dims(batch_entity_word_masks, 2),
axis=1)
return batch_entity_emb
示例11: compute_probs_tf
# 需要导入模块: from tensorflow.compat import v1 [as 别名]
# 或者: from tensorflow.compat.v1 import gather [as 别名]
def compute_probs_tf(slate, scores_tf, score_no_click_tf):
"""Computes the selection probability and returns selected index.
This assumes scores are normalizable, e.g., scores cannot be negative.
Args:
slate: a list of integers that represents the video slate.
scores_tf: a float tensor that stores the scores of all documents.
score_no_click_tf: a float tensor that represents the score for the action
of picking no document.
Returns:
A float tensor that represents the probabilities of selecting each document
in the slate.
"""
all_scores = tf.concat([
tf.gather(scores_tf, slate),
tf.reshape(score_no_click_tf, (1, 1))
], axis=0) # pyformat: disable
all_probs = all_scores / tf.reduce_sum(input_tensor=all_scores)
return all_probs[:-1]
示例12: permute_noise_tokens
# 需要导入模块: from tensorflow.compat import v1 [as 别名]
# 或者: from tensorflow.compat.v1 import gather [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)
示例13: _expansion_box_field_labels
# 需要导入模块: from tensorflow.compat import v1 [as 别名]
# 或者: from tensorflow.compat.v1 import gather [as 别名]
def _expansion_box_field_labels(self,
object_classes,
object_field,
copy_class_id=False):
"""Expand the labels of a specific object field according to the hierarchy.
Args:
object_classes: Int64 tensor with the class id for each element in
object_field.
object_field: Tensor to be expanded.
copy_class_id: Boolean to choose whether to use class id values in the
output tensor instead of replicating the original values.
Returns:
A tensor with the result of expanding object_field.
"""
expanded_indices = tf.gather(
self._ancestors_lut, object_classes - _LABEL_OFFSET, axis=0)
if copy_class_id:
new_object_field = tf.where(expanded_indices > 0)[:, 1] + _LABEL_OFFSET
else:
new_object_field = tf.repeat(
object_field, tf.reduce_sum(expanded_indices, axis=1), axis=0)
return new_object_field
示例14: matmul_gather_on_zeroth_axis
# 需要导入模块: from tensorflow.compat import v1 [as 别名]
# 或者: from tensorflow.compat.v1 import gather [as 别名]
def matmul_gather_on_zeroth_axis(params, indices, scope=None):
"""Matrix multiplication based implementation of tf.gather on zeroth axis.
TODO(rathodv, jonathanhuang): enable sparse matmul option.
Args:
params: A float32 Tensor. The tensor from which to gather values.
Must be at least rank 1.
indices: A Tensor. Must be one of the following types: int32, int64.
Must be in range [0, params.shape[0])
scope: A name for the operation (optional).
Returns:
A Tensor. Has the same type as params. Values from params gathered
from indices given by indices, with shape indices.shape + params.shape[1:].
"""
with tf.name_scope(scope, 'MatMulGather'):
params_shape = shape_utils.combined_static_and_dynamic_shape(params)
indices_shape = shape_utils.combined_static_and_dynamic_shape(indices)
params2d = tf.reshape(params, [params_shape[0], -1])
indicator_matrix = tf.one_hot(indices, params_shape[0])
gathered_result_flattened = tf.matmul(indicator_matrix, params2d)
return tf.reshape(gathered_result_flattened,
tf.stack(indices_shape + params_shape[1:]))
示例15: clip_tensor
# 需要导入模块: from tensorflow.compat import v1 [as 别名]
# 或者: from tensorflow.compat.v1 import gather [as 别名]
def clip_tensor(t, length):
"""Clips the input tensor along the first dimension up to the length.
Args:
t: the input tensor, assuming the rank is at least 1.
length: a tensor of shape [1] or an integer, indicating the first dimension
of the input tensor t after clipping, assuming length <= t.shape[0].
Returns:
clipped_t: the clipped tensor, whose first dimension is length. If the
length is an integer, the first dimension of clipped_t is set to length
statically.
"""
clipped_t = tf.gather(t, tf.range(length))
if not _is_tensor(length):
clipped_t = _set_dim_0(clipped_t, length)
return clipped_t