tf.stack和tf.unstack
1 import tensorflow as tf 2 3 a = tf.constant([1,2,3]) 4 b = tf.constant([4,5,6]) 5 6 c1 = tf.stack([a,b],axis=0) 7 c2 = tf.stack([a,b],axis=1) 8 #take c2 for example showing results of unstack 9 d1 = tf.unstack(c2,axis=0) # Return: The list of Tensor objects unstacked from value. 10 d2 = tf.unstack(c2,axis=1) 11 12 with tf.Session() as sess: 13 print('c1(with shape:{}):\n{}'.format(c1.shape, sess.run(c1))) 14 print('c2(with shape:{}):\n{}'.format(c2.shape, sess.run(c2))) 15 16 print('d1(with length:{}):\n{}'.format(len(d1), sess.run(d1))) 17 print('d2(with length:{}):\n{}'.format(len(d2), sess.run(d2)))
运行结果:
官方API:
https://www.tensorflow.org/api_docs/python/tf/stack
https://www.tensorflow.org/api_docs/python/tf/unstack