TensorFlow2.0中Tensor的索引与切片

构建数据

使用随机数据:

>>> import tensorflow as tf
>>> x = tf.random.normal([3, 5, 4, 2])	# 创建一组4维张量
>>> x
<tf.Tensor: id=11, shape=(3, 5, 4, 2), dtype=float32, numpy=
array([[[[ 2.023797  ,  2.1604726 ],
         [-2.0741608 , -0.04621543],
         [-0.39314505,  0.5395487 ],
         [-0.9819135 ,  1.0966431 ]],
         ...
         ...
         [[-0.43981767,  0.85973704],
         [ 0.00626672,  0.56414545],
         [-1.339878  ,  1.1833177 ],
         [ 0.7948731 , -0.18351097]]]], dtype=float32)>

索引

操作比较简单,直接使用 [] 来引用:

>>> x[0]	# 取0维度上的第一组数据
<tf.Tensor: id=15, shape=(5, 4, 2), dtype=float32, numpy=
array([[[ 2.023797  ,  2.1604726 ],
        [-2.0741608 , -0.04621543],
        [-0.39314505,  0.5395487 ],
        [-0.9819135 ,  1.0966431 ]],

       [[-1.8165376 ,  1.074334  ],
        [ 1.2004414 ,  2.202838  ],
        [-0.88942367, -2.0591717 ],
        [-1.1386424 ,  0.47814593]],

       [[-0.4635978 , -0.44835317],
        [ 1.4853488 , -0.12728298],
        [-1.4039013 ,  1.7855197 ],
        [-0.58983433,  0.7513566 ]],

       [[ 1.0006567 ,  0.42776355],
        [ 0.5909812 , -0.00761494],
        [-4.167632  , -0.81391484],
        [ 1.5311053 ,  0.0442026 ]],

       [[-1.0300825 , -0.3407836 ],
        [ 0.34273183,  0.61098814],
        [ 0.37355307, -1.2701924 ],
        [ 0.4935183 ,  1.0812984 ]]], dtype=float32)>
>>> x[0][0]
<tf.Tensor: id=23, shape=(4, 2), dtype=float32, numpy=
array([[ 2.023797  ,  2.1604726 ],
       [-2.0741608 , -0.04621543],
       [-0.39314505,  0.5395487 ],
       [-0.9819135 ,  1.0966431 ]], dtype=float32)>
>>> x[2][4]
<tf.Tensor: id=34, shape=(4, 2), dtype=float32, numpy=
array([[-0.43981767,  0.85973704],
       [ 0.00626672,  0.56414545],
       [-1.339878  ,  1.1833177 ],
       [ 0.7948731 , -0.18351097]], dtype=float32)>
>>> x[0][0][0]
<tf.Tensor: id=46, shape=(2,), dtype=float32, numpy=array([2.023797 , 2.1604726], dtype=float32)>

x[0][0]x[0] 中的第一组数据,x[0][0][0]x[0][0] 中的第一组数据,依此类推


切片

采用 [start : end : step] 的方式。start : end为含头不含尾的切片操作,表示从 start 位开始,一直到 end-1 位结束;加入步长 step 后,从 startend-1 间隔 step 取数

实现如下:

>>> x[1:3]	# 取0维度上第三、四组数据
<tf.Tensor: id=54, shape=(2, 5, 4, 2), dtype=float32, numpy=
array([[[[ 0.78193   ,  0.41572782],
         [-0.62721986, -0.6817407 ],
         [ 1.083634  ,  0.1193545 ],
         [-1.4487511 ,  0.2917125 ]],
         ...
         ...
         [[-0.43981767,  0.85973704],
         [ 0.00626672,  0.56414545],
         [-1.339878  ,  1.1833177 ],
         [ 0.7948731 , -0.18351097]]]], dtype=float32)>
>>> x[0, 1 : 3]    # 取0维度第一组数据,数据中1维度取第二、三组数据
<tf.Tensor: id=62, shape=(2, 4, 2), dtype=float32, numpy=
array([[[-1.8165376 ,  1.074334  ],
        [ 1.2004414 ,  2.202838  ],
        [-0.88942367, -2.0591717 ],
        [-1.1386424 ,  0.47814593]],

       [[-0.4635978 , -0.44835317],
        [ 1.4853488 , -0.12728298],
        [-1.4039013 ,  1.7855197 ],
        [-0.58983433,  0.7513566 ]]], dtype=float32)>
>>> x[1 : 2, 2 :, : 3, :]	# 取0维度第二、三组数据,数据中1维度取第三、四组数据,再2维度取一、二、三组数据,3维度取所有数据
<tf.Tensor: id=66, shape=(1, 3, 3, 2), dtype=float32, numpy=
array([[[[-0.45758048, -1.4702083 ],
         [ 0.06481762,  0.38248643],
         [ 1.0012771 , -0.06163676]],

        [[-0.10973785,  0.69076234],
         [-1.5624996 , -0.2788876 ],
         [-0.46190593, -0.46629223]],

        [[-0.76543164,  0.6476933 ],
         [-0.7069058 , -0.31542447],
         [ 0.88035417,  0.41730645]]]], dtype=float32)>

切片格式:

切片方式 含义
start : end : step 从 start 开始读取到 end(不包含 end),步长为 step
start : end 从 start 开始读取到 end(不包含 end),步长为 1
start : 从 start 开始读取完后续所有元素,步长为 1
start : : step 从 start 开始读取完后续所有元素,步长为 step
: end : step 从 0 开始读取到 end(不包含 end),步长为 step
: end 从 0 开始读取到 end(不包含 end),步长为 1
: : step 从头到尾读取,步长为 step
: : 读取所有元素
: 读取所有元素

猜你喜欢

转载自blog.csdn.net/weixin_44613063/article/details/103571787
今日推荐