本文整理汇总了Python中tensorflow.python.ops.array_ops.reverse方法的典型用法代码示例。如果您正苦于以下问题:Python array_ops.reverse方法的具体用法?Python array_ops.reverse怎么用?Python array_ops.reverse使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类tensorflow.python.ops.array_ops
的用法示例。
在下文中一共展示了array_ops.reverse方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: flip_left_right
# 需要导入模块: from tensorflow.python.ops import array_ops [as 别名]
# 或者: from tensorflow.python.ops.array_ops import reverse [as 别名]
def flip_left_right(image):
"""Flip an image horizontally (left to right).
Outputs the contents of `image` flipped along the second dimension, which is
`width`.
See also `reverse()`.
Args:
image: A 3-D tensor of shape `[height, width, channels].`
Returns:
A 3-D tensor of the same type and shape as `image`.
Raises:
ValueError: if the shape of `image` not supported.
"""
image = ops.convert_to_tensor(image, name='image')
image = control_flow_ops.with_dependencies(
_Check3DImage(image, require_static=False), image)
return fix_image_flip_shape(image, array_ops.reverse(image, [1]))
示例2: flip_up_down
# 需要导入模块: from tensorflow.python.ops import array_ops [as 别名]
# 或者: from tensorflow.python.ops.array_ops import reverse [as 别名]
def flip_up_down(image):
"""Flip an image horizontally (upside down).
Outputs the contents of `image` flipped along the first dimension, which is
`height`.
See also `reverse()`.
Args:
image: A 3-D tensor of shape `[height, width, channels].`
Returns:
A 3-D tensor of the same type and shape as `image`.
Raises:
ValueError: if the shape of `image` not supported.
"""
image = ops.convert_to_tensor(image, name='image')
image = control_flow_ops.with_dependencies(
_Check3DImage(image, require_static=False), image)
return fix_image_flip_shape(image, array_ops.reverse(image, [0]))
示例3: reverse
# 需要导入模块: from tensorflow.python.ops import array_ops [as 别名]
# 或者: from tensorflow.python.ops.array_ops import reverse [as 别名]
def reverse(x, axes):
"""Reverse a tensor along the specified axes.
Arguments:
x: Tensor to reverse.
axes: Integer or iterable of integers.
Axes to reverse.
Returns:
A tensor.
"""
if isinstance(axes, int):
axes = [axes]
return array_ops.reverse(x, axes)
# VALUE MANIPULATION
示例4: testUnknownDims
# 需要导入模块: from tensorflow.python.ops import array_ops [as 别名]
# 或者: from tensorflow.python.ops.array_ops import reverse [as 别名]
def testUnknownDims(self):
data_t = tf.placeholder(tf.float32)
dims_known_t = tf.placeholder(tf.bool, shape=[3])
reverse_known_t = tf.reverse(data_t, dims_known_t)
self.assertEqual(3, reverse_known_t.get_shape().ndims)
dims_unknown_t = tf.placeholder(tf.bool)
reverse_unknown_t = tf.reverse(data_t, dims_unknown_t)
self.assertIs(None, reverse_unknown_t.get_shape().ndims)
data_2d_t = tf.placeholder(tf.float32, shape=[None, None])
dims_2d_t = tf.placeholder(tf.bool, shape=[2])
reverse_2d_t = tf.reverse(data_2d_t, dims_2d_t)
self.assertEqual(2, reverse_2d_t.get_shape().ndims)
dims_3d_t = tf.placeholder(tf.bool, shape=[3])
with self.assertRaisesRegexp(ValueError, "must be rank 3"):
tf.reverse(data_2d_t, dims_3d_t)
示例5: _reverse2DimAuto
# 需要导入模块: from tensorflow.python.ops import array_ops [as 别名]
# 或者: from tensorflow.python.ops.array_ops import reverse [as 别名]
def _reverse2DimAuto(self, np_dtype):
x_np = np.array([[1, 2, 3], [4, 5, 6]], dtype=np_dtype)
for use_gpu in [False, True]:
with self.test_session(use_gpu=use_gpu):
x_tf_1 = array_ops.reverse_v2(x_np, [0]).eval()
x_tf_2 = array_ops.reverse_v2(x_np, [-2]).eval()
x_tf_3 = array_ops.reverse_v2(x_np, [1]).eval()
x_tf_4 = array_ops.reverse_v2(x_np, [-1]).eval()
x_tf_5 = array_ops.reverse_v2(x_np, [1, 0]).eval()
self.assertAllEqual(x_tf_1, np.asarray(x_np)[::-1, :])
self.assertAllEqual(x_tf_2, np.asarray(x_np)[::-1, :])
self.assertAllEqual(x_tf_3, np.asarray(x_np)[:, ::-1])
self.assertAllEqual(x_tf_4, np.asarray(x_np)[:, ::-1])
self.assertAllEqual(x_tf_5, np.asarray(x_np)[::-1, ::-1])
# This is the version of reverse that uses axis indices rather than
# bool tensors
# TODO(b/32254538): Change this test to use array_ops.reverse
示例6: random_flip_up_down
# 需要导入模块: from tensorflow.python.ops import array_ops [as 别名]
# 或者: from tensorflow.python.ops.array_ops import reverse [as 别名]
def random_flip_up_down(image, seed=None):
"""Randomly flips an image vertically (upside down).
With a 1 in 2 chance, outputs the contents of `image` flipped along the first
dimension, which is `height`. Otherwise output the image as-is.
Args:
image: A 3-D tensor of shape `[height, width, channels].`
seed: A Python integer. Used to create a random seed. See
[`set_random_seed`](../../api_docs/python/constant_op.md#set_random_seed)
for behavior.
Returns:
A 3-D tensor of the same type and shape as `image`.
Raises:
ValueError: if the shape of `image` not supported.
"""
image = ops.convert_to_tensor(image, name='image')
_Check3DImage(image, require_static=False)
uniform_random = random_ops.random_uniform([], 0, 1.0, seed=seed)
mirror = math_ops.less(array_ops.pack([uniform_random, 1.0, 1.0]), 0.5)
return array_ops.reverse(image, mirror)
示例7: random_flip_left_right
# 需要导入模块: from tensorflow.python.ops import array_ops [as 别名]
# 或者: from tensorflow.python.ops.array_ops import reverse [as 别名]
def random_flip_left_right(image, seed=None):
"""Randomly flip an image horizontally (left to right).
With a 1 in 2 chance, outputs the contents of `image` flipped along the
second dimension, which is `width`. Otherwise output the image as-is.
Args:
image: A 3-D tensor of shape `[height, width, channels].`
seed: A Python integer. Used to create a random seed. See
[`set_random_seed`](../../api_docs/python/constant_op.md#set_random_seed)
for behavior.
Returns:
A 3-D tensor of the same type and shape as `image`.
Raises:
ValueError: if the shape of `image` not supported.
"""
image = ops.convert_to_tensor(image, name='image')
_Check3DImage(image, require_static=False)
uniform_random = random_ops.random_uniform([], 0, 1.0, seed=seed)
mirror = math_ops.less(array_ops.pack([1.0, uniform_random, 1.0]), 0.5)
return array_ops.reverse(image, mirror)
示例8: flip_left_right
# 需要导入模块: from tensorflow.python.ops import array_ops [as 别名]
# 或者: from tensorflow.python.ops.array_ops import reverse [as 别名]
def flip_left_right(image):
"""Flip an image horizontally (left to right).
Outputs the contents of `image` flipped along the second dimension, which is
`width`.
See also `reverse()`.
Args:
image: A 3-D tensor of shape `[height, width, channels].`
Returns:
A 3-D tensor of the same type and shape as `image`.
Raises:
ValueError: if the shape of `image` not supported.
"""
image = ops.convert_to_tensor(image, name='image')
_Check3DImage(image, require_static=False)
return array_ops.reverse(image, [False, True, False])
示例9: flip_up_down
# 需要导入模块: from tensorflow.python.ops import array_ops [as 别名]
# 或者: from tensorflow.python.ops.array_ops import reverse [as 别名]
def flip_up_down(image):
"""Flip an image horizontally (upside down).
Outputs the contents of `image` flipped along the first dimension, which is
`height`.
See also `reverse()`.
Args:
image: A 3-D tensor of shape `[height, width, channels].`
Returns:
A 3-D tensor of the same type and shape as `image`.
Raises:
ValueError: if the shape of `image` not supported.
"""
image = ops.convert_to_tensor(image, name='image')
_Check3DImage(image, require_static=False)
return array_ops.reverse(image, [True, False, False])
示例10: _reverse
# 需要导入模块: from tensorflow.python.ops import array_ops [as 别名]
# 或者: from tensorflow.python.ops.array_ops import reverse [as 别名]
def _reverse(self, t, lengths):
"""Time reverse the provided tensor or list of tensors.
Assumes the top dimension is the time dimension.
Args:
t: 3D tensor or list of 2D tensors to be reversed
lengths: 1D tensor of lengths, or `None`
Returns:
A reversed tensor or list of tensors
"""
if isinstance(t, list):
return list(reversed(t))
else:
if lengths is None:
return array_ops.reverse(t, [True, False, False])
else:
return array_ops.reverse_sequence(t, lengths, 0, 1)
示例11: testNextSentencePredictionExtractor
# 需要导入模块: from tensorflow.python.ops import array_ops [as 别名]
# 或者: from tensorflow.python.ops.array_ops import reverse [as 别名]
def testNextSentencePredictionExtractor(self,
sentences,
expected_segment_a,
expected_segment_b,
expected_labels,
random_next_sentence_threshold=0.5,
test_description=""):
sentences = ragged_factory_ops.constant(sentences)
# Set seed and rig the shuffle function to a deterministic reverse function
# instead. This is so that we have consistent and deterministic results.
random_seed.set_seed(1234)
nsp = segment_extractor_ops.NextSentencePredictionExtractor(
shuffle_fn=functools.partial(array_ops.reverse, axis=[-1]),
random_next_sentence_threshold=random_next_sentence_threshold,
)
results = nsp.get_segments(sentences)
actual_segment_a, actual_segment_b, actual_labels = results
self.assertAllEqual(expected_segment_a, actual_segment_a)
self.assertAllEqual(expected_segment_b, actual_segment_b)
self.assertAllEqual(expected_labels, actual_labels)
示例12: flip_up_down
# 需要导入模块: from tensorflow.python.ops import array_ops [as 别名]
# 或者: from tensorflow.python.ops.array_ops import reverse [as 别名]
def flip_up_down(image):
"""Flip an image vertically (upside down).
Outputs the contents of `image` flipped along the first dimension, which is
`height`.
See also `reverse()`.
Args:
image: A 3-D tensor of shape `[height, width, channels].`
Returns:
A 3-D tensor of the same type and shape as `image`.
Raises:
ValueError: if the shape of `image` not supported.
"""
image = ops.convert_to_tensor(image, name='image')
image = control_flow_ops.with_dependencies(
_Check3DImage(image, require_static=False), image)
return fix_image_flip_shape(image, array_ops.reverse(image, [0]))
示例13: random_flip_up_down
# 需要导入模块: from tensorflow.python.ops import array_ops [as 别名]
# 或者: from tensorflow.python.ops.array_ops import reverse [as 别名]
def random_flip_up_down(image, seed=None):
"""Randomly flips an image vertically (upside down).
With a 1 in 2 chance, outputs the contents of `image` flipped along the first
dimension, which is `height`. Otherwise output the image as-is.
Args:
image: A 3-D tensor of shape `[height, width, channels].`
seed: A Python integer. Used to create a random seed. See
@{tf.set_random_seed}
for behavior.
Returns:
A 3-D tensor of the same type and shape as `image`.
Raises:
ValueError: if the shape of `image` not supported.
"""
image = ops.convert_to_tensor(image, name='image')
image = control_flow_ops.with_dependencies(
_Check3DImage(image, require_static=False), image)
uniform_random = random_ops.random_uniform([], 0, 1.0, seed=seed)
mirror_cond = math_ops.less(uniform_random, .5)
result = control_flow_ops.cond(mirror_cond,
lambda: array_ops.reverse(image, [0]),
lambda: image)
return fix_image_flip_shape(image, result)
示例14: random_flip_left_right
# 需要导入模块: from tensorflow.python.ops import array_ops [as 别名]
# 或者: from tensorflow.python.ops.array_ops import reverse [as 别名]
def random_flip_left_right(image, seed=None):
"""Randomly flip an image horizontally (left to right).
With a 1 in 2 chance, outputs the contents of `image` flipped along the
second dimension, which is `width`. Otherwise output the image as-is.
Args:
image: A 3-D tensor of shape `[height, width, channels].`
seed: A Python integer. Used to create a random seed. See
@{tf.set_random_seed}
for behavior.
Returns:
A 3-D tensor of the same type and shape as `image`.
Raises:
ValueError: if the shape of `image` not supported.
"""
image = ops.convert_to_tensor(image, name='image')
image = control_flow_ops.with_dependencies(
_Check3DImage(image, require_static=False), image)
uniform_random = random_ops.random_uniform([], 0, 1.0, seed=seed)
mirror_cond = math_ops.less(uniform_random, .5)
result = control_flow_ops.cond(mirror_cond,
lambda: array_ops.reverse(image, [1]),
lambda: image)
return fix_image_flip_shape(image, result)
示例15: rnn
# 需要导入模块: from tensorflow.python.ops import array_ops [as 别名]
# 或者: from tensorflow.python.ops.array_ops import reverse [as 别名]
def rnn(cls, x, cell_class, cell_kwargs, rnn_kwargs, activations, direction):
cell_kwargs["activation"] = activations[0]
rnn_cell = [cell_class(**cell_kwargs)]
cell_fw = tf.compat.v1.nn.rnn_cell.MultiRNNCell(rnn_cell)
if direction == "bidirectional":
cell_kwargs["activation"] = activations[1]
rnn_cell_bw = [cell_class(**cell_kwargs)]
cell_bw = tf.compat.v1.nn.rnn_cell.MultiRNNCell(rnn_cell_bw)
if direction == "forward":
outputs, states = tf.compat.v1.nn.dynamic_rnn(cell_fw, x, **rnn_kwargs)
elif direction == "bidirectional":
outputs, states = tf.compat.v1.nn.bidirectional_dynamic_rnn(
cell_fw, cell_bw, x, **rnn_kwargs)
elif direction == "reverse":
def _reverse(input_, seq_dim):
return array_ops.reverse(input_, axis=[seq_dim])
time_dim = 0
inputs_reverse = _reverse(x, time_dim)
outputs, states = tf.compat.v1.nn.dynamic_rnn(cell_fw, inputs_reverse,
**rnn_kwargs)
outputs = _reverse(outputs, time_dim)
return outputs, states