如下所示:
1
2
3
4
5
6
7
8
9
10
|
import tensorflow as tf a = tf.constant([[[ 1 , 2 , 3 , 4 ],[ 4 , 5 , 6 , 7 ],[ 7 , 8 , 9 , 10 ]], [[ 11 , 12 , 13 , 14 ],[ 20 , 21 , 22 , 23 ],[ 15 , 16 , 17 , 18 ]]]) print (a.shape) b,c = tf.split(a, 2 , 0 ) #參數1、張量 2、獲得的切片數 3、切片的維度 將兩個切片分別賦值給b,c print (b.shape) print (c.shape with tf.Session() as sess: #查看運行結果 print (sess.run(b)) print (sess.run(c)) |
輸出結果為
1
2
3
4
5
6
7
8
9
|
( 2 , 3 , 4 ) ( 1 , 3 , 4 ) ( 1 , 3 , 4 ) [[[ 1 2 3 4 ] [ 4 5 6 7 ] [ 7 8 9 10 ]]] [[[ 11 12 13 14 ] [ 20 21 22 23 ] [ 15 16 17 18 ]]] |
注意到此時b,c均為三維張量數據,若想轉換為二維數組,可使用tf.reshape命令
1
2
3
4
5
|
d = tf.reshape(b,[ 3 , 4 ]) print (d.shape) #output ( 3 , 4 ) |
以上這篇tensorflow實現對張量數據的切片操作方式就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持服務器之家。
原文鏈接:https://blog.csdn.net/ljxopencv/article/details/90523839