本文整理汇总了Python中tensorflow.dynamic_stitch方法的典型用法代码示例。如果您正苦于以下问题:Python tensorflow.dynamic_stitch方法的具体用法?Python tensorflow.dynamic_stitch怎么用?Python tensorflow.dynamic_stitch使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类tensorflow
的用法示例。
在下文中一共展示了tensorflow.dynamic_stitch方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: scheduled_sample
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import dynamic_stitch [as 别名]
def scheduled_sample(ground_truth_x, generated_x, batch_size, num_ground_truth):
"""Sample batch with specified mix of ground truth and generated data points.
Args:
ground_truth_x: tensor of ground-truth data points.
generated_x: tensor of generated data points.
batch_size: batch size
num_ground_truth: number of ground-truth examples to include in batch.
Returns:
New batch with num_ground_truth sampled from ground_truth_x and the rest
from generated_x.
"""
idx = tf.random_shuffle(tf.range(int(batch_size)))
ground_truth_idx = tf.gather(idx, tf.range(num_ground_truth))
generated_idx = tf.gather(idx, tf.range(num_ground_truth, int(batch_size)))
ground_truth_examps = tf.gather(ground_truth_x, ground_truth_idx)
generated_examps = tf.gather(generated_x, generated_idx)
return tf.dynamic_stitch([ground_truth_idx, generated_idx],
[ground_truth_examps, generated_examps])
示例2: scheduled_sample
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import dynamic_stitch [as 别名]
def scheduled_sample(self,
ground_truth_x,
generated_x,
batch_size,
num_ground_truth):
"""Sample batch with specified mix of groundtruth and generated data points.
Args:
ground_truth_x: tensor of ground-truth data points.
generated_x: tensor of generated data points.
batch_size: batch size
num_ground_truth: number of ground-truth examples to include in batch.
Returns:
New batch with num_ground_truth sampled from ground_truth_x and the rest
from generated_x.
"""
idx = tf.random_shuffle(tf.range(batch_size))
ground_truth_idx = tf.gather(idx, tf.range(num_ground_truth))
generated_idx = tf.gather(idx, tf.range(num_ground_truth, batch_size))
ground_truth_examps = tf.gather(ground_truth_x, ground_truth_idx)
generated_examps = tf.gather(generated_x, generated_idx)
return tf.dynamic_stitch([ground_truth_idx, generated_idx],
[ground_truth_examps, generated_examps])
示例3: testSimpleTwoDimensional
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import dynamic_stitch [as 别名]
def testSimpleTwoDimensional(self):
with self.test_session():
indices = [tf.constant([0, 4, 7]),
tf.constant([1, 6]),
tf.constant([2, 3, 5])]
data = [tf.constant([[0, 1], [40, 41], [70, 71]]),
tf.constant([[10, 11], [60, 61]]),
tf.constant([[20, 21], [30, 31], [50, 51]])]
stitched_t = tf.dynamic_stitch(indices, data)
stitched_val = stitched_t.eval()
self.assertAllEqual(
[[0, 1], [10, 11], [20, 21], [30, 31],
[40, 41], [50, 51], [60, 61], [70, 71]], stitched_val)
# Dimension 0 is determined by the max index in indices, so we
# can only infer that the output is a matrix with 2 columns and
# some unknown number of rows.
self.assertEqual([None, 2], stitched_t.get_shape().as_list())
示例4: testHigherRank
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import dynamic_stitch [as 别名]
def testHigherRank(self):
with self.test_session() as sess:
indices = [tf.constant(6), tf.constant([4, 1]),
tf.constant([[5, 2], [0, 3]])]
data = [tf.constant([61, 62]), tf.constant([[41, 42], [11, 12]]),
tf.constant([[[51, 52], [21, 22]], [[1, 2], [31, 32]]])]
stitched_t = tf.dynamic_stitch(indices, data)
stitched_val = stitched_t.eval()
correct = 10 * np.arange(7)[:, None] + [1, 2]
self.assertAllEqual(correct, stitched_val)
self.assertEqual([None, 2], stitched_t.get_shape().as_list())
# Test gradients
stitched_grad = 7 * stitched_val
grads = tf.gradients(stitched_t, indices + data, stitched_grad)
self.assertEqual(grads[:3], [None] * 3) # Indices have no gradients
for datum, grad in zip(data, sess.run(grads[3:])):
self.assertAllEqual(7 * datum.eval(), grad)
示例5: split_apply_merge
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import dynamic_stitch [as 别名]
def split_apply_merge(inp, partitions, fns):
"""Split input according to partitions. Pass results through fns and merge.
Args:
inp: the input vector
partitions: tensor of same length as input vector, having values 0, 1
fns: the two functions.
Returns:
the vector routed, where routed[i] = fns[partitions[i]](inp[i])
"""
new_inputs = tf.dynamic_partition(inp, partitions, len(fns))
new_outputs = [fns[i](x) for i, x in enumerate(new_inputs)]
new_indices = tf.dynamic_partition(
tf.range(0, inp.get_shape()[0]), partitions, len(fns))
return tf.dynamic_stitch(new_indices, new_outputs)
示例6: _arrange_back_fn
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import dynamic_stitch [as 别名]
def _arrange_back_fn(list_tensor_1d_mask_1d):
"""Arranges back tensor_1d to restore original order
modified by `_rearrange_fn` according to mask_1d:
- number of 0s in mask_1d values on the left are set to
their corresponding places where mask_1d=0,
- number of 1s in mask_1d values on the right are set to
their corresponding places where mask_1d=1"""
tensor_1d, mask_1d = list_tensor_1d_mask_1d
mask_indices = tf.dynamic_partition(tf.range(tf.shape(tensor_1d)[0]),
mask_1d, 2)
mask_sum = tf.reduce_sum(mask_1d, axis=0)
partitioned_tensor = [tf.zeros_like(tensor_1d[:-mask_sum]),
tensor_1d[-mask_sum:]]
return tf.dynamic_stitch(mask_indices, partitioned_tensor)
示例7: _update_alloc_and_usage_vectors
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import dynamic_stitch [as 别名]
def _update_alloc_and_usage_vectors(self, pre_write_weightings, pre_read_weightings, pre_usage_vector, free_gates):
retention_vector = tf.reduce_prod(1 - free_gates * pre_read_weightings, axis=1, keepdims=False,
name='retention_prod')
usage_vector = (
pre_usage_vector + pre_write_weightings - pre_usage_vector * pre_write_weightings) * retention_vector
sorted_usage, free_list = tf.nn.top_k(-1 * usage_vector, self.h_N)
sorted_usage = -1 * sorted_usage
cumprod_sorted_usage = tf.cumprod(sorted_usage, axis=1, exclusive=True)
corrected_free_list = free_list + self.const_batch_memory_range
cumprod_sorted_usage_re = [tf.reshape(cumprod_sorted_usage, [-1, ]), ]
corrected_free_list_re = [tf.reshape(corrected_free_list, [-1]), ]
stitched_usage = tf.dynamic_stitch(corrected_free_list_re, cumprod_sorted_usage_re, name=None)
stitched_usage = tf.reshape(stitched_usage, [self.h_B, self.h_N])
alloc_weighting = (1 - usage_vector) * stitched_usage
return alloc_weighting, usage_vector
示例8: Combine
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import dynamic_stitch [as 别名]
def Combine(self, x_tensors):
"""Reshuffles per-expert `Tensor`s to produce per-datashard `Tensor`s.
Dispatch must have been called at least once first.
The dimensions of all input and output `Tensor`s match, except for
dimension 0. In dimension 0, the input `Tensor`s match the corresponding
outputs of `Dispatch`, and the output `Tensor`s match the corresponding
`gates` `Tensor`s which were passed to the constructor.
Args:
x_tensors: a list of `Tensor`s, one per expert.
Returns:
a list of `Tensor`s, one per datashard.
"""
parts = self._model_parallelism(tf.split, x_tensors,
self._part_sizes_by_expert)
d_tensors = self._data_parallelism(tf.dynamic_stitch, self._stitch_indices,
TransposeListOfLists(parts))
return d_tensors
示例9: scheduled_sample
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import dynamic_stitch [as 别名]
def scheduled_sample(ground_truth_x, generated_x, batch_size, num_ground_truth):
"""Sample batch with specified mix of ground truth and generated data_files points.
Args:
ground_truth_x: tensor of ground-truth data_files points.
generated_x: tensor of generated data_files points.
batch_size: batch size
num_ground_truth: number of ground-truth examples to include in batch.
Returns:
New batch with num_ground_truth sampled from ground_truth_x and the rest
from generated_x.
"""
idx = tf.random_shuffle(tf.range(int(batch_size)))
ground_truth_idx = tf.gather(idx, tf.range(num_ground_truth))
generated_idx = tf.gather(idx, tf.range(num_ground_truth, int(batch_size)))
ground_truth_examps = tf.gather(ground_truth_x, ground_truth_idx)
generated_examps = tf.gather(generated_x, generated_idx)
return tf.dynamic_stitch([ground_truth_idx, generated_idx],
[ground_truth_examps, generated_examps])
示例10: scheduled_sample
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import dynamic_stitch [as 别名]
def scheduled_sample(ground_truth_x, generated_x, batch_size, num_ground_truth):
"""Sample batch with specified mix of ground truth and generated data points.
Args:
ground_truth_x: tensor of ground-truth data points.
generated_x: tensor of generated data points.
batch_size: batch size
num_ground_truth: number of ground-truth examples to include in batch.
Returns:
New batch with num_ground_truth sampled from ground_truth_x and the rest
from generated_x.
"""
idx = tf.random_shuffle(tf.range(int(batch_size)))
ground_truth_idx = tf.gather(idx, tf.range(num_ground_truth))
generated_idx = tf.gather(idx, tf.range(num_ground_truth, int(batch_size)))
ground_truth_examps = tf.gather(ground_truth_x, ground_truth_idx)
generated_examps = tf.gather(generated_x, generated_idx)
return tf.dynamic_stitch([ground_truth_idx, generated_idx],
[ground_truth_examps, generated_examps])
示例11: indices_to_dense_vector
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import dynamic_stitch [as 别名]
def indices_to_dense_vector(indices,
size,
indices_value=1.,
default_value=0,
dtype=tf.float32):
"""Creates dense vector with indices set to specific value and rest to zeros.
This function exists because it is unclear if it is safe to use
tf.sparse_to_dense(indices, [size], 1, validate_indices=False)
with indices which are not ordered.
This function accepts a dynamic size (e.g. tf.shape(tensor)[0])
Args:
indices: 1d Tensor with integer indices which are to be set to
indices_values.
size: scalar with size (integer) of output Tensor.
indices_value: values of elements specified by indices in the output vector
default_value: values of other elements in the output vector.
dtype: data type.
Returns:
dense 1D Tensor of shape [size] with indices set to indices_values and the
rest set to default_value.
"""
size = tf.to_int32(size)
zeros = tf.ones([size], dtype=dtype) * default_value
values = tf.ones_like(indices, dtype=dtype) * indices_value
return tf.dynamic_stitch([tf.range(size), tf.to_int32(indices)],
[zeros, values])
示例12: _create_regression_targets
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import dynamic_stitch [as 别名]
def _create_regression_targets(self, anchors, groundtruth_boxes, match):
"""Returns a regression target for each anchor.
Args:
anchors: a BoxList representing N anchors
groundtruth_boxes: a BoxList representing M groundtruth_boxes
match: a matcher.Match object
Returns:
reg_targets: a float32 tensor with shape [N, box_code_dimension]
"""
matched_anchor_indices = match.matched_column_indices()
unmatched_ignored_anchor_indices = (match.
unmatched_or_ignored_column_indices())
matched_gt_indices = match.matched_row_indices()
matched_anchors = box_list_ops.gather(anchors,
matched_anchor_indices)
matched_gt_boxes = box_list_ops.gather(groundtruth_boxes,
matched_gt_indices)
matched_reg_targets = self._box_coder.encode(matched_gt_boxes,
matched_anchors)
unmatched_ignored_reg_targets = tf.tile(
self._default_regression_target(),
tf.stack([tf.size(unmatched_ignored_anchor_indices), 1]))
reg_targets = tf.dynamic_stitch(
[matched_anchor_indices, unmatched_ignored_anchor_indices],
[matched_reg_targets, unmatched_ignored_reg_targets])
# TODO: summarize the number of matches on average.
return reg_targets
示例13: _create_classification_targets
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import dynamic_stitch [as 别名]
def _create_classification_targets(self, groundtruth_labels, match):
"""Create classification targets for each anchor.
Assign a classification target of for each anchor to the matching
groundtruth label that is provided by match. Anchors that are not matched
to anything are given the target self._unmatched_cls_target
Args:
groundtruth_labels: a tensor of shape [num_gt_boxes, d_1, ... d_k]
with labels for each of the ground_truth boxes. The subshape
[d_1, ... d_k] can be empty (corresponding to scalar labels).
match: a matcher.Match object that provides a matching between anchors
and groundtruth boxes.
Returns:
cls_targets: a float32 tensor with shape [num_anchors, d_1, d_2 ... d_k],
where the subshape [d_1, ..., d_k] is compatible with groundtruth_labels
which has shape [num_gt_boxes, d_1, d_2, ... d_k].
"""
matched_anchor_indices = match.matched_column_indices()
unmatched_ignored_anchor_indices = (match.
unmatched_or_ignored_column_indices())
matched_gt_indices = match.matched_row_indices()
matched_cls_targets = tf.gather(groundtruth_labels, matched_gt_indices)
ones = self._unmatched_cls_target.shape.ndims * [1]
unmatched_ignored_cls_targets = tf.tile(
tf.expand_dims(self._unmatched_cls_target, 0),
tf.stack([tf.size(unmatched_ignored_anchor_indices)] + ones))
cls_targets = tf.dynamic_stitch(
[matched_anchor_indices, unmatched_ignored_anchor_indices],
[matched_cls_targets, unmatched_ignored_cls_targets])
return cls_targets
示例14: indices_to_dense_vector
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import dynamic_stitch [as 别名]
def indices_to_dense_vector(indices,
size,
indices_value=1.,
default_value=0,
dtype=tf.float32):
"""Creates dense vector with indices set to specific (the para "indices_value" ) and rest to zeros.
This function exists because it is unclear if it is safe to use
tf.sparse_to_dense(indices, [size], 1, validate_indices=False)
with indices which are not ordered.
This function accepts a dynamic size (e.g. tf.shape(tensor)[0])
Args:
indices: 1d Tensor with integer indices which are to be set to
indices_values.
size: scalar with size (integer) of output Tensor.
indices_value: values of elements specified by indices in the output vector
default_value: values of other elements in the output vector.
dtype: data type.
Returns:
dense 1D Tensor of shape [size] with indices set to indices_values and the
rest set to default_value.
"""
size = tf.to_int32(size)
zeros = tf.ones([size], dtype=dtype) * default_value
values = tf.ones_like(indices, dtype=dtype) * indices_value
return tf.dynamic_stitch([tf.range(size), tf.to_int32(indices)],
[zeros, values])
示例15: split_apply_merge
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import dynamic_stitch [as 别名]
def split_apply_merge(inp, partitions, fns):
"""Split input according to partitions. Pass results through fns and merge.
Args:
inp: the input vector
partitions: tensor of same length as input vector, having values 0, 1
fns: the two functions.
Returns:
the vector routed, where routed[i] = fns[partitions[i]](inp[i])
"""
new_inputs = tf.dynamic_partition(inp, partitions, len(fns))
new_outputs = [fns[i](x) for i, x in enumerate(new_inputs)]
new_indices = tf.dynamic_partition(
tf.range(0, inp.get_shape()[0]), partitions, len(fns))
return tf.dynamic_stitch(new_indices, new_outputs)