1.合并
多个tensor的合并主要包括2个函数:tf.concat和tf.stack,他们的输出参数都是list of tensor。区别是:
- tf.concat是沿某一维度拼接shape相同的张量,拼接生成的新张量维度不会增加。
- tf.stack是在新的维度上拼接,拼接后维度加1。
import tensorflow as tf
a = tf.constant([[1,2,3],[4,5,6]])
b = tf.constant([[7,8,9],[10,11,12]])
ab1 = tf.concat([a,b],axis=0)
ab2 = tf.stack([a,b], axis=0)
sess = tf.Session()
print(sess.run(ab1))
print(sess.run(ab2))
print ab1
print ab2
结果:
[[ 1 2 3]
[ 4 5 6]
[ 7 8 9]
[10 11 12]]#ab1的值
[[[ 1 2 3]
[ 4 5 6]]
[[ 7 8 9]
[10 11 12]]]#ab2的值
Tensor("concat:0", shape=(4, 3), dtype=int32)#ab1的属性
Tensor("stack:0",