112-Basic Tensor Operation

IntroductionToTensor
What’s a Tensor?

1. Create Tensor Object

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import tensorflow as tf
import numpy as np

# 1.创建张量对象
x = tf.constant([1,2,3,4,5])
print(x)

y = tf.ones((1,5))
print(y)

z = tf.zeros((1,5))
print(z)

q = tf.range(start=1, limit=6, delta=1)
print(q)

print("\n---------------------------------------------------------------\n")

2. tensor.ndim

1
2
3
4
5
6
7
8
9
10
11
12
# 2. 张量的维度: Rank-0()
x = tf.constant(1.0)
print("Rank-0张量:", " size=", tf.size(x).numpy(), "rank=", x.ndim, "shape=", x.shape, "dtype=", x.dtype)

y = tf.constant([1,2])
print("Rank-1张量:", " size=", tf.size(y).numpy(), "rank=", y.ndim, "shape=", y.shape, "dtype=", x.dtype)

z = tf.constant([[1,2], [3,4]])
print("Rank-2张量:", " size=", tf.size(z).numpy(), "rank=", z.ndim, "shape=", z.shape, "dtype=", x.dtype)

u = tf.constant([[[1,2], [3,4]], [[5,6],[7,8]]])
print("Rank-3张量:", " size=", tf.size(u).numpy(), "rank=", u.ndim, "shape=", u.shape, "dtype=", x.dtype)
tf.Tensor([1 2 3 4 5], shape=(5,), dtype=int32)
tf.Tensor([[1. 1. 1. 1. 1.]], shape=(1, 5), dtype=float32)
tf.Tensor([[0. 0. 0. 0. 0.]], shape=(1, 5), dtype=float32)
tf.Tensor([1 2 3 4 5], shape=(5,), dtype=int32)

---------------------------------------------------------------

Rank-0张量:  size= 1 rank= 0 shape= () dtype= <dtype: 'float32'>
Rank-1张量:  size= 2 rank= 1 shape= (2,) dtype= <dtype: 'float32'>
Rank-2张量:  size= 4 rank= 2 shape= (2, 2) dtype= <dtype: 'float32'>
Rank-3张量:  size= 8 rank= 3 shape= (2, 2, 2) dtype= <dtype: 'float32'>

