tensorflow从张量中提取一个切片
示例
请参阅文档以获取详细信息。tf.slice(input,begin,size)
参数:
input:张量
begin:每个维度的起始位置input
size:的每个维度的元素数input,使用-1包括所有剩余元素
块状切片:
# x has shape [2, 3, 2] x = tf.constant([[[1., 2.], [3., 4. ], [5. , 6. ]], [[7., 8.], [9., 10.], [11., 12.]]]) # Extracts x[0, 1:2, :] == [[[ 3., 4.]]] res = tf.slice(x, [0, 1, 0], [1, 1, -1])
使用负索引,检索第三维中的最后一个元素:
# Extracts x[0, :, -1:] == [[[2.], [4.], [6.]]] last_indice = x.get_shape().as_list()[2] - 1 res = tf.slice(x, [0, 1, last_indice], [1, -1, -1])