目录

基本操作

# Tensor

Tensor集成了一系列数据类型,包括scalar,vector,matrix

  • scalar : 1.1
  • vector : [1.1],[1.1,2.2,...]
  • matrix : [[1,2,3],[4,5,6]]

支持类型

  • int, float, double
  • bool
  • string
import tensorflow as tf

a = tf.constant(1)
# <tf.Tensor: id=2, shape=(), dtype=int32, numpy=1>

a = tf.constant(1.) # 加上.则变为浮点型
# <tf.Tensor: id=4, shape=(), dtype=float32, numpy=1>

a = tf.constant(2.2, dtype=tf.int32) # 报错,给定数值与给定类型不符
# ERROR

a = tf.constant([True, False])
# <tf.Tensor: id=9, shape=(2,), dtype=bool, numpy=array([True, False])>

a = tf.constant('hello,world.')
# <tf.Tensor: id=14, shape=(), dtype=string, numpy=b'hello,world.'>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

额外补充

a.device # 查看当前tensor所在设备的名称

a.numpy() # 返回一个numpy对象

a.ndim() # 查看数据维度

tf.is_tensor(a) # 查看a是否为tensor类型

a.dtype == tf.float32 # 查看a的类型是否是float32

tf.fill([2,2],0) # 创建一个2x2的矩阵,并以0填充
1
2
3
4
5
6
7
8
9
10
11

类型转换

b = np.arange(5) #array([0,1,2,3,4])

tf.convert_to_tensor(b) # 将b转化为tensor

tf.convert_to_tensor(b,dtype=tf.int32) # 将b转化为tensor,且类型为int32

tf.cast(b, dtype=tf.float32) # 将一个tensor类型的数据转换数据类型
1
2
3
4
5
6
7

Normal

tf.random.normal([2,2],mean=1,stddev=1) # 生成一个平均值为1,方差为1的正态分布矩阵

tf.random.uniform([2,2],minval=0,maxval=1) # 从0~1分布中均匀地采样

idx = tf.range(10)
tf.random.shuffle(idx) # 随即打散
1
2
3
4
5
6

Loss

out = tf.random.uniform([4,10])
y = tf.range(4)
y = tf.one_hot(y,depth=10)

loss = tf.keras.losses.mse(y,out)
loss = tf.reduce_mean(loss)
1
2
3
4
5
6

# 索引与切片

# 传统方式
a = tf.constant([[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]])
a[0][0] # 1. tf.Tensor(1, shape=(), dtype=int32)

# Numpy风格
a[0,0] # 1. tf.Tensor(1, shape=(), dtype=int32)
a[0:2,0:2]
'''
<tf.Tensor: shape=(2, 2), dtype=int32, numpy=
array([[1, 2],
       [5, 6]])>
'''
a[:,2] # <tf.Tensor: shape=(4,), dtype=int32, numpy=array([ 3,  7, 11, 15])>
1
2
3
4
5
6
7
8
9
10
11
12
13

随意采样

a = tf.constant([[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]])


tf.gather(a,axis=0,indices=[0,3]) # 数据源a,以行为准,取第0行以及第3行

tf.gather_nd(a,[0,1]) # 数据源a,取第0个元素中的第1个元素. 结果为2
1
2
3
4
5
6

# 数学运算

  • +,-,*,/
  • **,pow,square
  • sqrt
  • //,%
  • exp,log
  • @,matmul
  • linear layer
a = tf.constant([[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]])
b = tf.constant([[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]])

a+b
'''
<tf.Tensor: shape=(4, 4), dtype=int32, numpy=
array([[ 2,  4,  6,  8],
       [10, 12, 14, 16],
       [18, 20, 22, 24],
       [26, 28, 30, 32]])>
'''

a-b
'''
<tf.Tensor: shape=(4, 4), dtype=int32, numpy=
array([[0, 0, 0, 0],
       [0, 0, 0, 0],
       [0, 0, 0, 0],
       [0, 0, 0, 0]])>
'''

a*b
'''
<tf.Tensor: shape=(4, 4), dtype=int32, numpy=
array([[  1,   4,   9,  16],
       [ 25,  36,  49,  64],
       [ 81, 100, 121, 144],
       [169, 196, 225, 256]])>
'''

a/b
'''
<tf.Tensor: shape=(4, 4), dtype=int32, numpy=
array([[  1,   4,   9,  16],
       [ 25,  36,  49,  64],
       [ 81, 100, 121, 144],
       [169, 196, 225, 256]])>
'''

a@b  # 矩阵乘法
'''
<tf.Tensor: shape=(4, 4), dtype=int32, numpy=
array([[ 90, 100, 110, 120],
       [202, 228, 254, 280],
       [314, 356, 398, 440],
       [426, 484, 542, 600]])>
'''

a = tf.constant([[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]],dtype=float)
tf.math.log(a)
'''
<tf.Tensor: shape=(4, 4), dtype=float32, numpy=
array([[0.       , 0.6931472, 1.0986123, 1.3862944],
       [1.609438 , 1.7917595, 1.9459102, 2.0794415],
       [2.1972246, 2.3025851, 2.3978953, 2.4849067],
       [2.5649493, 2.6390574, 2.7080503, 2.7725887]], dtype=float32)>
'''

tf.math.exp(a)
'''
<tf.Tensor: shape=(4, 4), dtype=float32, numpy=
array([[2.7182817e+00, 7.3890562e+00, 2.0085537e+01, 5.4598148e+01],
       [1.4841316e+02, 4.0342880e+02, 1.0966332e+03, 2.9809580e+03],
       [8.1030840e+03, 2.2026467e+04, 5.9874145e+04, 1.6275480e+05],
       [4.4241341e+05, 1.2026044e+06, 3.2690175e+06, 8.8861110e+06]],
      dtype=float32)>
'''
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67

增维 有时,两个矩阵间计算,由于维度不同,无法直接运算,如:

a.shape # TensorShape([4,2,3])
b.shape # TensorShape([3,5])
1
2

我们可以用tf.broadcast_to函数,将b进行增维

bb = tf.broadcast_to(b,[4,3,5])
a@bb # 此时可以运算了
1
2
最近更新
01
基本知识
07-18
02
卷积神经网络识别图像
07-18
03
损失函数
07-18
更多文章>