TensorFlow2-索引与切片(6)

索引与切片

Indexing 索引

  • Basic indexing
    • [idx][idx][idx]
  • Same with Numpy
  • [idx, idx,…]
  • start:end
  • start🔚step
import tensorflow as tf
import numpy as np

Basic indexing

  • 索引方式通用
  • 单一
a=tf.ones([1,5,5,3])
a.shape
TensorShape([1, 5, 5, 3])
a[0][0]
<tf.Tensor: shape=(5, 3), dtype=float32, numpy=
array([[1., 1., 1.],
       [1., 1., 1.],
       [1., 1., 1.],
       [1., 1., 1.],
       [1., 1., 1.]], dtype=float32)>
a[0][0][0]
<tf.Tensor: shape=(3,), dtype=float32, numpy=array([1., 1., 1.], dtype=float32)>
a[0][0][0][2]
<tf.Tensor: shape=(), dtype=float32, numpy=1.0>

Numpy-style indexing

a=tf.random.normal([4,28,28,3])
a[1].shape
TensorShape([28, 28, 3])
a[1,2].shape
TensorShape([28, 3])
a[1,2,3].shape
TensorShape([3])
a[1,2,3]
<tf.Tensor: shape=(3,), dtype=float32, numpy=array([-0.1556891 , -0.03499719,  1.491767  ], dtype=float32)>
a[1,2,3,2].shape
TensorShape([])
a[1,2,3,2]
<tf.Tensor: shape=(), dtype=float32, numpy=1.491767>

start:end

a=tf.range(10)
a
<tf.Tensor: shape=(10,), dtype=int32, numpy=array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], dtype=int32)>
a[0]
<tf.Tensor: shape=(), dtype=int32, numpy=0>
a[-1:]
<tf.Tensor: shape=(1,), dtype=int32, numpy=array([9], dtype=int32)>
a[-2:]
<tf.Tensor: shape=(2,), dtype=int32, numpy=array([8, 9], dtype=int32)>
a[:2]
<tf.Tensor: shape=(2,), dtype=int32, numpy=array([0, 1], dtype=int32)>
a[:-1]
<tf.Tensor: shape=(9,), dtype=int32, numpy=array([0, 1, 2, 3, 4, 5, 6, 7, 8], dtype=int32)>

Indexing by 综合使用

a=tf.random.normal([4,28,28,3])
a.shape
TensorShape([4, 28, 28, 3])

后置切片

a[0].shape
TensorShape([28, 28, 3])
a[0,:,:,:].shape
TensorShape([28, 28, 3])
a[0,1,:,:].shape
TensorShape([28, 3])
a[0,1,1,:].shape
TensorShape([3])

前置切片

a[:,:,:,:].shape
TensorShape([4, 28, 28, 3])
a[:,:,:,0].shape
TensorShape([4, 28, 28])
a[:,:,:,2].shape
TensorShape([4, 28, 28])

前后切片

a[:,0,:,:].shape
TensorShape([4, 28, 3])

Indexing by ::

  • start🔚step
  • ::step
a=tf.random.normal([4,28,28,3])
a.shape
TensorShape([4, 28, 28, 3])
# 第一维度[0-2]的样本
a[0:2,:,:,:].shape
TensorShape([2, 28, 28, 3])
# 2、3维度[0-28]间隔2个取值
a[:,0:28:2,0:28:2,:].shape
TensorShape([4, 14, 14, 3])
# 2、3维度[0-14]取值
a[:,:14,:14,:].shape
TensorShape([4, 14, 14, 3])
# 2、3维度[14:]取值
a[:,14:,14:,:].shape
TensorShape([4, 14, 14, 3])
# 2、3维度[0-28]间隔2个取值
a[:,::2,::2,:].shape
TensorShape([4, 14, 14, 3])

::-1 倒序

