將 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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。
