tensorflow切片

 

 

 

#gather相当于收集
#data[classes,studets,objects] #班级、学生、课程
a=[4,35,8]
tf.gather(a,axis=0,indices=[2,3]).shape#axis取那个维度0表示classes班级,indices获取班级具体索引[2,3]
#[2,35,8] #表示从a获取第一列的2-3俩个班级,35人,没人8门课

#对第axis个维度,进行采样,可以乱序
tf.gather(a,axis=0,indices=[2,1,3,0]).shape
tf.gather(a,axis=1,indices=[2,3,7,9,16]).shape
tf.gather(a,axis=2,indices=[2,3,7]).shape

#aa = tf.gather(a,axis,[several students]]
#aaa = tf.gather(aa,axis,[several subjects]]

tf.gather_nd(a,[0]).shape #第0号班级的学生和课程
tf.gather_nd(a,[0,1]).shape# 0号班级的第一号学生的课程
tf.gather_nd(a,[0,1,2]).shape #0号班级,第一个学生的第2门课程,就是[]
tf.gather_nd(a,[[0,1,2]]).shape#[1]

 

 

 

 

 

 

 

 

 

 

#逆序切片(-x~-1)
#正序切片(0~x)
a = tf.range(10)
print(a[-1:]) #这是9
print(a[-2:])#8和9
print(a[:2])#0和1
print(a[:-1])#[0,1,2,3,4,5,6,7,8]
#冒号:表示维度都取
a.shape#([4,28,28,3])
a[0]#([28,28,3])
a[0,:,:,:].shape#([28,28,3])

a[:,:,:,0].shape#[4,28,28,1]去R通道
a[:,:,:,2].shape#[4,28,28,1]取B通道

#start:end:step或者::step(开始:结束:步长)
a[:,0,:,:].shape#[4,28,3]
a[:,:14,:14,3].shape#[4,14,14,3]
a[:,14:,14:,:].shape#[4,14,14,3]
a[:,::2,::2,:].shape#[4,14,14,3]

a[:,0:28:2,0:28:2,:].shape#[4,14,14,3]

#::实现一个倒序的功能
a = tf.range(4)#[0,1,2,3]
a[::-1]#[3,2,1,0]倒序
a[::-2]#[3,1]
a[2::-2]#[2,0]

#介绍...三点号的用法
a = tf.random.normal([2,4,28,28,3])

a[0,:,:,:,:]#[4,28,28,3]
a[0,...].shape#[4,28,28,3]

a[:,:,:,:,0].shape#[2,4,28,28]
a[...,0].shape#[2,4,28,28]

a[0,...,2].shape#[4,28,28]
a[1,0,...,0].shape#[28,28]
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值