tf.concat中axis的含义是什么
tf.concat中axis的含义是什么?相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。
tensorflow中tf.concat的axis的使用我一直理解的比较模糊,这次做个笔记理下自己的思路。
importtensorflowastf tf.enable_eager_execution() importnumpyasnp
先生成两个矩阵m1, 和m2, 大小为两行三列
m1=np.random.rand(2,3)#m1.shape(2,3) m1 >>array([[0.44529968,0.42451167,0.07463199], [0.35787143,0.22926186,0.34583839]]) m2=np.random.rand(2,3)#m2.shape(2,3) m2 >>array([[0.92811531,0.6180391,0.71969461], [0.00564108,0.55381637,0.17155987]])
接下来采用tf.concat进行连接,简单来说,axis=0实际就是按行拼接,axis=1就是按列拼接
#axis=0 m3=tf.concat([m1,m2],axis=0) m3 >>array([[0.44529968,0.42451167,0.07463199], [0.35787143,0.22926186,0.34583839], [0.92811531,0.6180391,0.71969461], [0.00564108,0.55381637,0.17155987]]) m3.shape >>(4,3) #axis=1 m4=tf.concat([m1,m2],axis=1) m4 >>array([[0.44529968,0.42451167,0.07463199,0.92811531,0.6180391, 0.71969461], [0.35787143,0.22926186,0.34583839,0.00564108,0.55381637, 0.17155987]]) m4.shape >>(2,6)
但这实际上这只有在我们的输入是二维矩阵时才可以这样理解。axis的实际含义是根据axis指定的维度进行连接,如矩阵m1的维度为(2,3), 那么axis=0就代表了第一个维度‘2',因此,将m1和m2按照第一个维度进行连接,得到的新的矩阵就是将第一维度进行相加,其余维度不变,即维度变成了(4,3).
同理,axis=1时就是将矩阵的第二维度进行合并,其余维度不变,即维度变成了(2,6)。
接下来处理三个维度的数据,这也是我们在神经网络数据中经常要用到的,增加的一个维度通常代表了batch_size. 如下面的m5, batch_size=5, 可以理解为每个样本是个2*3的矩阵,一次将5个样本放在一起。
m5=np.random.rand(5,2,3) m6=np.random.rand(5,2,3) m5 >>array([[[0.04347217,0.03368232,0.36017024], [0.74223151,0.06609717,0.38155531]], [[0.50602728,0.355745,0.93379797], [0.97572621,0.53745311,0.66461841]], [[0.92832972,0.02441683,0.48436203], [0.69651043,0.24194495,0.64623769]], [[0.66667596,0.60053027,0.2970753], [0.13281764,0.29326326,0.32393028]], [[0.40892782,0.48516547,0.02298178], [0.51239083,0.40151008,0.29913204]]]) m6 >>array([[[0.58001909,0.56925704,0.09798246], [0.20841893,0.62683633,0.17923217]], [[0.91216164,0.0200782,0.3986682], [0.86687006,0.83730576,0.48443545]], [[0.65641654,0.59786311,0.2055584], [0.65391822,0.74093133,0.02416627]], [[0.80778861,0.22644312,0.91610686], [0.0789411,0.86955002,0.41437046]], [[0.97821668,0.97118328,0.97714882], [0.21543173,0.06964724,0.35360077]]])
在这种情况下,axis=0代表的第一个维度的含义就不再是之前认为的行的概念了,现在m5的第一维度的值是5,代表的是batch_size。仍然按照之前的理解,如果设置axis=0, axis=0就是将第一维度进行相加,其余维度不变,因此我们可以得到新的维度为(10,2,3)。
m7=tf.concat([m5,m6],axis=0) m7 >>array([[[0.04347217,0.03368232,0.36017024], [0.74223151,0.06609717,0.38155531]], [[0.50602728,0.355745,0.93379797], [0.97572621,0.53745311,0.66461841]], [[0.92832972,0.02441683,0.48436203], [0.69651043,0.24194495,0.64623769]], [[0.66667596,0.60053027,0.2970753], [0.13281764,0.29326326,0.32393028]], [[0.40892782,0.48516547,0.02298178], [0.51239083,0.40151008,0.29913204]], [[0.58001909,0.56925704,0.09798246], [0.20841893,0.62683633,0.17923217]], [[0.91216164,0.0200782,0.3986682], [0.86687006,0.83730576,0.48443545]], [[0.65641654,0.59786311,0.2055584], [0.65391822,0.74093133,0.02416627]], [[0.80778861,0.22644312,0.91610686], [0.0789411,0.86955002,0.41437046]], [[0.97821668,0.97118328,0.97714882], [0.21543173,0.06964724,0.35360077]]]) m7.shape >>(10,2,3)
同理,也可以进行axis=1, axis=2的concat操作。
此外,axis的值也可以设置为负数,如axis=-1实际上就是指倒数第一个维度,如m5的倒数第一个维度的值就是‘3'。因此,axis=2的操作和axis=-1的操作是等价的。
看完上述内容,你们掌握tf.concat中axis的含义是什么的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注恰卡编程网行业资讯频道,感谢各位的阅读!