本文整理汇总了Python中tensorflow.dynamic_stitch函数的典型用法代码示例。如果您正苦于以下问题:Python dynamic_stitch函数的具体用法?Python dynamic_stitch怎么用?Python dynamic_stitch使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了dynamic_stitch函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: testErrorDataDimSizeMismatch
def testErrorDataDimSizeMismatch(self):
indices = [tf.constant([0, 4, 5]),
tf.constant([1, 6, 2, 3])]
data = [tf.constant([[0], [40], [70]]),
tf.constant([[10, 11], [60, 61], [20, 21], [30, 31]])]
with self.assertRaises(ValueError):
tf.dynamic_stitch(indices, data)
示例2: testErrorDataAndIndicesSizeMismatch
def testErrorDataAndIndicesSizeMismatch(self):
indices = [tf.constant([0, 4, 7]),
tf.constant([1, 6, 2, 3, 5])]
data = [tf.constant([0, 40, 70]),
tf.constant([10, 60, 20, 30])]
with self.assertRaises(ValueError):
tf.dynamic_stitch(indices, data)
示例3: loop
def loop(q_, mask, mass_, found_):
q_list = tf.dynamic_partition(q_, mask, 2)
condition_indices = tf.dynamic_partition(tf.range(tf.shape(q_)[0]), mask, 2) # 0 element it False,
# 1 element if true
p = q_list[1] * (1.0 - mass_) / tf.reduce_sum(q_list[1])
p_new = tf.dynamic_stitch(condition_indices, [q_list[0], p])
# condition verification and mask modification
less_mask = tf.cast(tf.less(u, p_new), tf.int32) # 0 when u is bigger than p, 1 when u is less than p
condition_indices = tf.dynamic_partition(tf.range(tf.shape(p_new)[0]), less_mask,
2) # 0 when u is bigger than p, 1 when u is less than p
split_p_new = tf.dynamic_partition(p_new, less_mask, 2)
split_u = tf.dynamic_partition(u, less_mask, 2)
alpha = tf.dynamic_stitch(condition_indices, [split_p_new[0], split_u[1]])
mass_ += tf.reduce_sum(split_u[1])
mask = mask * (tf.ones_like(less_mask) - less_mask)
found_ = tf.cond(tf.equal(tf.reduce_sum(less_mask), 0),
lambda: False,
lambda: True)
alpha = tf.reshape(alpha, q_.shape)
return alpha, mask, mass_, found_
示例4: testErrorIndicesMultiDimensional
def testErrorIndicesMultiDimensional(self):
indices = [tf.constant([0, 4, 7]),
tf.constant([[1, 6, 2, 3, 5]])]
data = [tf.constant([[0, 40, 70]]),
tf.constant([10, 60, 20, 30, 50])]
with self.assertRaises(ValueError):
tf.dynamic_stitch(indices, data)
示例5: CircularConvolution
def CircularConvolution(vector, kernel):
size = int(vector.get_shape()[0])
kernel_size = int(kernel.get_shape()[0])
kernel_shift = int(math.floor(kernel_size/2.0))
output = tf.zeros_like(vector)
def loop(idx):
if idx < 0: return size + idx
if idx >= size : return idx - size
else: return idx
kernels = []
for i in xrange(size):
indices = [loop(i+j) for j in xrange(kernel_shift, -kernel_shift-1, -1)]
v = tf.gather(vector, indices)
kernels.append(tf.reduce_sum(v * kernel, 0, keep_dims=True))
output = tf.dynamic_stitch([[i] for i in xrange(size)], kernels)
# # code with double loop
# for i in xrange(size):
# for j in xrange(kernel_size):
# idx = i + kernel_shift - j + 1
# if idx < 0: idx = idx + size
# if idx >= size: idx = idx - size
# w = tf.gather(vector, int(idx)) * tf.gather(kernel, j)
# output = tf.scatter_add(output, [i], tf.reshape(w, [1, -1]))
return output
示例6: testSumGradArgs
def testSumGradArgs(self):
with self.test_session(use_gpu=False):
indices = [tf.convert_to_tensor([0, 1, 2, 3]),
tf.convert_to_tensor([2, 3])]
values = [tf.convert_to_tensor([2, 3, 5, 7]), tf.convert_to_tensor([1, 1])]
self.assertAllEqual(
tf.dynamic_stitch(indices, values).eval(), [2, 3, 1, 1])
示例7: indices_to_dense_vector
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])
示例8: _create_regression_targets
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
示例9: _partition_and_stitch
def _partition_and_stitch(self, args, func_name):
"""
args is a list of tensors, to be passed to self.likelihoods.<func_name>
args[-1] is the 'Y' argument, which contains the indexes to self.likelihoods.
This function splits up the args using dynamic_partition, calls the
relevant function on the likelihoods, and re-combines the result.
"""
# get the index from Y
Y = args[-1]
ind = Y[:, -1]
ind = tf.cast(ind, tf.int32)
Y = Y[:, :-1]
args[-1] = Y
# split up the arguments into chunks corresponding to the relevant likelihoods
args = zip(*[tf.dynamic_partition(X, ind, self.num_likelihoods) for X in args])
# apply the likelihood-function to each section of the data
with params_as_tensors_for(self, convert=False):
funcs = [getattr(lik, func_name) for lik in self.likelihood_list]
results = [f(*args_i) for f, args_i in zip(funcs, args)]
# stitch the results back together
partitions = tf.dynamic_partition(tf.range(0, tf.size(ind)), ind, self.num_likelihoods)
results = tf.dynamic_stitch(partitions, results)
return results
示例10: circular_convolution
def circular_convolution(v, k):
"""Computes circular convolution.
Args:
v: a 1-D `Tensor` (vector)
k: a 1-D `Tensor` (kernel)
"""
size = int(v.get_shape()[0])
kernel_size = int(k.get_shape()[0])
kernel_shift = int(math.floor(kernel_size/2.0))
def loop(idx):
if idx < 0: return size + idx
if idx >= size : return idx - size
else: return idx
kernels = []
for i in xrange(size):
indices = [loop(i+j) for j in xrange(kernel_shift, -kernel_shift-1, -1)]
v_ = tf.gather(v, indices)
kernels.append(tf.reduce_sum(v_ * k, 0))
# # code with double loop
# for i in xrange(size):
# for j in xrange(kernel_size):
# idx = i + kernel_shift - j + 1
# if idx < 0: idx = idx + size
# if idx >= size: idx = idx - size
# w = tf.gather(v, int(idx)) * tf.gather(kernel, j)
# output = tf.scatter_add(output, [i], tf.reshape(w, [1, -1]))
return tf.dynamic_stitch([i for i in xrange(size)], kernels)
示例11: _create_classification_targets
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
示例12: scheduled_sample_count
def scheduled_sample_count(ground_truth_x,
generated_x,
batch_size,
scheduled_sample_var):
"""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
scheduled_sample_var: 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.
"""
num_ground_truth = scheduled_sample_var
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)
output = tf.dynamic_stitch([ground_truth_idx, generated_idx],
[ground_truth_examps, generated_examps])
# if batch size is known set it.
if isinstance(batch_size, int):
output.set_shape([batch_size] + common_layers.shape_list(output)[1:])
return output
示例13: test_dynamic_stitch
def test_dynamic_stitch(sess):
x = tf.zeros((1, 3))
y = tf.dynamic_stitch([[0], [0]], [x, tf.ones((1, 3))])
z = tf.gather(y, [0])
with sess.as_default():
analytic, numeric = tf.test.compute_gradient(x, (1, 3), z, (1, 3))
assert np.allclose(analytic, numeric)
示例14: testOneListOneDimensional
def testOneListOneDimensional(self):
with self.test_session():
indices = [tf.constant([1, 6, 2, 3, 5, 0, 4, 7])]
data = [tf.constant([10, 60, 20, 30, 50, 0, 40, 70])]
stitched_t = tf.dynamic_stitch(indices, data)
stitched_val = stitched_t.eval()
self.assertAllEqual([0, 10, 20, 30, 40, 50, 60, 70], stitched_val)
# Dimension 0 is determined by the max index in indices, so we
# can only infer that the output is a vector of some unknown
# length.
self.assertEqual([None], stitched_t.get_shape().as_list())
示例15: testStitchOrder
def testStitchOrder(self):
with self.test_session():
indices = []
np_values = []
values = []
for _ in range(10):
indices.extend([tf.convert_to_tensor(np.arange(100).astype(np.int32))])
np_values.extend([np.random.uniform(size=100)])
values.extend([tf.convert_to_tensor(np_values[-1])])
stitched = tf.dynamic_stitch(indices, values).eval()
self.assertAllEqual(np_values[-1], stitched)