用法
@classmethod
from_tensor(
tensor, lengths=None, padding=None, ragged_rank=1, name=None,
row_splits_dtype=tf.dtypes.int64
)参数
-
tensor要转换的Tensor。等级必须为ragged_rank + 1或更高。 -
lengths一组可选的行长度,使用长度等于tensor.shape[0](tensor中的行数)的一维整数Tensor指定。如果指定,则output[row]将包含tensor[row][:lengths[row]]。负长度被视为零。您可以选择将长度列表或元组传递给此参数,它们将用作嵌套行长度以构造具有多个不规则维度的不规则张量。 -
padding可选的填充值。如果指定,则任何完全由padding组成的行后缀都将从返回的 RaggedTensor 中排除。padding是与tensor和shape=tensor.shape[ragged_rank + 1:]具有相同 dtype 的Tensor。 -
ragged_rank指定返回的RaggedTensor的不规则排名的整数。必须大于零。 -
name返回张量的名称前缀(可选)。 -
row_splits_dtypedtype用于返回的RaggedTensor的row_splits张量。tf.int32或tf.int64之一。
返回
-
带有指定
ragged_rank的RaggedTensor。返回的不规则张量的形状与tensor的形状兼容。
抛出
-
ValueError如果同时指定了lengths和padding。
将 tf.Tensor 转换为 RaggedTensor 。
可以使用长度向量或填充值(但不能同时使用两者)来指定缺失/默认值的集合。如果指定了lengths,则输出张量将满足output[row] = tensor[row][:lengths[row]]。如果'lengths' 是列表列表或列表元组,则这些列表将用作嵌套行长度。如果指定了 padding,则任何完全由 padding 组成的行后缀都将从返回的 RaggedTensor 中排除。如果既没有指定lengths 也没有指定padding,则返回的RaggedTensor 将没有缺失/默认值。
例子:
dt = tf.constant([[5, 7, 0], [0, 3, 0], [6, 0, 0]])
tf.RaggedTensor.from_tensor(dt)
<tf.RaggedTensor [[5, 7, 0], [0, 3, 0], [6, 0, 0]]>
tf.RaggedTensor.from_tensor(dt, lengths=[1, 0, 3])
<tf.RaggedTensor [[5], [], [6, 0, 0]]>
tf.RaggedTensor.from_tensor(dt, padding=0)
<tf.RaggedTensor [[5, 7], [0, 3], [6]]>
dt = tf.constant([[[5, 0], [7, 0], [0, 0]],
[[0, 0], [3, 0], [0, 0]],
[[6, 0], [0, 0], [0, 0]]])
tf.RaggedTensor.from_tensor(dt, lengths=([2, 0, 3], [1, 1, 2, 0, 1]))
<tf.RaggedTensor [[[5], [7]], [], [[6, 0], [], [0]]]>
相关用法
- Python tf.RaggedTensor.from_uniform_row_length用法及代码示例
- Python tf.RaggedTensor.from_value_rowids用法及代码示例
- Python tf.RaggedTensor.from_nested_row_lengths用法及代码示例
- Python tf.RaggedTensor.from_row_limits用法及代码示例
- Python tf.RaggedTensor.from_nested_row_splits用法及代码示例
- Python tf.RaggedTensor.from_sparse用法及代码示例
- Python tf.RaggedTensor.from_row_splits用法及代码示例
- Python tf.RaggedTensor.from_row_starts用法及代码示例
- Python tf.RaggedTensor.from_nested_value_rowids用法及代码示例
- Python tf.RaggedTensor.from_row_lengths用法及代码示例
- Python tf.RaggedTensor.row_lengths用法及代码示例
- Python tf.RaggedTensor.__rmul__用法及代码示例
- Python tf.RaggedTensor.__radd__用法及代码示例
- Python tf.RaggedTensor.__pow__用法及代码示例
- Python tf.RaggedTensor.__rand__用法及代码示例
- Python tf.RaggedTensor.numpy用法及代码示例
- Python tf.RaggedTensor.get_shape用法及代码示例
- Python tf.RaggedTensor.__xor__用法及代码示例
- Python tf.RaggedTensor.__gt__用法及代码示例
- Python tf.RaggedTensor.__getitem__用法及代码示例
注:本文由纯净天空筛选整理自tensorflow.org大神的英文原创作品 tf.RaggedTensor.from_tensor。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。
