切片
切片的方式主要有:
[start: end]:从tensor的开始位置到结束位置的数据切片;
[start :end :step]或者[::step]:从tensor的开始位置到结束位置每隔step的数据切片;
[::-1]:负数表示倒序切片;
‘...’:任意长。
代码:
创建一个4维tensor。tensor包含4张图片,每张图片的大小为1001003
tensor_h = tf.random.normal([4,100,100,3])
tensor_h
输出:
<tf.Tensor: shape=(4, 100, 100, 3), dtype=float32, numpy=
array([[[[ 1.68444023e-01, -7.46562362e-01, -4.34964240e-01],
[-4.69263226e-01, 6.26460612e-01, 1.21065331e+00],
[ 7.21675277e-01, 4.61057723e-01, -9.20868576e-01],
...,
代码:
取出第一张图片
tensor_h[0,:,:,:]
输出:
<tf.Tensor: shape=(100, 100, 3), dtype=float32, numpy=
array([[[ 1.68444023e-01, -7.46562362e-01, -4.34964240e-01],
[-4.69263226e-01, 6.26460612e-01, 1.21065331e+00],
[ 7.21675277e-01, 4.61057723e-01, -9.20868576e-01],
...,
代码:
每两张图片取出一张的切片
tensor_h[::2,...]
输出:
<tf.Tensor: shape=(2, 100, 100, 3), dtype=float32, numpy=
array([[[[ 1.68444023e-01, -7.46562362e-01, -4.34964240e-01],
[-4.69263226e-01, 6.26460612e-01, 1.21065331e+00],
[ 7.21675277e-01, 4.61057723e-01, -9.20868576e-01],
...,
代码:
倒序切片
tensor_h[::-1]
输出:
<tf.Tensor: shape=(4, 100, 100, 3), dtype=float32, numpy=
array([[[[-1.70684665e-01, 1.52386248e+00, -1.91677585e-01],
[-1.78917408e+00, -7.48436213e-01, 6.10363662e-01],
[ 7.64770031e-01, 6.06725179e-02, 1.32704067e+00],
...,