当前位置: 首页 > news >正文

深度学习TensorFlow2基础知识学习后半部分

介绍几个重要操作:

1.范数

a = tf.fill([1,2], value=2.)
b = tf.norm(a)# 二范数#第二种计算方法
# 计算验证
a = tf.square(a)
log("a的平方:", a)
a = tf.reduce_sum(a)
log("a平方后的和:", a)
b = tf.sqrt(a)
log("a平方和后开根号:", b)#二者结果是一样的b = tf.norm(a,ord=1)#一范数
print(b)
print(tf.reduce_sum(tf.abs(a)))#所有值得绝对值之和
# 指定计算轴:axis=1
b = tf.norm(a, ord=1, axis=1)
log("a的axis=1的1范数b:", b)

2.最大最小平均值的计算

a = tf.range(12, dtype=tf.float32)
a = tf.reshape(a, (4,3))
log("a数组:", a)b = tf.reduce_min(a)
log("a数组最小值:", b)
b = tf.reduce_max(a)
log("a数组最大值:", b)
b = tf.reduce_mean(a)
log("a数组平均值:", b)b = tf.reduce_min(a, axis=0)
log("a数组axis=0最小值:", b)
b = tf.reduce_max(a, axis=0)
log("a数组axis=0最大值:", b)
b = tf.reduce_mean(a, axis=0)
log("a数组axis=0平均值:", b)b = tf.reduce_min(a, axis=1)
log("a数组axis=1最小值:", b)
b = tf.reduce_max(a, axis=1)
log("a数组axis=1最大值:", b)
b = tf.reduce_mean(a, axis=1)
log("a数组axis=1平均值:", b)

其实都挺简单的,和numpy和torch都差不多。

3.索引

  • tf.argmax
  • tf.argmin
def log(prefix="", val=""):print(prefix, val, "\n")# 定义一个随机数组
a = tf.random.uniform((3,10), minval=0, maxval=10, dtype=tf.int32)
log("a", a)# 取最大索引位置数组,通常用于取得模型预测结果
b = tf.argmax(a, axis=1)
log("a数组axis=1的最大值索引位置:", b)# 取最小索引位置数组
b = tf.argmin(a, axis=1)
log("a数组axis=1的最小值索引位置:", b)

4.数组比较

一些python基础语法

a = tf.random.uniform((1,10), minval=0, maxval=10, dtype=tf.int32)
b = tf.random.uniform((1,10), minval=0, maxval=10, dtype=tf.int32)log("a:", a)
log("b:", b)# a,b数组比较
log("a==b", a==b)
# 相同元素输出
log(a[a==b])
# 相同元素索引位置输出
log(tf.where(a==b))

5.张量排序

  • 张量数值排序:tf.sort
  • 张量索引排序:tf.argsort
#################################################
# 声明数组a
a = tf.random.shuffle(tf.range(10))
log("a", a)
# a tf.Tensor([8 2 0 5 7 9 3 1 4 6], shape=(10,), dtype=int32) #################################################
# 升序排列
b = tf.sort(a, direction="ASCENDING")
log("b", b)
# 降序排列
b = tf.sort(a, direction="DESCENDING")
log("b", b)
# b tf.Tensor([0 1 2 3 4 5 6 7 8 9], shape=(10,), dtype=int32) 
# b tf.Tensor([9 8 7 6 5 4 3 2 1 0], shape=(10,), dtype=int32) #################################################
# 升序排列,返回索引位置
b = tf.argsort(a, direction="ASCENDING")
log("b", b)
# 降序排列,返回索引位置
b = tf.argsort(a, direction="DESCENDING")
log("b", b)
# b tf.Tensor([2 7 1 6 8 3 9 4 0 5], shape=(10,), dtype=int32) 
# b tf.Tensor([5 0 4 9 3 8 6 1 7 2], shape=(10,), dtype=int32) #################################################
# 按索引位置b, 从数组a中收集数据
c = tf.gather(a, b)
log("c", c)
# c tf.Tensor([9 8 7 6 5 4 3 2 1 0], shape=(10,), dtype=int32) 

