本文将介绍如下内容:
- tf.constant
- tf.strings
- tf.ragged.constant
- tf.SparseTensor
- tf.Variable
一,tf.constant常量
1,定义tf.constant常量
复制代码
1
2
3
4
5
6
7
8t = 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可以使用索引进行切片操作
复制代码
1
2
3
4
5
6
7
8
9
10t = 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,算子操作
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16t = 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之间的转换
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14print(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,零维数据的定义和转换
复制代码
1
2
3
4
5
6
7
8
9# Scalars t = tf.constant(2.718) print(t.numpy()) print(t.shape) #----output------------ 2.718 ()
二,tf.strings字符串常量
1,纯英文字符的UTF8编码对应码
复制代码
1
2
3
4
5
6
7
8
9
10
11
12t = 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编码对应码
复制代码
1
2
3
4
5
6
7
8
9
10
11t = 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的索引切片操作
复制代码
1
2
3
4
5
6
7
8
9
10r = 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的行拼接操作
复制代码
1
2
3
4
5
6
7r = 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的列拼接操作
注意: 对于列拼接,需要行数必须相同,否则会报错!
复制代码
1
2
3
4
5
6
7r = 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来补空位,填补在真实值后。
复制代码
1
2
3
4
5
6
7
8
9
10r = 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的定义
复制代码
1
2
3
4
5
6
7
8
9
10
11s = 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密集矩阵
复制代码
1
2
3
4
5
6
7
8
9
10
11s = 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排序。
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17s5 = 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的计算
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41s = 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的定义
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15v = 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的赋值
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16# 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函数,不能使用=赋值
复制代码
1
2
3
4
5
6
7
8try: v[1] = [7., 8., 9.] except TypeError as ex: print(ex) # ----output----- 'ResourceVariable' object does not support item assignment
最后
以上就是不安康乃馨最近收集整理的关于Keras(七)TF2中基础的数据类型API介绍的全部内容,更多相关Keras(七)TF2中基础内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复