tensorflow中的concat

张量的维度

张量用符合的[]来表示

a = tf.constant([[[1,1],[2,2]],[[3,3],[4,4]]])

举例说明,这里的[[[1,1],[2,2]],[[3,3],[4,4]]]最左边的连着的三个‘[‘就表示三维张量,最外边的’[‘表示0维度,也就是张量整体,第一维度是第二个’[’,对应[[1,1],[2,2]],第二维度对应[1,1],第三维度对应1。

concat的用法

这个函数起到张量合并的作用将两个张量在某一维度进行整合
concat(values, axis, name = “concat”)
其中,values填充一个列表,列表中存在待处理张量,axis表示整合的维度。下面分别从0维度,1维度,二维度进行整合(第三维度只有一个元素,无法整合)
在终端依次输入:

a = tf.constant([[[1,1],[2,2]],[[3,3],[4,4]]])
 b = tf.constant([[[5,5],[6,6]],[[7,7],[8,8]]])
sess = tf.Session()
sess.run(a)
a.shape

终端输出

array([[[1, 1],
        [2, 2]],

       [[3, 3],
        [4, 4]]])
TensorShape([Dimension(2), Dimension(2), Dimension(2)])

再分别输入

sess.run(tf.concat([a,b],0))
tf.concat([a,b],0).shape
sess.run(tf.concat([a,b],1))
tf.concat([a,b],1).shape
sess.run(tf.concat([a,b],2))
tf.concat([a,b],2).shape

分别对应输出

array([[[1, 1],
        [2, 2]],

       [[3, 3],
        [4, 4]],

       [[5, 5],
        [6, 6]],

       [[7, 7],
        [8, 8]]])
TensorShape([Dimension(4), Dimension(2), Dimension(2)])
array([[[1, 1],
        [2, 2],
        [5, 5],
        [6, 6]],

       [[3, 3],
        [4, 4],
        [7, 7],
        [8, 8]]])
TensorShape([Dimension(2), Dimension(4), Dimension(2)])
array([[[1, 1, 5, 5],
        [2, 2, 6, 6]],

       [[3, 3, 7, 7],
        [4, 4, 8, 8]]])
TensorShape([Dimension(2), Dimension(2), Dimension(4)])

猜你喜欢

转载自blog.csdn.net/qq_42327424/article/details/88928753