本文整理匯總了Python中tensorflow.python.ops.rnn._transpose_batch_time方法的典型用法代碼示例。如果您正苦於以下問題:Python rnn._transpose_batch_time方法的具體用法?Python rnn._transpose_batch_time怎麽用?Python rnn._transpose_batch_time使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類tensorflow.python.ops.rnn
的用法示例。
在下文中一共展示了rnn._transpose_batch_time方法的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: transpose_batch_time
# 需要導入模塊: from tensorflow.python.ops import rnn [as 別名]
# 或者: from tensorflow.python.ops.rnn import _transpose_batch_time [as 別名]
def transpose_batch_time(inputs):
"""Transposes inputs between time-major and batch-major.
Args:
inputs: A Tensor of shape `[batch_size, max_time, ...]` (batch-major)
or `[max_time, batch_size, ...]` (time-major), or a (possibly
nested) tuple of such elements.
Returns:
A (possibly nested tuple of) Tensor with transposed batch and
time dimensions of inputs.
"""
flat_input = nest.flatten(inputs)
flat_input = [ops.convert_to_tensor(input_) for input_ in flat_input]
# pylint: disable=protected-access
flat_input = [rnn._transpose_batch_time(input_) for input_ in flat_input]
return nest.pack_sequence_as(structure=inputs, flat_sequence=flat_input)
示例2: _mask_sequences_tensor
# 需要導入模塊: from tensorflow.python.ops import rnn [as 別名]
# 或者: from tensorflow.python.ops.rnn import _transpose_batch_time [as 別名]
def _mask_sequences_tensor(sequence,
sequence_length,
dtype=None,
time_major=False,
tensor_rank=2):
"""Masks out sequence entries that are beyond the respective sequence
lengths. Masks along the time dimension.
Args:
sequence: A Tensor of sequence values.
If `time_major=False` (default), this must be a Tensor of shape:
`[batch_size, max_time, d_2, ..., d_rank]`, where the rank of
the Tensor is specified with :attr:`tensor_rank`.
If `time_major=True`, this must be a Tensor of shape:
`[max_time, batch_size, d_2, ..., d_rank].`
sequence_length: A Tensor of shape `[batch_size]`. Time steps beyond
the respective sequence lengths will be made zero.
dtype: Type of :attr:`sequence`. If `None`, inferred from
:attr:`sequence` automatically.
time_major (bool): The shape format of the inputs. If `True`,
:attr:`sequence` must have shape
`[max_time, batch_size, d_2, ..., d_rank]`.
If `False` (default), :attr:`sequence` must have
shape `[batch_size, max_time, d_2, ..., d_rank]`.
tensor_rank (int): The number of dimensions of :attr:`sequence`.
Default is 2, i.e., :attr:`sequence` is a 2D Tensor consisting
of batch and time dimensions.
Returns:
The masked sequence, i.e., a Tensor of the same shape as
:attr:`sequence` but with masked-out entries (set to zero).
"""
if tensor_rank is None:
tensor_rank = 2
if tensor_rank < 2:
raise ValueError(
"tensor_rank must be > 2. Got tensor_rank = {}".format(tensor_rank))
if time_major:
sequence = rnn._transpose_batch_time(sequence)
max_time = tf.to_int32(tf.shape(sequence)[1])
if dtype is None:
dtype = sequence.dtype
mask = tf.sequence_mask(
tf.to_int32(sequence_length), max_time, dtype=dtype)
for _ in range(2, tensor_rank):
mask = tf.expand_dims(mask, axis=-1)
sequence = sequence * mask
if time_major:
sequence = rnn._transpose_batch_time(sequence)
return sequence
示例3: _mask_sequences_tensor
# 需要導入模塊: from tensorflow.python.ops import rnn [as 別名]
# 或者: from tensorflow.python.ops.rnn import _transpose_batch_time [as 別名]
def _mask_sequences_tensor(sequence,
sequence_length,
dtype=None,
time_major=False,
tensor_rank=2):
"""Masks out sequence entries that are beyond the respective sequence
lengths. Masks along the time dimension.
Args:
sequence: A Tensor of sequence values.
If `time_major=False` (default), this must be a Tensor of shape:
`[batch_size, max_time, d_2, ..., d_rank]`, where the rank of
the Tensor is specified with :attr:`tensor_rank`.
If `time_major=True`, this must be a Tensor of shape:
`[max_time, batch_size, d_2, ..., d_rank].`
sequence_length: A Tensor of shape `[batch_size]`. Time steps beyond
the respective sequence lengths will be made zero.
dtype (dtype): Type of :attr:`sequence`. If `None`, infer from
:attr:`sequence` automatically.
time_major (bool): The shape format of the inputs. If `True`,
:attr:`sequence` must have shape
`[max_time, batch_size, d_2, ..., d_rank]`.
If `False` (default), :attr:`sequence` must have
shape `[batch_size, max_time, d_2, ..., d_rank]`.
tensor_rank (int): The number of dimensions of :attr:`sequence`.
Default is 2, i.e., :attr:`sequence` is a 2D Tensor consisting
of batch and time dimensions.
Returns:
The masked sequence, i.e., a Tensor of the same shape as
:attr:`sequence` but with masked-out entries (set to zero).
"""
if tensor_rank is None:
tensor_rank = 2
if tensor_rank < 2:
raise ValueError(
"tensor_rank must be > 2. Got tensor_rank = {}".format(tensor_rank))
if time_major:
sequence = rnn._transpose_batch_time(sequence)
max_time = tf.cast(tf.shape(sequence)[1], tf.int32)
if dtype is None:
dtype = sequence.dtype
mask = tf.sequence_mask(
tf.cast(sequence_length, tf.int32), max_time, dtype=dtype)
for _ in range(2, tensor_rank):
mask = tf.expand_dims(mask, axis=-1)
sequence = sequence * mask
if time_major:
sequence = rnn._transpose_batch_time(sequence)
return sequence