從張量中提取切片。
用法
tf.slice(
input_, begin, size, name=None
)參數
-
input_一個Tensor。 -
begin一個int32或int64Tensor。 -
size一個int32或int64Tensor。 -
name操作的名稱(可選)。
返回
-
Tensor與input_類型相同。
此操作從張量 input_ 中提取大小為 size 的切片,從 begin 指定的位置開始。切片size 表示為張量形狀,其中size[i] 是要切片的input_ 的'i'th 維度的元素數。切片的起始位置 (begin) 表示為 input_ 的每個維度中的偏移量。換句話說,begin[i] 是您要從中切片的input_ 的第 i 個維度的偏移量。
請注意,tf.Tensor.getitem 通常是一種更 Pythonic 的方式來執行切片,因為它允許您編寫 foo[3:7,:-2] 而不是 tf.slice(foo, [3, 0], [4, foo.get_shape()[1]-2]) 。
begin 是從零開始的; size 是基於 1 的。如果size[i] 為 -1,則維度 i 中的所有剩餘元素都包含在切片中。換句話說,這相當於設置:
size[i] = input_.dim_size(i) - begin[i]
此操作要求:
0 <= begin[i] <= begin[i] + size[i] <= Di for i in [0, n]
例如:
t = tf.constant([[[1, 1, 1], [2, 2, 2]],
[[3, 3, 3], [4, 4, 4]],
[[5, 5, 5], [6, 6, 6]]])
tf.slice(t, [1, 0, 0], [1, 1, 3]) # [[[3, 3, 3]]]
tf.slice(t, [1, 0, 0], [1, 2, 3]) # [[[3, 3, 3],
# [4, 4, 4]]]
tf.slice(t, [1, 0, 0], [2, 1, 3]) # [[[3, 3, 3]],
# [[5, 5, 5]]]
相關用法
- Python tf.summary.scalar用法及代碼示例
- Python tf.strings.substr用法及代碼示例
- Python tf.strings.reduce_join用法及代碼示例
- Python tf.sparse.cross用法及代碼示例
- Python tf.sparse.mask用法及代碼示例
- Python tf.strings.regex_full_match用法及代碼示例
- Python tf.sparse.split用法及代碼示例
- Python tf.strings.regex_replace用法及代碼示例
- Python tf.signal.overlap_and_add用法及代碼示例
- Python tf.strings.length用法及代碼示例
- Python tf.strided_slice用法及代碼示例
- Python tf.sparse.to_dense用法及代碼示例
- Python tf.strings.bytes_split用法及代碼示例
- Python tf.summary.text用法及代碼示例
- Python tf.shape用法及代碼示例
- Python tf.sparse.expand_dims用法及代碼示例
- Python tf.signal.frame用法及代碼示例
- Python tf.scatter_nd用法及代碼示例
- Python tf.sparse.maximum用法及代碼示例
- Python tf.signal.linear_to_mel_weight_matrix用法及代碼示例
注:本文由純淨天空篩選整理自tensorflow.org大神的英文原創作品 tf.slice。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。
