博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Keras(七)TF2中基础的数据类型API介绍
阅读量:4202 次
发布时间:2019-05-26

本文共 5950 字,大约阅读时间需要 19 分钟。

本文将介绍如下内容:

  • 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,零维数据的定义和转换
# Scalarst = 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.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.Tensor([21 22 23], shape=(3,), dtype=int32)
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------
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------
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.0print(s2)# tf.SparseTensor不支持加法计算try:    s3 = s + 1except 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()) 	# 将变量变成tensorprint(v.numpy())	# 打印具体的数值# ---output------
tf.Tensor([[1. 2. 3.] [4. 5. 6.]], shape=(2, 3), dtype=float32)[[1. 2. 3.] [4. 5. 6.]]
2,tf.Variable的赋值
# assign valuev.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

转载地址:http://zvili.baihongyu.com/

你可能感兴趣的文章
NTP-网络时间协议
查看>>
C/C++学习方法
查看>>
Borland编译器,在windows7的命令行中运行C++
查看>>
Apache Derby 网络服务器 - 10.9.1.0 - (1344872) 已启动并准备接受端口 1527 上的连接
查看>>
Java日常常用小算法
查看>>
JavaSE经典编程示例
查看>>
Eclipse软件相关知识
查看>>
人工智能资料汇总--AI传送门
查看>>
百度地图SDKv4.1.1 错误码230
查看>>
Android百度地图SDK -- 环境搭建
查看>>
Android学习路线
查看>>
导航栏实现
查看>>
图文混排实现
查看>>
Android 源码学习资源
查看>>
2016年华为研发出征大会演讲--任正非
查看>>
Android studio 快捷键及使用技巧
查看>>
日常常用网站
查看>>
关于 Java 中 finally 语句块的深度辨析
查看>>
安全公司
查看>>
Android中XML使用
查看>>