介绍
合并tensor(张量)
其操作与numpy.concatenate()相同
官方网址:
https://www.tensorflow.org/versions/r2.0/api_docs/python/tf/concat
语法
tf.concat(
values,
axis,
name='concat'
)
参数
- values:由tensor组成的list,或者一个tensor
- axis:轴,沿着哪个轴进行合并。
负数表示倒数第几个轴,axis=-1表示倒数第1个轴,对于二维来说,等价axis=1
例子
按0轴合并
t1 = [[0, 1, 2], [3, 4, 5]]
t2 = [[10, 11, 12], [13, 14, 15]]
tf.concat([t1, t2], 0)
执行结果:
<tf.Tensor: id=548, shape=(4, 3), dtype=int32, numpy=
array([[ 0, 1, 2],
[ 3, 4, 5],
[10, 11, 12],
[13, 14, 15]], dtype=int32)>
按1轴合并
tf.concat([t1, t2], 1)
执行结果:
<tf.Tensor: id=552, shape=(2, 6), dtype=int32, numpy=
array([[ 0, 1, 2, 10, 11, 12],
[ 3, 4, 5, 13, 14, 15]], dtype=int32)>
按-1轴合并
tf.concat([t1, t2], -1)
表示倒数第1个轴,由于当前是二维数组,所以结果等于 按1轴合并

