首页 > 其他 > 详细

吴裕雄--天生自然TensorFlow2教程:合并与分割

时间:2020-01-01 22:33:49      阅读:97      评论:0      收藏:0      [点我收藏+]
import tensorflow as tf

# 6个班级的学生分数情况
a = tf.ones([4, 35, 8])
b = tf.ones([2, 35, 8])
c = tf.concat([a, b], axis=0)
c.shape
# 3个学生学生补考
a = tf.ones([4, 32, 8])
b = tf.ones([4, 3, 8])
tf.concat([a, b], axis=1).shape
a = tf.ones([4, 35, 8])
b = tf.ones([4, 35, 8])
a.shape
b.shape
tf.concat([a, b], axis=-1).shape
tf.stack([a, b], axis=0).shape
tf.stack([a, b], axis=3).shape
a = tf.ones([4, 35, 8])
b = tf.ones([3, 33, 8])
try:
    tf.concat([a, b], axis=0).shape
except Exception as e:
    print(e)
# concat保证只有一个维度不相等
b = tf.ones([2, 35, 8])
c = tf.concat([a, b], axis=0)
c.shape
# stack保证所有维度相等
try:
    tf.stack([a, b], axis=0)
except Exception as e:
    print(e)
a.shape
b = tf.ones([4, 35, 8])
c = tf.stack([a, b])
c.shape
aa, bb = tf.unstack(c, axis=0)
aa.shape, bb.shape
# [2,4,35,8]
res = tf.unstack(c, axis=3)
# 8个[2, 4, 35]的Tensor
res[0].shape, res[1].shape, res[7].shape
# [2,4,35,8]
res = tf.unstack(c, axis=2)
# 35个[2, 4, 8]的Tensor
res[0].shape, res[1].shape, res[34].shape
# 8个Tensor,全为1
res = tf.unstack(c, axis=3)
len(res)
# 2个Tensor,一个6、一个2
res = tf.split(c, axis=3, num_or_size_splits=2)
len(res)
res[0].shape
res = tf.split(c, axis=3, num_or_size_splits=[2, 2, 4])
res[0].shape, res[1].shape, res[2].shape

 

吴裕雄--天生自然TensorFlow2教程:合并与分割

原文:https://www.cnblogs.com/tszr/p/12130167.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!