调用格式:
tf.concat(
values,
axis,
name='concat'
)
axis=0代表在第0个维度拼接,axis=1代表在第一个维度拼接。
注意:tf.concat()拼接的张量只会改变一个维度,其他维度是保存不变的。比如两个shape为[2,3]的矩阵拼接,要么通过axis=0变成[4,3],要么通过axis=1变成[2,6]。改变的维度索引对应axis的值。
举例如下:
import tensorflow as tf
tfe = tf.contrib.eager
tfe.enable_eager_execution()
t1 = [[1, 2, 3], [4, 5, 6]]
t2 = [[7, 8, 9], [10, 11, 12]]
t3 = tf.concat([t1, t2], 0)
t4 = tf.concat([t1, t2], 1)
print(t3)
print(t4)
输出:
tf.Tensor(
[[ 1 2 3]
[ 4 5 6]
[ 7 8 9]
[10 11 12]], shape=(4, 3), dtype=int32)
tf.Tensor(
[[ 1 2 3 7 8 9]
[ 4 5 6 10 11 12]], shape=(2, 6), dtype=int32)
Process finished with exit code 0