3. Tensor Slice

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# 3. 张量运算
s = tf.constant([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
print("first tensor:", s[0].numpy())
print("last tensor:", s[-1].numpy())
print("slice:", s[0:-1].numpy())
print("slice:", s[-1:-6:-1].numpy())
print("sequence slice:", s[::1].numpy())
print("reverse slice:", s[::-1].numpy())

print("\n", "*"*100, "\n")

m = tf.range(20)
m = tf.reshape(m, [4,5])
print(m.numpy())
print(m[1:3, 2:4].numpy())
first tensor: 0
last tensor: 10
slice: [0 1 2 3 4 5 6 7 8 9]
slice: [10  9  8  7  6]
sequence slice: [ 0  1  2  3  4  5  6  7  8  9 10]
reverse slice: [10  9  8  7  6  5  4  3  2  1  0]

[[ 0  1  2  3  4]
 [ 5  6  7  8  9]
 [10 11 12 13 14]
 [15 16 17 18 19]]

[[ 7  8]
 [12 13]]

4. Basic Operation

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
# 4. 张量的基本运算
a = tf.constant([[1.0,2.0], [3.0,4.0]])
b = tf.constant([[5.0,6.0], [7.0,8.0]])
print("加法:", (a+b).numpy())
print("加法:", tf.add(a,b).numpy())

print("\n---------------------------------------------------------------\n")

print("减法:", (a-b).numpy())
print("减法:", tf.subtract(a,b).numpy())

print("\n---------------------------------------------------------------\n")

print("乘法:", (a * b).numpy())
print("乘法:", tf.multiply(a,b).numpy())

print("\n---------------------------------------------------------------\n")
print("矩阵乘法:", tf.matmul(a, b).numpy())

print("\n---------------------------------------------------------------\n")
print("reduce_max:", tf.reduce_max(a))
print("reduce_min:", tf.reduce_min(a))
print("reduce_sum:", tf.reduce_sum(a))
print("reduce_argmax:", tf.argmax(a).numpy())
print("reduce_argmin:", tf.argmin(a).numpy())
print("reduce_softmax:", tf.nn.softmax(a).numpy())

print("\n---------------------------------------------------------------\n")

t = tf.constant([[1,2,3],[4,5,6],[7,8,9],[10,11,12]])
print("reshape [3,4]:", tf.reshape(t, [3,4]).numpy())
print("reshape [6,1]:", tf.reshape(t, [6,2]).numpy())
print("reshape [12,1]:", tf.reshape(t, [12,1]).numpy())
print("reshape [-1]:", tf.reshape(t, [-1]).numpy())

print("\n---------------------------------------------------------------\n")
# broadcast
m = tf.constant([1,2])
n = tf.constant([[3,4], [5,6]])
print(m+n)
加法: [[ 6.  8.]
 [10. 12.]]
加法: [[ 6.  8.]
 [10. 12.]]

---------------------------------------------------------------

减法: [[-4. -4.]
 [-4. -4.]]
减法: [[-4. -4.]
 [-4. -4.]]

---------------------------------------------------------------

乘法: [[ 5. 12.]
 [21. 32.]]
乘法: [[ 5. 12.]
 [21. 32.]]

---------------------------------------------------------------

矩阵乘法: [[19. 22.]
 [43. 50.]]

---------------------------------------------------------------

reduce_max: tf.Tensor(4.0, shape=(), dtype=float32)
reduce_min: tf.Tensor(1.0, shape=(), dtype=float32)
reduce_sum: tf.Tensor(10.0, shape=(), dtype=float32)
reduce_argmax: [1 1]
reduce_argmin: [0 0]
reduce_softmax: [[0.26894143 0.7310586 ]
 [0.26894143 0.7310586 ]]

---------------------------------------------------------------

reshape [3,4]: [[ 1  2  3  4]
 [ 5  6  7  8]
 [ 9 10 11 12]]
reshape [6,1]: [[ 1  2]
 [ 3  4]
 [ 5  6]
 [ 7  8]
 [ 9 10]
 [11 12]]
reshape [12,1]: [[ 1]
 [ 2]
 [ 3]
 [ 4]
 [ 5]
 [ 6]
 [ 7]
 [ 8]
 [ 9]
 [10]
 [11]
 [12]]
reshape [-1]: [ 1  2  3  4  5  6  7  8  9 10 11 12]

---------------------------------------------------------------

tf.Tensor(
[[4 6]
 [6 8]], shape=(2, 2), dtype=int32)

5. Other Tensor

1
2
3
4
5
6
7
8
# Ragged Tensor
print("Ragged Tensor:", tf.ragged.constant([[1,2,3], [4,5], [6]]))

# String Tensor
print("String Tensor:", tf.constant(["Hello World", "This is TensorFlow"]))

# Sparse Tensor
print("Sparse Tensor:", tf.sparse.SparseTensor(indices=[[0,0], [2,2], [4,4]], values=[10, 20, 30], dense_shape=[5,5]))
Ragged Tensor: <tf.RaggedTensor [[1, 2, 3], [4, 5], [6]]>
String Tensor: tf.Tensor([b'Hello World' b'This is TensorFlow'], shape=(2,), dtype=string)
Sparse Tensor: SparseTensor(indices=tf.Tensor(
[[0 0]
 [2 2]
 [4 4]], shape=(3, 2), dtype=int64), values=tf.Tensor([10 20 30], shape=(3,), dtype=int32), dense_shape=tf.Tensor([5 5], shape=(2,), dtype=int64))