a=tf.range(4)
a
<tf.Tensor: shape=(4,), dtype=int32, numpy=array([0, 1, 2, 3], dtype=int32)>
a[::-1]
<tf.Tensor: shape=(4,), dtype=int32, numpy=array([3, 2, 1, 0], dtype=int32)>
a[::-2]
<tf.Tensor: shape=(2,), dtype=int32, numpy=array([3, 1], dtype=int32)>
a[2::-2]
<tf.Tensor: shape=(2,), dtype=int32, numpy=array([2, 0], dtype=int32)>

… 省略号的方式

a=tf.random.normal([2,4,28,28,3])
a.shape
TensorShape([2, 4, 28, 28, 3])
a[0].shape
TensorShape([4, 28, 28, 3])
a[0,:,:,:,:].shape
TensorShape([4, 28, 28, 3])
a[0,...].shape
TensorShape([4, 28, 28, 3])
a[0,:,...].shape
TensorShape([4, 28, 28, 3])
a[:,:,:,:,0].shape
TensorShape([2, 4, 28, 28])
a[...,0].shape
TensorShape([2, 4, 28, 28])
a[0,...,2].shape
TensorShape([4, 28, 28])
a[1,0,...,2].shape
TensorShape([28, 28])

Selective Indexing 选择性索引

  • tf.gather
  • tf.gather_nd
  • tf.boolean_ma

tf.gather

  • data: [classes, students, subjects]
    • [4, 35, 8]
a=tf.random.normal([4,35,8])
a.shape
TensorShape([4, 35, 8])
# axis:选择的维度
# indices:索引号
tf.gather(a, axis=0,indices=[2,3]).shape
TensorShape([2, 35, 8])
a[2:4].shape
TensorShape([2, 35, 8])
tf.gather(a, axis=0,indices=[2,1,3,0]).shape
TensorShape([4, 35, 8])
tf.gather(a, axis=1,indices=[2,3,7,9,16]).shape
TensorShape([4, 5, 8])
tf.gather(a, axis=2,indices=[2,3,7]).shape
TensorShape([4, 35, 3])

tf.gather_nd

  • data: [classes, students, subjects]
  • What if sample several students and their several subjects?如果抽样几个学生和他们的几个科目呢?
  • 𝑎𝑎 = 𝑡𝑓. 𝑔𝑎𝑡ℎ𝑒𝑟(𝑎, 𝑎𝑥𝑖𝑠,[𝑠𝑒𝑣𝑒𝑟𝑎𝑙 𝑠𝑡𝑢𝑑𝑒𝑛𝑡𝑠])
  • 𝑎𝑎𝑎 = 𝑡𝑓. 𝑔𝑎𝑡ℎ𝑒𝑟(𝑎𝑎, 𝑎𝑥𝑖𝑠,[𝑠𝑒𝑣𝑒𝑟𝑎𝑙 𝑠𝑢𝑏𝑗𝑒𝑐𝑡𝑠])
a=tf.random.normal([4,35,8])
a.shape
TensorShape([4, 35, 8])
aa=tf.gather(a,axis=1,indices=[1,3,5,7,8,12,16])
aa.shape
TensorShape([4, 7, 8])
aaa=tf.gather(aa,axis=2,indices=[0,1,2])
aaa.shape
TensorShape([4, 7, 3])

tf.gather_nd

  • data: [classes, students, subjects]
  • What if sample several (classes and students)?如果抽样几个(班级和学生)呢
  • 𝑓𝑜𝑟 𝑖𝑛𝑠𝑡𝑎𝑛𝑐𝑒:
    • [class1_student1, class2_studnet2, class3_student3, class4_student4]
    • → [4, 8]
a=tf.random.normal([4,35,8])
a.shape
TensorShape([4, 35, 8])
# [[0]] 第0个班级
# a[0]  -> [35,8]
tf.gather_nd(a, [0]).shape
TensorShape([35, 8])
# 第0个班级,第1学生
# a[0,1]  -> [8]
# a[0][1] -> [8]
tf.gather_nd(a, [0,1]).shape
TensorShape([8])
# 第0个班级,第1学生,第2门课
# a[0,1,2]  -> []
# a[0][1][2] -> []
tf.gather_nd(a, [0,1,2]).shape
TensorShape([])
tf.gather_nd(a, [0,1,2])
<tf.Tensor: shape=(), dtype=float32, numpy=0.012457304>
tf.gather_nd(a, [[0,1,2]]).shape
TensorShape([1])
# a[0][1][2] -> 标量
# 返长度为1的数据
tf.gather_nd(a, [[0,1,2]])
<tf.Tensor: shape=(1,), dtype=float32, numpy=array([0.0124573], dtype=float32)>

