TensorFlow索引与切片语句
生活随笔
收集整理的這篇文章主要介紹了
TensorFlow索引与切片语句
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
學習課程
1.Basic indexing
2.Numpy-style indexing
a = tf.random.normal([4,28,28,3]) #4張照片 a[1].shape #第2張照片的信息維度,TensorShape([28,28,3]) a[1,2].shape #第二張照片第三行的信息維度,TensorShape([28,3]) a[1,2,3].shape #第二張照片第三行第四列的信息維度TensorShape([3]) a[1,2,3,2].shape #第二張照片第三行第四列地三層的信息維度TensorShape([])3.start:end #左閉右開
a=tf.range(10) a[-1:] #結果為[9] a[-2:] #結果為[8,9] a[1:] #結果為[1,2,3,4,5,6,7,8,9] a[:-1] #結果為[0,1,2,3,4,5,6,7,8]4.Indexing by :
a = tf.random.normal([4,28,28,3]) #4張照片 a[0].shape #第1張照片的信息維度,TensorShape([28,28,3]) a[0,:,:,:].shape #第1張照片的信息維度,TensorShape([28,28,3]) a[0,1,:,:].shape #第1張照片第1行的信息維度,TensorShape([28,3]) a[:,:,:,2].shape #第三層信息維度,TensorShape([4,28,28]) a[:,0,:,:].shape #第1行信息維度,TensorShape([4,28,3])5.Indexing by ::
start: end:step
::step
6.::-1 #逆序
a=tf.range(4) #[0,1,2,3] a[::-1] #[3,2,1,0] a[::-2] #[3,1] a[2::-2] #[2,0]7.…
a = tf.random.normal([2,4,28,28,3]) #,2個tasts,4張照片 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,:,:,:,0].shape #TensorShape([4,28,28]) a[0,...,0].shape #TensorShape([4,28,28])8.tf.gather
a=tf.random.normal([4,35,8]) #4個班級,每個班級35個學生,每個學生8科課程 tf.gather(a,axis=0,indices[2,3]).shape #TensorShape([2,35,8]),選第3和第4個班級的所有數據 a[2:4].shape #TensorShape([2,35,8]),選第3和第4個班級的所有數據 tf.gather(a,axis=0,indices[3,2,4,1]).shape #TensorShape([4,35,8]),所有班級所有數據,但班級查看順序不一樣 tf.gather(a,axis=1,indices[2,4,7]).shape #TensorShape([4,3,8]),所有班級的第3,5,8號學生的所有科目成績9.tf.gather_nd
略
10.tf.boolean_mask
略
總結
以上是生活随笔為你收集整理的TensorFlow索引与切片语句的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: TensorFlow创建tensor语句
- 下一篇: TensorFlow维度变换函数语句