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