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))
输出结果为
(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命令
d=tf.reshape(b,[3,4])
print(d.shape)
#output
(3, 4)