tensorflow2.0---笔记2 tensor高阶操作

tensor的合并与分割、排序等操作
摘要由CSDN通过智能技术生成

tensor的合并与分割

在这里插入图片描述

合并

在这里插入图片描述

tf.concat([a, b], axis = )
注:
(1)不创建新的维度;
(2)在哪个维度合并,哪个维度数量增加;其他维度必须相同。
在这里插入图片描述

a = tf.ones([4, 35, 8])
b = tf.ones([2, 35, 8])
c = tf.ones([4, 35, 8])

d = tf.concat([a, b], axis=0)
print(d.shape)  # (6, 35, 8)

e = tf.concat([a, c], axis=-1)
print(e.shape)  # (4, 35, 16)

在这里插入图片描述

tf.stack([a, b], axis = )
注意,会创建新的维度!

a = tf.ones([4, 35, 8])
b = tf.ones([4, 35, 8])

c = tf.stack([a, b], axis=0)
print(c.shape)

d = tf.stack([a, b], axis=-1)
print(d.shape)  # (4, 35, 8, 2)

a、b的原始维度必须完全相同,否则报错:

a = tf.ones([4, 35, 8])
b = tf.ones([2, 35, 8])

c = tf.stack([a, b], axis=0)
print(c.shape)
#  ensorflow.python.framework.errors_impl.InvalidArgumentError: Shapes of all inputs must match: values[0].shape = [4,35,8] != values[1].shape = [2,35,8] [Op:Pack] name: stack

分割

tf.unstack(a, axis = )
注意,返回一个list,list里面的内容是tensor

a = tf.ones([4, 35, 8])
b = tf.ones([4, 35, 8])
c = tf.stack([a, b], axis=0)
print(c.shape)  # (2, 4, 35, 8)

d = tf.unstack(c, axis=0)
print(type(d))  # <class 'list'>
print(d[0].shape)  # (4, 35, 8)

e = tf.unstack(c, axis=-1)
print(len(e)) # 8
print(e[0].shape)  # (2, 4, 35)   范围e[0]~e[7]

tf.split(a, axis = , num_or_size_splits = )

a = tf.ones([4, 35, 8])
b = tf.ones([4, 35, 8])
c = tf.stack([a, b], axis=0)
print(c.shape)  # (2, 4, 35, 8)

d = tf.split(c, axis=3, num_or_size_splits=2)  # 分成两份
print(type(d))  # <class 'list'>
print(len(d))  # 2
print(d[0].shape)  # (2, 4, 35, 4)

e = tf.split(c, axis=3, num_or_size_splits=[2, 2, 4])
print(e[0].shape)  # (2, 4, 35, 2)
print(e[2].shape)  # (2, 4, 35, 4)

数据统计

在这里插入图片描述
向量的范数:
在这里插入图片描述

tf.norm (范数)

a = tf.ones([2, 2])
print(tf.norm(a)) # tf.Tensor(2.0, shape=(), dtype=float32)  
# 不指定几范数和轴,默认二范数
print(tf.sqrt(tf.reduce_sum(tf.square(a))))  # 同样的效果
a = tf.ones([2, 3])
b = tf.norm(a, ord=2, axis=1)
print(b.shape)  # (2,)  指定列方向,结果为行数
a = tf.ones([2, 3])
b = tf.norm(a, ord=1, axis=0)
print(b)
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值