网络编程
位置:首页>> 网络编程>> 网络编程>> 浅谈tensorflow 中tf.concat()的使用

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

作者:momaojia  发布时间:2023-07-21 20:24:08 

标签: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]]]

来源:https://blog.csdn.net/momaojia/article/details/77603322

0
投稿

猜你喜欢

手机版 网络编程 asp之家 www.aspxhome.com