浅谈tensorflow 中tf.concat()的使用

concat()是将tensor沿着指定维度连接起来。其中tensorflow1.3版中是这样定义的:

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

一、对于2维来说,0表示行,1表示列

t1 = [[1, 2, 3], [4, 5, 6]]
t2 = [[7, 8, 9], [10, 11, 12]]
 
with tf.Session() as sess:
 print(sess.run(tf.concat([t1, t2], 0) ))

结果为:[[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]]

t1 = [[1, 2, 3], [4, 5, 6]]
t2 = [[7, 8, 9], [10, 11, 12]]
 
with tf.Session() as sess:
 print(sess.run(tf.concat([t1, t2], 1) ))

结果为:[[1, 2, 3, 7, 8, 9], [4, 5, 6, 10, 11, 12]]

二、 对于3维来说 0表示纵向,1表示行,2表示列

t1 = [[[1, 1, 1],[2, 2, 2]],[[3, 3, 3],[4, 4, 4]]]
 
t2 = [[[5, 5, 5],[6, 6, 6]],[[7, 7, 7],[8, 8, 8]]]
 
with tf.Session() as sess:
 print(sess.run(tf.concat([t1, t2], 0) ))

结果:[[[1 1 1],[2 2 2]] , [[3 3 3],[4 4 4]] , [[5 5 5],[6 6 6]] ,  [[7 7 7],[8 8 8]]]
Tensor("concat_30:0", shape=(4, 2, 3), dtype=int32)

axis=1的结果如下:

Tensor("concat_31:0", shape=(2, 4, 3), dtype=int32)
[[[1 1 1], [2 2 2],[5 5 5],[6 6 6]], [[3 3 3], [4 4 4],[7 7 7], [8 8 8]]]

axis=2的结果如下:

Tensor("concat_32:0", shape=(2, 2, 6), dtype=int32)
[[[1 1 1 5 5 5],[2 2 2 6 6 6]], [[3 3 3 7 7 7], [4 4 4 8 8 8]]]

以上这篇浅谈tensorflow 中tf.concat()的使用就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。

若文章对您有帮助,帮忙点个赞!

0
-3
发布时间 2020-02-07 18:01:04
0 条回复(回复会通过微信通知作者)
点击加载更多评论
登录 后再进行评论
(微信扫码即可登录,无需注册)