更复杂的使用方式

# [a[0][0],a[1][1]]
tf.gather_nd(a, [[0,0],[1,1]]).shape
TensorShape([2, 8])
# [a[0][0],a[1][1],a[2][2]]
tf.gather_nd(a, [[0,0],[1,1],[2,2]]).shape
TensorShape([3, 8])
# [a[0][0][0],a[1][1][1],a[2][2][2]]
tf.gather_nd(a, [[0,0,0],[1,1,1],[2,2,2]]).shape
TensorShape([3])
# [[a[0][0][0],a[1][1][1],a[2][2][2]]]
tf.gather_nd(a, [[[0,0,0],[1,1,1],[2,2,2]]]).shape
TensorShape([1, 3])

tf.gather_nd

  • recommended ind
  • [[0], [1],…]
  • [[0,0], [1,1],…]
  • [[0,0,0], [1,1,1],…]
  • 推荐2层的写法:[[0]]

tf.boolean_mask

a=tf.random.normal([4,28,28,3])
a.shape
TensorShape([4, 28, 28, 3])
tf.boolean_mask(a, mask=[True, True,False,False]).shape
TensorShape([2, 28, 28, 3])
tf.boolean_mask(a, mask=[True, True,False],axis=3).shape
TensorShape([4, 28, 28, 2])
a=tf.ones([2,3,4])
# [True,False,False]  第1维;第2维;第3维

tf.boolean_mask(a, mask=[[True,False,False],[False, True,True]])
<tf.Tensor: shape=(3, 4), dtype=float32, numpy=
array([[1., 1., 1., 1.],
       [1., 1., 1., 1.],
       [1., 1., 1., 1.]], dtype=float32)>
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
TensorFlow中的切片操作是用来截取张量中的特定部分。通过指定开始和结束索引,可以获取张量中的子集。在TensorFlow中,切片操作使用冒号(:)作为分隔符,并且开始索引是包含在切片内的,而结束索引是不包含在切片内的。 下面是一个使用切片操作的例子: import tensorflow as tf a = tf.range(10) # 创建一个长度为10的张量 b = a[2:6] # 取出索引2到5的元素 print(b) # 输出 [2, 3, 4, 5] 通过这个例子,我们可以看到,使用切片操作可以轻松地获取张量中的指定部分。在这个例子中,我们从索引2开始到索引5结束,取出了4个元素。 除了基本的切片操作,TensorFlow还提供了一些高级的切片函数,如tf.gather_nd()。tf.gather_nd()函数可以根据给定的索引值从张量中抽取指定的数据组合。 下面是一个使用tf.gather_nd()函数的例子: import tensorflow as tf a = tf.range(10) # 创建一个长度为10的张量 b = tf.gather_nd(a, [0, 1]) # 从张量a中抽取索引为[0, 1]的元素 c = tf.gather_nd(a, [0, 1, 2]) # 从张量a中抽取索引为[0, 1, 2]的元素 d = tf.gather_nd(a, [[0, 1, 2]]) # 从张量a中抽取索引为[[0, 1, 2]]的元素 print(b.shape) # 输出 (1,) print(c.shape) # 输出 (1,) print(d.shape) # 输出 (1,) 通过这个例子,我们可以看到,tf.gather_nd()函数可以按照给定的索引值从张量中抽取指定的数据组合。在这个例子中,我们分别抽取了索引为[0, 1]、[0, 1, 2]和[[0, 1, 2]]的元素,并打印了它们的形状。 这就是TensorFlow切片操作的用法和示例。希望对你有帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值