用法
__getitem__(
key
)
參數
-
rt_input
要切片的 RaggedTensor。 -
key
使用標準 Python 語義(例如,從末尾開始的負值索引)指示要返回的 RaggedTensor 的哪一部分。key
可能有以下任何一種類型:int
常量- 標量整數
Tensor
slice
包含整數常量和/或標量整數Tensor
sEllipsis
tuple
包含以上任何一項(用於多維索引)
返回
-
Tensor
或RaggedTensor
對象。包含至少一個參差不齊的維度的值將返回為RaggedTensor
。不包含不規則尺寸的值將返回為Tensor
。有關返回Tensor
s 與RaggedTensor
s 的表達式示例,請參見上文。
拋出
-
ValueError
如果key
超出範圍。 -
ValueError
如果不支持key
。 -
TypeError
如果key
中的索引具有不受支持的類型。
返回此 RaggedTensor 的指定部分。
支持多維索引和切片,但有一個限製:不允許索引到參差不齊的內部維度。這種情況是有問題的,因為指示的值可能存在於某些行中但不存在於其他行中。在這種情況下,我們是否應該(1)報告 IndexError 並不明顯; (2) 使用默認值;或 (3) 跳過該值並返回一個比我們開始時行數更少的張量。遵循 Python 的指導原則(“麵對歧義,拒絕猜測的誘惑”),我們簡單地禁止這種操作。
例子:
# A 2-D ragged tensor with 1 ragged dimension.
rt = tf.ragged.constant([['a', 'b', 'c'], ['d', 'e'], ['f'], ['g']])
rt[0].numpy() # First row (1-D `Tensor`)
array([b'a', b'b', b'c'], dtype=object)
rt[:3].to_list() # First three rows (2-D RaggedTensor)
[[b'a', b'b', b'c'], [b'd', b'e'], [b'f']]
rt[3, 0].numpy() # 1st element of 4th row (scalar)
b'g'
# A 3-D ragged tensor with 2 ragged dimensions.
rt = tf.ragged.constant([[[1, 2, 3], [4]],
[[5], [], [6]],
[[7]],
[[8, 9], [10]]])
rt[1].to_list() # Second row (2-D RaggedTensor)
[[5], [], [6]]
rt[3, 0].numpy() # First element of fourth row (1-D Tensor)
array([8, 9], dtype=int32)
rt[:, 1:3].to_list() # Items 1-3 of each row (3-D RaggedTensor)
[[[4]], [[], [6]], [], [[10]]]
rt[:, -1:].to_list() # Last item of each row (3-D RaggedTensor)
[[[4]], [[6]], [[7]], [[10]]]
相關用法
- Python tf.RaggedTensor.__ge__用法及代碼示例
- Python tf.RaggedTensor.__gt__用法及代碼示例
- Python tf.RaggedTensor.__rmul__用法及代碼示例
- Python tf.RaggedTensor.__radd__用法及代碼示例
- Python tf.RaggedTensor.__pow__用法及代碼示例
- Python tf.RaggedTensor.__rand__用法及代碼示例
- Python tf.RaggedTensor.__xor__用法及代碼示例
- Python tf.RaggedTensor.__rpow__用法及代碼示例
- Python tf.RaggedTensor.__rxor__用法及代碼示例
- Python tf.RaggedTensor.__ror__用法及代碼示例
- Python tf.RaggedTensor.__rsub__用法及代碼示例
- Python tf.RaggedTensor.__abs__用法及代碼示例
- Python tf.RaggedTensor.__sub__用法及代碼示例
- Python tf.RaggedTensor.__add__用法及代碼示例
- Python tf.RaggedTensor.__lt__用法及代碼示例
- Python tf.RaggedTensor.__or__用法及代碼示例
- Python tf.RaggedTensor.__invert__用法及代碼示例
- Python tf.RaggedTensor.__le__用法及代碼示例
- Python tf.RaggedTensor.__mul__用法及代碼示例
- Python tf.RaggedTensor.__and__用法及代碼示例
注:本文由純淨天空篩選整理自tensorflow.org大神的英文原創作品 tf.RaggedTensor.__getitem__。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。