TensorFlow2.0:索引和切片(1)

本文详细介绍了在TensorFlow中使用不同方式对张量进行索引和切片操作的方法,包括基本索引、Numpy风格索引、start:end切片及start:end:step切片等,展示了如何灵活地选取张量中的数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

**

一 基本的索引方式

**
给定每个维度的索引,直接进行索引

In [1]: import tensorflow as tf                                                 
   
In [2]: import numpy as np                                                      

In [3]: a = tf.ones([1,5,5,3])                                                  

In [4]: a[0][0]                                                                 
Out[4]: 
<tf.Tensor: id=10, shape=(5, 3), dtype=float32, numpy=
array([[1., 1., 1.],
       [1., 1., 1.],
       [1., 1., 1.],
       [1., 1., 1.],
       [1., 1., 1.]], dtype=float32)>

In [5]: a[0][0][0]                                                              
Out[5]: <tf.Tensor: id=23, shape=(3,), dtype=float32, numpy=array([1., 1., 1.], dtype=float32)>

In [6]: a[0][0][0][2]                                                           
Out[6]: <tf.Tensor: id=40, shape=(), dtype=float32, numpy=1.0>

**

二 Numpy风格索引

**
采用 [1,2,3] 的形式进行索引,较 [1][2][3] 更加简洁,代码的可读性强

In [7]: a = tf.random.normal([4,28,28,3])                                       

In [8]: a[1].shape                                                              
Out[8]: TensorShape([28, 28, 3])

In [9]: a[1,2].shape                                                            
Out[9]: TensorShape([28, 3])

In [10]: a[1,2,3].shape                                                         
Out[10]: TensorShape([3])

In [11]: a[1,2,3,2].shape                                                       
Out[11]: TensorShape([])

**

三 start:end形式的切片操作

**

--start : end为含头不含尾的切片操作,表示从start位开始,一直到end-1位结束.
--[-1:]从-1位(即最后一位开始)一直到最后一位,就是最后一位
--[-2:]从-2位(即倒数第二位开始)一直到最后一位,就是最后两位
--[2:10]从第2位开始(默认为0位开始)一直到第9位结束
--[:2]从开头一直到第1位结束,即开头前两位
--[:-1]从开头一直到末尾,但未包含末尾最后一位
In [12]: a = tf.range(10)                                                       

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

In [14]: a[-1:]#start:end = -1:末尾 = 最后一位                                  
Out[14]: <tf.Tensor: id=72, shape=(1,), dtype=int32, numpy=array([9], dtype=int32)>

In [15]: a[-2:]#start:end = -2:末尾 = 最后两位                                  
Out[15]: <tf.Tensor: id=77, shape=(2,), dtype=int32, numpy=array([8, 9], dtype=int32)>

In [16]: a[:2]#start:end = 开头:2 = 前面两位                                    
Out[16]: <tf.Tensor: id=82, shape=(2,), dtype=int32, numpy=array([0, 1], dtype=int32)>

In [17]: a[:-1]#start:end = 开头:-1 = 除最后一位的所有                          
Out[17]: <tf.Tensor: id=87, shape=(9,), dtype=int32, numpy=array([0, 1, 2, 3, 4, 5, 6, 7, 8], dtype=int32)>
In [1]: import tensorflow as tf                                                 

In [2]: import numpy as np                                                      

In [3]: a = tf.random.normal([4,28,28,3],mean=1,stddev=1)                       

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

In [5]: a[0].shape   # = a[0,:,:,:].shape                                                      
Out[5]: TensorShape([28, 28, 3])

In [6]: a[0,:,:,:].shape                                                        
Out[6]: TensorShape([28, 28, 3])

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

In [8]: a[:,:,:,0].shape                                                        
Out[8]: TensorShape([4, 28, 28])

In [9]: a[:,:,:,2].shape                                                        
Out[9]: TensorShape([4, 28, 28])

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

**

四 start : end : step形式的切片操作

**
–start : end : step为含头不含尾的切片操作,表示从start位开始,一直到end-1位结束,其步长为step

In [1]: import tensorflow as tf                                                 
 
In [2]: a = tf.random.normal([4,28,28,3],mean=1,stddev=1)                       

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

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

In [5]: a[:,0:28:2,0:28:2,:].shape                                              
Out[5]: TensorShape([4, 14, 14, 3])

In [6]: a[:,:14,:14,:].shape                                                    
Out[6]: TensorShape([4, 14, 14, 3])

In [7]: a[:,14:,14:,:].shape                                                    
Out[7]: TensorShape([4, 14, 14, 3])

In [8]: a[:,::2,::2,:].shape                                                    
Out[8]: TensorShape([4, 14, 14, 3])

–: : -1 默认从末尾逆序采样到开头,其步长为1
–: : -2 默认从末尾逆序采样到开头,其步长为2
–2: : -2 从第2位逆序采样到开头,其步长为2
–A:B: -2 从位置A逆序采样到第位,其步长为2

In [9]: a = tf.range(5)                                                         

In [10]: a                                                                      
Out[10]: <tf.Tensor: id=29, shape=(5,), dtype=int32, numpy=array([0, 1, 2, 3, 4], dtype=int32)>

In [11]: a[::-1]#从默认的末尾逆序采样到默认的开头,其步长为1                     
Out[11]: <tf.Tensor: id=34, shape=(5,), dtype=int32, numpy=array([4, 3, 2, 1, 0], dtype=int32)>

In [12]: a[::-2]#从默认的末尾逆序采样的默认的开头,其步长为2                     
Out[12]: <tf.Tensor: id=39, shape=(3,), dtype=int32, numpy=array([4, 2, 0], dtype=int32)>

In [13]: a[2::-2]#从第2位开始逆序采样的默认的开头,其步长为2                     
Out[13]: <tf.Tensor: id=44, shape=(2,), dtype=int32, numpy=array([2, 0], dtype=int32)>

In [14]: a[3:1:-2]#从第3位开始逆序采样的第1为,其步长为2                         
Out[14]: <tf.Tensor: id=49, shape=(1,), dtype=int32, numpy=array([3], dtype=int32)>

–…切片

In [1]: import tensorflow as tf                                                 

In [2]: a = tf.random.normal([2,4,28,28,3],mean=1,stddev=1)                     

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

In [4]: a[0].shape    # =a[0,:,:,:,:].shape    = a[0,...].shape                                                 
Out[4]: TensorShape([4, 28, 28, 3])

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

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

In [7]: a[:,:,:,:,0].shape                                                      
Out[7]: TensorShape([2, 4, 28, 28])

In [8]: a[...,0].shape                                                          
Out[8]: TensorShape([2, 4, 28, 28])

In [9]: a[0,...,2].shape                                                        
Out[9]: TensorShape([4, 28, 28])

In [10]: a[1,0,...,0].shape                                                     
Out[10]: TensorShape([28, 28])
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值