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

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

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

momaojia   2021-04-02 我要评论

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()的使用就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。

您可能感兴趣的文章:

猜您喜欢

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们