将 op 应用于一个或多个 RaggedTensor 的 flat_values。
用法
tf.ragged.map_flat_values(
op, *args, **kwargs
)参数
-
op应该应用于 RaggedTensorflat_values的操作。op通常是逐元素操作(例如math_ops.add),但可以使用任何保留最外层维度大小的操作。即,op返回的值的shape[0]必须与RaggedTensors'flat_values张量的shape[0]匹配。 -
*argsop的参数。 -
**kwargsop的关键字参数。
返回
-
一个
RaggedTensor,其ragged_rank与所有输入RaggedTensor的ragged_rank匹配。
抛出
-
ValueError如果 args 不包含RaggedTensors,或者输入RaggedTensor的nested_splits不相同。
将 args 或 kwargs 中的任何 RaggedTensor 替换为其 flat_values 张量(它会折叠所有参差不齐的维度),然后调用 op 。返回由输入 RaggedTensor s' nested_row_splits 和 op 返回的值构成的 RaggedTensor。
如果输入参数包含多个 RaggedTensor ,则它们必须具有相同的 nested_row_splits 。
此操作通常用于将元素操作应用于 RaggedTensor 中的每个值。
警告: tf.ragged.map_flat_values做不是应用op到参差不齐的张量的每一行。这种差异对于非元素操作很重要,例如tf.math.reduce_sum.如果您希望对不规则张量的每一行应用非元素操作,请使用tf.map_fn反而。 (您可能需要指定一个output_signature使用时tf.map_fn带有参差不齐的张量。)
例子:
rt = tf.ragged.constant([[1, 2, 3], [], [4, 5], [6]])
tf.ragged.map_flat_values(tf.ones_like, rt)
<tf.RaggedTensor [[1, 1, 1], [], [1, 1], [1]]>
tf.ragged.map_flat_values(tf.multiply, rt, rt)
<tf.RaggedTensor [[1, 4, 9], [], [16, 25], [36]]>
tf.ragged.map_flat_values(tf.add, rt, 5)
<tf.RaggedTensor [[6, 7, 8], [], [9, 10], [11]]>
非元素操作的示例(请注意,map_flat_values 和 map_fn 返回不同的结果):
rt = tf.ragged.constant([[1.0, 3.0], [], [3.0, 6.0, 3.0]])
def normalized(x):
return x / tf.reduce_sum(x)
tf.ragged.map_flat_values(normalized, rt)
<tf.RaggedTensor [[0.0625, 0.1875], [], [0.1875, 0.375, 0.1875]]>
tf.map_fn(normalized, rt)
<tf.RaggedTensor [[0.25, 0.75], [], [0.25, 0.5, 0.25]]>
相关用法
- Python tf.ragged.cross用法及代码示例
- Python tf.ragged.stack_dynamic_partitions用法及代码示例
- Python tf.ragged.boolean_mask用法及代码示例
- Python tf.ragged.stack用法及代码示例
- Python tf.ragged.range用法及代码示例
- Python tf.ragged.row_splits_to_segment_ids用法及代码示例
- Python tf.ragged.cross_hashed用法及代码示例
- Python tf.ragged.segment_ids_to_row_splits用法及代码示例
- Python tf.ragged.constant用法及代码示例
- Python tf.raw_ops.TPUReplicatedInput用法及代码示例
- Python tf.raw_ops.Bitcast用法及代码示例
- Python tf.raw_ops.SelfAdjointEigV2用法及代码示例
- Python tf.raw_ops.BatchMatMul用法及代码示例
- Python tf.raw_ops.OneHot用法及代码示例
- Python tf.raw_ops.ResourceScatterNdSub用法及代码示例
- Python tf.raw_ops.ReadVariableXlaSplitND用法及代码示例
- Python tf.raw_ops.GatherV2用法及代码示例
- Python tf.raw_ops.Expm1用法及代码示例
- Python tf.range用法及代码示例
- Python tf.raw_ops.BitwiseAnd用法及代码示例
注:本文由纯净天空筛选整理自tensorflow.org大神的英文原创作品 tf.ragged.map_flat_values。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。
