Keras(七)TF2中基础的数据类型API介绍

2024-03-26 15:48

本文主要是介绍Keras(七)TF2中基础的数据类型API介绍,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

本文将介绍如下内容:

  • tf.constant
  • tf.strings
  • tf.ragged.constant
  • tf.SparseTensor
  • tf.Variable

一,tf.constant常量

1,定义tf.constant常量
t = tf.constant([[1., 2., 3.], [4., 5., 6.]])
print(t)#----output------------
tf.Tensor(
[[1. 2. 3.][4. 5. 6.]], shape=(2, 3), dtype=float32)
2,根据index索引切片

tf.constant可以使用索引进行切片操作

t = tf.constant([[1., 2., 3.], [4., 5., 6.]])
print(t[..., 1:])
print(t[:, 1])#----output------------
tf.Tensor(
[[2. 3.][5. 6.]], shape=(2, 2), dtype=float32)
tf.Tensor([2. 5.], shape=(2,), dtype=float32)
3,算子操作
t = tf.constant([[1., 2., 3.], [4., 5., 6.]])
print(t+10)
print(tf.square(t))
print(t @ tf.transpose(t))	# 矩阵与其转置相乘#----output------------
tf.Tensor(
[[11. 12. 13.][14. 15. 16.]], shape=(2, 3), dtype=float32)
tf.Tensor(
[[ 1.  4.  9.][16. 25. 36.]], shape=(2, 3), dtype=float32)
tf.Tensor(
[[14. 32.][32. 77.]], shape=(2, 2), dtype=float32)
4,与numpy之间的转换
print(t.numpy())
print(np.square(t))
np_t = np.array([[1., 2., 3.], [4., 5., 6.]])
print(tf.constant(np_t))#----output------------
[[1. 2. 3.][4. 5. 6.]]
[[ 1.  4.  9.][16. 25. 36.]]
tf.Tensor(
[[1. 2. 3.][4. 5. 6.]], shape=(2, 3), dtype=float64)
5,零维数据的定义和转换
# Scalars
t = tf.constant(2.718)
print(t.numpy())
print(t.shape)#----output------------
2.718
()

二,tf.strings字符串常量

1,纯英文字符的UTF8编码对应码
t = tf.constant("cafe")
print(t)
print(tf.strings.length(t))
print(tf.strings.length(t, unit="UTF8_CHAR"))
print(tf.strings.unicode_decode(t, "UTF8"))#----output------------
tf.Tensor(b'cafe', shape=(), dtype=string)
tf.Tensor(4, shape=(), dtype=int32)
tf.Tensor(4, shape=(), dtype=int32)
tf.Tensor([ 99  97 102 101], shape=(4,), dtype=int32)
2,含中文字符的UTF8编码对应码
t = tf.constant(["cafe", "coffee", "咖啡"])
print(tf.strings.length(t))
print(tf.strings.length(t, unit="UTF8_CHAR"))
r = tf.strings.unicode_decode(t, "UTF8")
print(r)#----output------------
tf.Tensor([4 6 6], shape=(3,), dtype=int32)
tf.Tensor([4 6 2], shape=(3,), dtype=int32)
<tf.RaggedTensor [[99, 97, 102, 101], [99, 111, 102, 102, 101, 101], [21654, 21857]]>

三,tf.ragged.constant

在定义TF常量时,如果数据类型不是标准的矩阵,可以使用tf.ragged.constant来处理

1,tf.ragged_tensor的索引切片操作
r = tf.ragged.constant([[11, 12], [21, 22, 23], [], [41]])
print(r)
print(r[1])
print(r[1:2])#------output------
<tf.RaggedTensor [[11, 12], [21, 22, 23], [], [41]]>
tf.Tensor([21 22 23], shape=(3,), dtype=int32)
<tf.RaggedTensor [[21, 22, 23]]>
2,tf.ragged_tensor的行拼接操作
r = tf.ragged.constant([[11, 12], [21, 22, 23], [], [41]])
r2 = tf.ragged.constant([[51, 52], [], [71]])
print(tf.concat([r, r2], axis = 0))#------output------
<tf.RaggedTensor [[11, 12], [21, 22, 23], [], [41], [51, 52], [], [71]]>
3,tf.ragged_tensor的列拼接操作

注意: 对于列拼接,需要行数必须相同,否则会报错!

r = tf.ragged.constant([[11, 12], [21, 22, 23], [], [41]])
r3 = tf.ragged.constant([[13, 14], [15], [], [42, 43]])
print(tf.concat([r, r3], axis = 1))#------output------
<tf.RaggedTensor [[11, 12, 13, 14], [21, 22, 23, 15], [], [41, 42, 43]]>
4,将tf.RaggedTensor 转化为 tf.Tensor(使用0来补空位)

