tf.stack() tf.unstack() tf.concat() tf.split()

目录

拼接操作: (tf.concat,tf.stack)

分割操作: (tf.unstack,tf.split) 

对比总结:


 

a = tf.constant([[1,2,3],[3,4,5]]) # shape (2,3)
b = tf.constant([[7,8,9],[10,11,12]]) # shape (2,3)

拼接操作: (tf.concat,tf.stack)

ab1 = tf.concat([a,b], axis=0) # shape(4,3)
[[ 1  2  3]
 [ 3  4  5]
 [ 7  8  9]
 [10 11 12]]
  1. tf.stack其作用类似于tf.concat,都是拼接两个张量,
  2. 不同之处,tf.concat拼接的是除了拼接维度axis外, 其他维度的shape完全相同的张量,并且产生的张量的阶数不会发生变化,而tf.stack则会在新的张量阶上拼接,产生的张量的阶数将会增加。

 

分割操作: (tf.unstack,tf.split) 

tf.unstack

import tensorflow as tf
a = tf.constant([[1, 2, 3], [4, 5, 6]])
b = tf.unstack(a, axis=0)
c = tf.unstack(a, axis=1)
with tf.Session() as sess:
        print(sess.run(a))
        print(sess.run(b))
        print(sess.run(c))

输出

[[1 2 3]
 [4 5 6]]


[array([1, 2, 3], dtype=int32), array([4, 5, 6], dtype=int32)]

[array([1, 4], dtype=int32), array([2, 5], dtype=int32), array([3, 6], dtype=int32)]

 

tf.split


import tensorflow as tf

a = tf.constant([[1, 2, 3], [4, 5, 6]])
b = tf.split(a,num_or_size_splits=2,axis=0)

with tf.Session() as sess:
    print(sess.run(a))
    print(sess.run(b))

[[1 2 3]
 [4 5 6]]
[array([[1, 2, 3]]), array([[4, 5, 6]])]

 

对比总结:

import tensorflow as tf

A = [[1, 2, 3], [4, 5, 6]]
a0 = tf.split(A, num_or_size_splits=3, axis=1)#不改变维数(!!)
a1 = tf.unstack(A, num=3,axis=1)
a2 = tf.split(A, num_or_size_splits=2, axis=0)
a3 = tf.unstack(A, num=2,axis=0)
with tf.Session() as sess:
    # print(sess.run(a0))
    # print(sess.run(a1))
    print(sess.run(a2))
    # print(sess.run(a3)

 

 

 


 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

贾世林jiashilin

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值