本文共 5950 字,大约阅读时间需要 19 分钟。
本文将介绍如下内容:
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)
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)
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)
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)
# Scalarst = tf.constant(2.718)print(t.numpy())print(t.shape)#----output------------2.718()
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)
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常量时,如果数据类型不是标准的矩阵,可以使用tf.ragged.constant来处理
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)
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------
注意: 对于列拼接,需要行数必须相同,否则会报错!
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为不规则矩阵,所以转化时会使用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.ragged.constant中的填充数只能在真实值的后面,可以使用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))
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)
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)
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.]]
# 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/