注意: 因为tf.RaggedTensor为不规则矩阵,所以转化时会使用0来补空位,填补在真实值后。

r = tf.ragged.constant([[11, 12], [21, 22, 23], [], [41]])
print(r.to_tensor())#------output------
tf.Tensor(
[[11 12  0][21 22 23][ 0  0  0][41  0  0]], shape=(4, 3), dtype=int32)

四,tf.SparseTensor

tf.ragged.constant中的填充数只能在真实值的后面,可以使用tf.SparseTensor类型解决此问题。

1,tf.SparseTensor的定义
s = tf.SparseTensor(indices = [[0, 1], [1, 0], [2, 3]],values = [1., 2., 3.],dense_shape = [3, 4])
print(s)#------output------
SparseTensor(indices=tf.Tensor(
[[0 1][1 0][2 3]], shape=(3, 2), dtype=int64), values=tf.Tensor([1. 2. 3.], shape=(3,), dtype=float32), dense_shape=tf.Tensor([3 4], shape=(2,), dtype=int64))
2,将tf.SparseTensor转为tf.Tensor密集矩阵
s = tf.SparseTensor(indices = [[0, 1], [1, 0], [2, 3]],values = [1., 2., 3.],dense_shape = [3, 4])
print(tf.sparse.to_dense(s)) # ---output------
tf.Tensor(
[[0. 1. 0. 0.][2. 0. 0. 0.][0. 0. 0. 3.]], shape=(3, 4), dtype=float32)

注意:若indices位置颠倒,tf.SparseTensor无法转为tf.Tensor密集矩阵.可先使用tf.sparse.reorder排序。

s5 = tf.SparseTensor(indices = [[0, 2], [0, 1], [2, 3]],values = [1., 2., 3.],dense_shape = [3, 4])
print(s5)
s6 = tf.sparse.reorder(s5)
print(tf.sparse.to_dense(s6))#-----output----------
SparseTensor(indices=tf.Tensor(
[[0 2][0 1][2 3]], shape=(3, 2), dtype=int64), values=tf.Tensor([1. 2. 3.], shape=(3,), dtype=float32), dense_shape=tf.Tensor([3 4], shape=(2,), dtype=int64))
tf.Tensor(
[[0. 2. 1. 0.][0. 0. 0. 0.][0. 0. 0. 3.]], shape=(3, 4), dtype=float32)
3,tf.SparseTensor的计算
s = tf.SparseTensor(indices = [[0, 1], [1, 0], [2, 3]],values = [1., 2., 3.],dense_shape = [3, 4])
print(s)
# 将tf.SparseTensor转为tf.Tensor密集矩阵
print(tf.sparse.to_dense(s)) 
# tf.SparseTensor的计算
s2 = s * 2.0
print(s2)# tf.SparseTensor不支持加法计算
try:s3 = s + 1
except TypeError as ex:print(ex)s4 = tf.constant([[10., 20.],[30., 40.],[50., 60.],[70., 80.]])
print(tf.sparse.sparse_dense_matmul(s, s4))#---output----------
SparseTensor(indices=tf.Tensor(
[[0 1][1 0][2 3]], shape=(3, 2), dtype=int64), values=tf.Tensor([1. 2. 3.], shape=(3,), dtype=float32), dense_shape=tf.Tensor([3 4], shape=(2,), dtype=int64))
tf.Tensor(
[[0. 1. 0. 0.][2. 0. 0. 0.][0. 0. 0. 3.]], shape=(3, 4), dtype=float32)
SparseTensor(indices=tf.Tensor(
[[0 1][1 0][2 3]], shape=(3, 2), dtype=int64), values=tf.Tensor([2. 4. 6.], shape=(3,), dtype=float32), dense_shape=tf.Tensor([3 4], shape=(2,), dtype=int64))
unsupported operand type(s) for +: 'SparseTensor' and 'int'
tf.Tensor(
[[ 30.  40.][ 20.  40.][210. 240.]], shape=(3, 2), dtype=float32)

五,tf.Variable