6.二维张量排序

  • 2维张量数值排序:tf.sort
  • 2维张量索引排序:tf.argsort
#################################################
# 声明2维数组a
a = tf.random.uniform([3,5], maxval=10, dtype=tf.int32)
log("a", a)
# a tf.Tensor(
# [[2 5 8 0 4]
#  [1 7 2 4 5]
#  [6 0 2 5 0]], shape=(3, 5), dtype=int32) #################################################
# 升序排列
b = tf.sort(a, axis=1, direction="ASCENDING")
log("b", b)
# 降序排列
b = tf.sort(a, axis=1, direction="DESCENDING")
log("b", b)
# b tf.Tensor(
# [[0 2 4 5 8]
#  [1 2 4 5 7]
#  [0 0 2 5 6]], shape=(3, 5), dtype=int32) 
# b tf.Tensor(
# [[8 5 4 2 0]
#  [7 5 4 2 1]
#  [6 5 2 0 0]], shape=(3, 5), dtype=int32) #################################################
# 升序排列,返回索引位置
b = tf.argsort(a, axis=1, direction="ASCENDING")
log("b", b)
# 降序排列,返回索引位置
b = tf.argsort(a, axis=1, direction="DESCENDING")
log("b", b)
# b tf.Tensor(
# [[3 0 4 1 2]
#  [0 2 3 4 1]
#  [1 4 2 3 0]], shape=(3, 5), dtype=int32) 
# b tf.Tensor(
# [[2 1 4 0 3]
#  [1 4 3 2 0]
#  [0 3 2 1 4]], shape=(3, 5), dtype=int32) 

7.TopK值取得

################################################
# 2维数组定义
a = tf.random.uniform([3,5], maxval=10, dtype=tf.int32)
log("a", a)
# a tf.Tensor(
# [[1 2 5 8 7]
#  [6 1 4 3 9]
#  [5 9 6 5 5]], shape=(3, 5), dtype=int32) #################################################
# 取数组每行前3位
b = tf.math.top_k(a, k=3, sorted=True)
# 前3位数值
log("b", b.values)
# 前3位数值索引
log("b", b.indices)# b tf.Tensor(
# [[8 7 5]
#  [9 6 4]
#  [9 6 5]], shape=(3, 3), dtype=int32) 
# b tf.Tensor(
# [[3 4 2]
#  [4 0 2]
#  [1 2 0]], shape=(3, 3), dtype=int32) 

http://www.lryc.cn/news/254998.html

相关文章:

  • 电脑系统重装Win10专业版操作教程
  • 打包Python项目
  • 使用Python实现爬虫IP负载均衡和高可用集群
  • Jenkins+Maven+Gitlab+Tomcat 自动化构建打包,部署
  • 泰凌微(Telink)8258配置串口收发自定义数据
  • 入门低代码开发:快速构建应用程序的方法
  • 常见客户端消息推送服务【Java后端】
  • C++11(下)
  • 深度学习与逻辑回归模型的融合--TensorFlow多元分类的高级应用
  • 水库大坝安全监测参数与设备
  • 要求CHATGPT高质量回答的艺术:提示工程技术的完整指南—第 22 章:情感分析提示
  • 数据清洗、特征工程和数据可视化、数据挖掘与建模的主要内容
  • C++ STL容器与常用库函数
  • Nmap脚本简介
  • Kafka -- 初识
  • 玩转Sass:掌握数据类型!
  • Django + Matplotlib:实现数据分析显示与下载为PDF或SVG
  • 【Rust】第一节:安装
  • 12-07 周四 Pytorch 使用Visdom 进行可视化
  • 基于微信小程序的智慧校园导航系统研究
  • VUE3给table的head添加popover筛选、时间去除时分秒、字符串替换某字符
  • 19、XSS——HTTP协议安全
  • 深圳锐杰金融:用金融力量守护社区健康
  • python对py文件加密
  • Thymeleaf生成pdf表格合并单元格描边不显示
  • C# Solidworks二次开发:三种获取SW设计结构树的方法-第二讲
  • 分布式搜索引擎03
  • flex布局的flex为1到底是什么
  • class050 双指针技巧与相关题目【算法】
  • 计算机操作系统4