tensorflow2中constant,ragged,SparseTensor,Variable的使用

from tensorflow import keras
import tensorflow as tf
import numpy as np
print(tf.__name__,tf.__version__)
print(keras.__name__, keras.__version__)
tensorflow 2.0.0
tensorflow_core.keras 2.2.4-tf
# 定义常量
t = tf.constant([[1., 2., 3.], [4., 5., 6.]])
# 做索引操作
print(t)
# 取从第二例以后的数值
print(t[:, 1:])
# 只把第二列取出来,当做一个tensor
print(t[..., 1])

# 做算子操作
# 加
print(t+10)
# 平方
print(tf.square(t))
# 乘转置
print(t @ tf.transpose(t))
tf.Tensor(
[[1. 2. 3.]
 [4. 5. 6.]], shape=(2, 3), dtype=float32)
tf.Tensor(
[[2. 3.]
 [5. 6.]], shape=(2, 2), dtype=float32)
tf.Tensor([2. 5.], shape=(2,), dtype=float32)
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)
# 进行numpy 操作
print(t.numpy())
print(np.square(t))
np_t = np.array([[1., 2., 3.], [4., 5., 6.]])
print(tf.constant(np_t))
[[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)
# 0维, 一个具体的数
t = tf.constant(3.1415)
print(t)
tf.Tensor(3.1415, shape=(), dtype=float32)
# string
t = tf.constant("tesorflow")
print(t)
print(tf.strings.length(t))
print(tf.strings.unicode_decode(t, "UTF8"))

# string array
t = tf.constant(["tensorflow", "pytorch", "咖啡"])
print(tf.strings.length(t, unit="UTF8_CHAR"))
tf.Tensor(b'tesorflow', shape=(), dtype=string)
tf.Tensor(9, shape=(), dtype=int32)
tf.Tensor([116 101 115 111 114 102 108 111 119], shape=(9,), dtype=int32)
tf.Tensor([10  7  2], shape=(3,), dtype=int32)
# ragged tensor
r = tf.ragged.constant([[11, 12], [21, 22, 23], [], [41]])
print(r)
print(r[1:3])
print(r.to_tensor())
# 拼接操作
r2 = tf.ragged.constant([[51, 52], [], [72, 72]])
print(tf.concat([r, r2], axis = 0))
# axis=1 拼接时, 需要维数相同
r3 = tf.ragged.constant([[13,14], [15], [], [42, 43]])
print(tf.concat([r, r3], axis = 1))
<tf.RaggedTensor [[11, 12], [21, 22, 23], [], [41]]>
<tf.RaggedTensor [[21, 22, 23], []]>
tf.Tensor(
[[11 12  0]
 [21 22 23]
 [ 0  0  0]
 [41  0  0]], shape=(4, 3), dtype=int32)
<tf.RaggedTensor [[11, 12], [21, 22, 23], [], [41], [51, 52], [], [72, 72]]>
<tf.RaggedTensor [[11, 12, 13, 14], [21, 22, 23, 15], [], [41, 42, 43]]>
# sparse tensor 大部分位置为0,少部分
s = tf.SparseTensor(indices = [[0, 1], [1, 0], [2, 3]],
                   values = [1., 2., 3.],
                   dense_shape = [3, 4])
print(s)
print(tf.sparse.to_dense(s))

# 操作
s2 = s * 2.0
# 没有+法操作  s3 = s + 1
    
s4 = tf.constant([[10., 20.],
                [30., 40.],
                [50., 60.],
                [70., 80.]])
print(tf.sparse.sparse_dense_matmul(s, s4))
# 如果indices排序不对,使用tf.sparse.reorder进行排序
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)
tf.Tensor(
[[ 30.  40.]
 [ 20.  40.]
 [210. 240.]], shape=(3, 2), dtype=float32)
# 变量 variables
v = tf.Variable([[1., 2., 3.], [4., 5., 6.]])
print(v)
print(v.value())
print(v.numpy())

# 只能用assign 不能用=
v.assign(2 * v)
print(v.numpy())
v[0, 1].assign(42)
print(v.numpy())
v[1].assign([7., 8., 9.])
print(v.numpy())
<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.  4.  6.]
 [ 8. 10. 12.]]
[[ 2. 42.  6.]
 [ 8. 10. 12.]]
[[ 2. 42.  6.]
 [ 7.  8.  9.]]

猜你喜欢

转载自www.cnblogs.com/flyuz/p/11770485.html