1 ,tf.Variable的定义
v = tf.Variable([[1., 2., 3.], [4., 5., 6.]])
print(v)			# 打印变量
print(v.value()) 	# 将变量变成tensor
print(v.numpy())	# 打印具体的数值# ---output------
<tf.Variable 'Variable:0' shape=(2, 3) dtype=float32, numpy=
array([[1., 2., 3.],[4., 5., 6.]], dtype=float32)>
tf.Tensor(
[[1. 2. 3.][4. 5. 6.]], shape=(2, 3), dtype=float32)
[[1. 2. 3.][4. 5. 6.]]
2,tf.Variable的赋值
# assign value
v.assign(2*v)
print(v.numpy())
v[0, 1].assign(42)
print(v.numpy())
v[1].assign([7., 8., 9.])
print(v.numpy())# ----output-----
[[ 2.  4.  6.][ 8. 10. 12.]]
[[ 2. 42.  6.][ 8. 10. 12.]]
[[ 2. 42.  6.][ 7.  8.  9.]]

注意:变量的赋值只能用assign函数,不能使用=赋值

try:v[1] = [7., 8., 9.]
except TypeError as ex:print(ex)# ----output-----
'ResourceVariable' object does not support item assignment

这篇关于Keras(七)TF2中基础的数据类型API介绍的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/849005

相关文章

redis过期key的删除策略介绍

《redis过期key的删除策略介绍》:本文主要介绍redis过期key的删除策略,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录第一种策略:被动删除第二种策略:定期删除第三种策略:强制删除关于big key的清理UNLINK命令FLUSHALL/FLUSHDB命

Android Mainline基础简介

《AndroidMainline基础简介》AndroidMainline是通过模块化更新Android核心组件的框架,可能提高安全性,本文给大家介绍AndroidMainline基础简介,感兴趣的朋... 目录关键要点什么是 android Mainline?Android Mainline 的工作原理关键

springboot项目中常用的工具类和api详解

《springboot项目中常用的工具类和api详解》在SpringBoot项目中,开发者通常会依赖一些工具类和API来简化开发、提高效率,以下是一些常用的工具类及其典型应用场景,涵盖Spring原生... 目录1. Spring Framework 自带工具类(1) StringUtils(2) Coll

Pytest多环境切换的常见方法介绍

《Pytest多环境切换的常见方法介绍》Pytest作为自动化测试的主力框架,如何实现本地、测试、预发、生产环境的灵活切换,本文总结了通过pytest框架实现自由环境切换的几种方法,大家可以根据需要进... 目录1.pytest-base-url2.hooks函数3.yml和fixture结论你是否也遇到过

mysql的基础语句和外键查询及其语句详解(推荐)

《mysql的基础语句和外键查询及其语句详解(推荐)》:本文主要介绍mysql的基础语句和外键查询及其语句详解(推荐),本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋... 目录一、mysql 基础语句1. 数据库操作 创建数据库2. 表操作 创建表3. CRUD 操作二、外键

Python基础语法中defaultdict的使用小结

《Python基础语法中defaultdict的使用小结》Python的defaultdict是collections模块中提供的一种特殊的字典类型,它与普通的字典(dict)有着相似的功能,本文主要... 目录示例1示例2python的defaultdict是collections模块中提供的一种特殊的字

Python基础文件操作方法超详细讲解(详解版)

《Python基础文件操作方法超详细讲解(详解版)》文件就是操作系统为用户或应用程序提供的一个读写硬盘的虚拟单位,文件的核心操作就是读和写,:本文主要介绍Python基础文件操作方法超详细讲解的相... 目录一、文件操作1. 文件打开与关闭1.1 打开文件1.2 关闭文件2. 访问模式及说明二、文件读写1.

C#基础之委托详解(Delegate)

《C#基础之委托详解(Delegate)》:本文主要介绍C#基础之委托(Delegate),具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1. 委托定义2. 委托实例化3. 多播委托(Multicast Delegates)4. 委托的用途事件处理回调函数LINQ

基于Flask框架添加多个AI模型的API并进行交互

《基于Flask框架添加多个AI模型的API并进行交互》:本文主要介绍如何基于Flask框架开发AI模型API管理系统,允许用户添加、删除不同AI模型的API密钥,感兴趣的可以了解下... 目录1. 概述2. 后端代码说明2.1 依赖库导入2.2 应用初始化2.3 API 存储字典2.4 路由函数2.5 应

MySQL中慢SQL优化的不同方式介绍

《MySQL中慢SQL优化的不同方式介绍》慢SQL的优化,主要从两个方面考虑,SQL语句本身的优化,以及数据库设计的优化,下面小编就来给大家介绍一下有哪些方式可以优化慢SQL吧... 目录避免不必要的列分页优化索引优化JOIN 的优化排序优化UNION 优化慢 SQL 的优化,主要从两个方面考虑,SQL 语