tf.concat

    技术2025-10-07  9

    tf.concat

    https://github.com/tensorflow/docs/tree/r1.4/site/en/api_docs/api_docs/python/tf site/en/api_docs/api_docs/python/tf/concat.md

    concat( values, axis, name='concat' )

    Defined in tensorflow/python/ops/array_ops.py. See the guide: Tensor Transformations > Slicing and Joining

    Concatenates tensors along one dimension. 将张量沿一个维度串联。

    Concatenates the list of tensors values along dimension axis. If values[i].shape = [D0, D1, ... Daxis(i), ...Dn], the concatenated result has shape 沿着维度轴 axis 连接张量值的列表。如果 values[i].shape = [D0, D1, ... Daxis(i), ...Dn],则连接的结果具有如下形状:

    [D0, D1, ... Raxis, ...Dn]

    where

    Raxis = sum(Daxis(i))

    That is, the data from the input tensors is joined along the axis dimension. 也就是说,来自输入张量的数据沿着 axis维度连接。

    The number of dimensions of the input tensors must match, and all dimensions except axis must be equal. 输入张量的维数必须匹配,除 axis 外的所有维度必须相等。

    For example:

    t1 = [[1, 2, 3], [4, 5, 6]] t2 = [[7, 8, 9], [10, 11, 12]] tf.concat([t1, t2], 0) # [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]] tf.concat([t1, t2], 1) # [[1, 2, 3, 7, 8, 9], [4, 5, 6, 10, 11, 12]] # tensor t3 with shape [2, 3] # tensor t4 with shape [2, 3] tf.shape(tf.concat([t3, t4], 0)) # [4, 3] tf.shape(tf.concat([t3, t4], 1)) # [2, 6]

    对于一个二维矩阵,第 0 个维度代表最外层方括号所框下的子集,第 1 个维度代表内部方括号所框下的子集。维度越高,括号越小。 axis=0 代表在第 0 个维度拼接。 axis=1 代表在第 1 个维度拼接。 对于 [ [ ], [ ]] 和 [[ ], [ ]],低维拼接等于拿掉最外面括号,高维拼接是拿掉里面的括号 (保证其他维度不变)。tf.concat() 拼接的张量只会改变一个维度,其他维度是保存不变的。

    axis=-1 表示倒数第一个维度。对于三维矩阵拼接,axis=-1 等价于 axis=2,axis=-2 代表倒数第二个维度,axis=-2 等价于 axis=1。

    Note: If you are concatenating along a new axis consider using stack. 如果你在一个新的轴上连接,考虑使用堆栈。 E.g.

    tf.concat([tf.expand_dims(t, axis) for t in tensors], axis)

    can be rewritten as

    tf.stack(tensors, axis=axis)

    1. Args

    values: A list of Tensor objects or a single Tensor. axis: 0-D int32 Tensor. Dimension along which to concatenate. Must be in the range [-rank(values), rank(values)). name: A name for the operation (optional). (操作的名称 (可选)。)

    在 Python 中,axis 可以为负值。负轴 (axis) 被解释为从秩 (rank) 的末尾开始计数。

    2. Returns

    A Tensor resulting from concatenation of the input tensors. 从 input tensors 连接而成的 Tensor 结果。

    3. Example

    #!/usr/bin/env python # -*- coding: utf-8 -*- from __future__ import absolute_import from __future__ import print_function from __future__ import division import os import sys import numpy as np import tensorflow as tf sys.path.append(os.path.dirname(os.path.abspath(__file__))) current_directory = os.path.dirname(os.path.abspath(__file__)) print(16 * "++--") print("current_directory:", current_directory) print(16 * "++--") t1 = [[[1, 2], [3, 4]], [[5, 6], [7, 8]]] t2 = [[[9, 10], [11, 12]], [[13, 14], [15, 16]]] concat_data_1 = tf.concat([t1, t2], -1) concat_data_2 = tf.concat([t1, t2], 2) sess = tf.Session() with sess: print("concat_data_1:\n", sess.run(concat_data_1)) print(16 * "++--") print("concat_data_1:\n", sess.run(concat_data_1)) /usr/bin/python2.7 /home/strong/tensorflow_work/R2CNN_Faster-RCNN_Tensorflow/yongqiang.py ++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++-- current_directory: /home/strong/tensorflow_work/R2CNN_Faster-RCNN_Tensorflow ++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++-- 2019-08-20 14:11:19.961286: I tensorflow/core/platform/cpu_feature_guard.cc:137] Your CPU supports instructions that this TensorFlow binary was not compiled to use: SSE4.1 SSE4.2 AVX AVX2 FMA 2019-08-20 14:11:20.022024: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:892] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero 2019-08-20 14:11:20.022310: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1030] Found device 0 with properties: name: GeForce GTX 1080 major: 6 minor: 1 memoryClockRate(GHz): 1.7335 pciBusID: 0000:01:00.0 totalMemory: 7.92GiB freeMemory: 7.29GiB 2019-08-20 14:11:20.022321: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1120] Creating TensorFlow device (/device:GPU:0) -> (device: 0, name: GeForce GTX 1080, pci bus id: 0000:01:00.0, compute capability: 6.1) concat_data_1: [[[ 1 2 9 10] [ 3 4 11 12]] [[ 5 6 13 14] [ 7 8 15 16]]] ++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++-- concat_data_1: [[[ 1 2 9 10] [ 3 4 11 12]] [[ 5 6 13 14] [ 7 8 15 16]]] Process finished with exit code 0

    4. Example

    #!/usr/bin/env python # -*- coding: utf-8 -*- from __future__ import absolute_import from __future__ import print_function from __future__ import division import os import sys import numpy as np import tensorflow as tf sys.path.append(os.path.dirname(os.path.abspath(__file__))) current_directory = os.path.dirname(os.path.abspath(__file__)) print(16 * "++--") print("current_directory:", current_directory) print(16 * "++--") t1 = tf.constant([1, 2, 3]) t2 = tf.constant([4, 5, 6]) t1_expand = tf.expand_dims(tf.constant([1, 2, 3]), 1) t2_expand = tf.expand_dims(tf.constant([4, 5, 6]), 1) concat_1 = tf.concat([t1, t2], 0) concat_2 = tf.concat([t1_expand, t2_expand], 0) concat_3 = tf.concat([t1_expand, t2_expand], 1) sess = tf.Session() with sess: print("t1_expand:\n", sess.run(t1_expand)) print(16 * "++--") print("t2_expand:\n", sess.run(t2_expand)) print(16 * "++--") print("concat_1:\n", sess.run(concat_1)) print(16 * "++--") print("concat_2:\n", sess.run(concat_2)) print(16 * "++--") print("concat_3:\n", sess.run(concat_3)) /usr/bin/python2.7 /home/strong/tensorflow_work/R2CNN_Faster-RCNN_Tensorflow/yongqiang.py ++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++-- current_directory: /home/strong/tensorflow_work/R2CNN_Faster-RCNN_Tensorflow ++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++-- 2019-08-20 14:17:44.771383: I tensorflow/core/platform/cpu_feature_guard.cc:137] Your CPU supports instructions that this TensorFlow binary was not compiled to use: SSE4.1 SSE4.2 AVX AVX2 FMA 2019-08-20 14:17:44.838946: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:892] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero 2019-08-20 14:17:44.839184: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1030] Found device 0 with properties: name: GeForce GTX 1080 major: 6 minor: 1 memoryClockRate(GHz): 1.7335 pciBusID: 0000:01:00.0 totalMemory: 7.92GiB freeMemory: 7.29GiB 2019-08-20 14:17:44.839194: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1120] Creating TensorFlow device (/device:GPU:0) -> (device: 0, name: GeForce GTX 1080, pci bus id: 0000:01:00.0, compute capability: 6.1) t1_expand: [[1] [2] [3]] ++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++-- t2_expand: [[4] [5] [6]] ++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++-- concat_1: [1 2 3 4 5 6] ++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++-- concat_2: [[1] [2] [3] [4] [5] [6]] ++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++-- concat_3: [[1 4] [2 5] [3 6]] Process finished with exit code 0

    5. Example

    #!/usr/bin/env python # -*- coding: utf-8 -*- from __future__ import absolute_import from __future__ import print_function from __future__ import division import os import sys import numpy as np import tensorflow as tf sys.path.append(os.path.dirname(os.path.abspath(__file__))) current_directory = os.path.dirname(os.path.abspath(__file__)) print(16 * "++--") print("current_directory:", current_directory) print(16 * "++--") t1 = tf.constant([[0, 1, 2], [3, 4, 5]], dtype=np.float32) t2 = tf.constant([[6, 7, 8], [9, 10, 11]], dtype=np.float32) matrix0 = tf.concat([t1, t2], 0) matrix1 = tf.concat([t1, t2], 1) ops_shape0 = tf.shape(tf.concat([t1, t2], 0)) ops_shape1 = tf.shape(tf.concat([t1, t2], 1)) with tf.Session() as sess: input_t1 = sess.run(t1) print("input_t1.shape:", input_t1.shape) print('\n') input_t2 = sess.run(t2) print("input_t2.shape:", input_t2.shape) print('\n') output_t1 = sess.run(matrix0) print("output_t1.shape:", output_t1.shape) print("output_t1:\n", output_t1) print('\n') output_t2 = sess.run(matrix1) print("output_t2.shape:", output_t2.shape) print("output_t2:\n", output_t2) print('\n') output_shape0 = sess.run(ops_shape0) output_shape1 = sess.run(ops_shape1) print("output_shape0:", output_shape0) print("output_shape1:", output_shape1) /usr/bin/python2.7 /home/strong/tensorflow_work/R2CNN_Faster-RCNN_Tensorflow/yongqiang.py ++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++-- current_directory: /home/strong/tensorflow_work/R2CNN_Faster-RCNN_Tensorflow ++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++-- 2019-08-20 14:35:06.029910: I tensorflow/core/platform/cpu_feature_guard.cc:137] Your CPU supports instructions that this TensorFlow binary was not compiled to use: SSE4.1 SSE4.2 AVX AVX2 FMA 2019-08-20 14:35:06.097682: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:892] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero 2019-08-20 14:35:06.097926: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1030] Found device 0 with properties: name: GeForce GTX 1080 major: 6 minor: 1 memoryClockRate(GHz): 1.7335 pciBusID: 0000:01:00.0 totalMemory: 7.92GiB freeMemory: 7.29GiB 2019-08-20 14:35:06.097938: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1120] Creating TensorFlow device (/device:GPU:0) -> (device: 0, name: GeForce GTX 1080, pci bus id: 0000:01:00.0, compute capability: 6.1) input_t1.shape: (2, 3) input_t2.shape: (2, 3) output_t1.shape: (4, 3) output_t1: [[ 0. 1. 2.] [ 3. 4. 5.] [ 6. 7. 8.] [ 9. 10. 11.]] output_t2.shape: (2, 6) output_t2: [[ 0. 1. 2. 6. 7. 8.] [ 3. 4. 5. 9. 10. 11.]] output_shape0: [4 3] output_shape1: [2 6] Process finished with exit code 0

    6. Example

    #!/usr/bin/env python # -*- coding: utf-8 -*- from __future__ import absolute_import from __future__ import print_function from __future__ import division import os import sys import numpy as np import tensorflow as tf sys.path.append(os.path.dirname(os.path.abspath(__file__))) current_directory = os.path.dirname(os.path.abspath(__file__)) print(16 * "++--") print("current_directory:", current_directory) print(16 * "++--") t1 = tf.constant([[[0, 1, 2], [3, 4, 5], [6, 7, 8], [9, 10, 11]]], dtype=np.float32) t2 = tf.constant([[[12, 13, 14], [15, 16, 17], [18, 19, 20], [21, 22, 23]]], dtype=np.float32) matrix0 = tf.concat([t1, t2], 0) matrix1 = tf.concat([t1, t2], 1) matrix2 = tf.concat([t1, t2], 2) ops_shape0 = tf.shape(tf.concat([t1, t2], 0)) ops_shape1 = tf.shape(tf.concat([t1, t2], 1)) ops_shape2 = tf.shape(tf.concat([t1, t2], 2)) with tf.Session() as sess: input_t1 = sess.run(t1) print("input_t1.shape:", input_t1.shape) print('\n') input_t2 = sess.run(t2) print("input_t2.shape:", input_t2.shape) print('\n') output_t1 = sess.run(matrix0) print("output_t1.shape:", output_t1.shape) print("output_t1:", output_t1) print('\n') output_t2 = sess.run(matrix1) print("output_t2.shape:", output_t2.shape) print("output_t2:", output_t2) print('\n') output_t3 = sess.run(matrix2) print("output_t3.shape:", output_t3.shape) print("output_t3:", output_t3) print('\n') output_shape0 = sess.run(ops_shape0) output_shape1 = sess.run(ops_shape1) output_shape2 = sess.run(ops_shape2) print("output_shape0:", output_shape0) print("output_shape1:", output_shape1) print("output_shape2:", output_shape2) /usr/bin/python2.7 /home/strong/tensorflow_work/R2CNN_Faster-RCNN_Tensorflow/yongqiang.py ++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++-- current_directory: /home/strong/tensorflow_work/R2CNN_Faster-RCNN_Tensorflow ++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++-- 2019-08-20 14:39:34.651230: I tensorflow/core/platform/cpu_feature_guard.cc:137] Your CPU supports instructions that this TensorFlow binary was not compiled to use: SSE4.1 SSE4.2 AVX AVX2 FMA 2019-08-20 14:39:34.719667: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:892] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero 2019-08-20 14:39:34.719909: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1030] Found device 0 with properties: name: GeForce GTX 1080 major: 6 minor: 1 memoryClockRate(GHz): 1.7335 pciBusID: 0000:01:00.0 totalMemory: 7.92GiB freeMemory: 7.29GiB 2019-08-20 14:39:34.719920: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1120] Creating TensorFlow device (/device:GPU:0) -> (device: 0, name: GeForce GTX 1080, pci bus id: 0000:01:00.0, compute capability: 6.1) input_t1.shape: (1, 4, 3) input_t2.shape: (1, 4, 3) output_t1.shape: (2, 4, 3) output_t1: [[[ 0. 1. 2.] [ 3. 4. 5.] [ 6. 7. 8.] [ 9. 10. 11.]] [[12. 13. 14.] [15. 16. 17.] [18. 19. 20.] [21. 22. 23.]]] output_t2.shape: (1, 8, 3) output_t2: [[[ 0. 1. 2.] [ 3. 4. 5.] [ 6. 7. 8.] [ 9. 10. 11.] [12. 13. 14.] [15. 16. 17.] [18. 19. 20.] [21. 22. 23.]]] output_t3.shape: (1, 4, 6) output_t3: [[[ 0. 1. 2. 12. 13. 14.] [ 3. 4. 5. 15. 16. 17.] [ 6. 7. 8. 18. 19. 20.] [ 9. 10. 11. 21. 22. 23.]]] output_shape0: [2 4 3] output_shape1: [1 8 3] output_shape2: [1 4 6] Process finished with exit code 0
    Processed: 0.009, SQL: 9