tf.concat

15515 ワード

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. テンソルを1つの次元に沿って直列に接続します.
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]

2 Dマトリクスの場合、0番目の次元は最外層の角カッコで囲まれたサブセットを表し、1番目の次元は内部の角カッコで囲まれたサブセットを表します.次元が高いほどかっこが小さくなります.axis=0は、0次元の接合を表します.axis=1は、1次元目の接合を表します.[[],[]],[],[],[]]では,低次元接合は最も外側の括弧を取り除くことに等しく,高次元接合は中の括弧を取り除く(他の次元が変わらないことを保証する).tf.concat()接合のテンソルは1つの次元しか変更されず、他の次元は保存されません.
axis=-1は、最後から最初の次元を表します.3 Dマトリクス接合の場合、axis=−1はaxis=2に等価であり、axis=−2は逆数の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)はランクの末尾からカウントされると解釈される.
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:
", sess.run(concat_data_1)) print(16 * "++--") print("concat_data_1:
", 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:
", sess.run(t1_expand)) print(16 * "++--") print("t2_expand:
", sess.run(t2_expand)) print(16 * "++--") print("concat_1:
", sess.run(concat_1)) print(16 * "++--") print("concat_2:
", sess.run(concat_2)) print(16 * "++--") print("concat_3:
", 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('
') input_t2 = sess.run(t2) print("input_t2.shape:", input_t2.shape) print('
') output_t1 = sess.run(matrix0) print("output_t1.shape:", output_t1.shape) print("output_t1:
", output_t1) print('
') output_t2 = sess.run(matrix1) print("output_t2.shape:", output_t2.shape) print("output_t2:
", output_t2) print('
') 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('
') input_t2 = sess.run(t2) print("input_t2.shape:", input_t2.shape) print('
') output_t1 = sess.run(matrix0) print("output_t1.shape:", output_t1.shape) print("output_t1:", output_t1) print('
') output_t2 = sess.run(matrix1) print("output_t2.shape:", output_t2.shape) print("output_t2:", output_t2) print('
') output_t3 = sess.run(matrix2) print("output_t3.shape:", output_t3.shape) print("output_t3:", output_t3) print('
') 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