1. 合并与分割
1.1. concat
- 维度必须相同,指定哪个维度要合并
1.2. stack: create new dim
- Statistics about scores
- School1:[classes, students, scores]
- School2:[classes, students, scores]
- [
schools
, classes, students, scores]
- Dim mismatch
1.3. Unstack
1.4. Split
- VS unstack
2. 数据统计
2.1. tf.norm
- Here talks about Vector Norm
- L1 Norm
2.2. tf.reduce_min/max
2.3. tf.argmax/argmin
2.4. tf.equal
2.5. tf.unique
3. 张量的排序
3.1. Sort, argsort
3.2. Top_k
- Only return top-k values and indices
3.3. Top-k accuracy
- Prob:[0.1, 0.2, 0.3, 0.4]
- Label:[2]
- Only consider top-1 prediction: [3]
- Only consider top-2 prediction: [3, 2]
- Only consider top-3 prediction: [3, 2, 1]
import tensorflow as tf
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
tf.random.set_seed(2467)
def accuracy(output, target, top_k=(1,)):
max_k = max(top_k)
batch_size = target.shape[0]
print("max_k: ", max_k)
print("output: ", output)
# 返回输出结果中top_k的元素排列的索引
pred = tf.math.top_k(output, max_k).indices
pred = tf.transpose(pred, perm=[1, 0])
target_ = tf.broadcast_to(target, pred.shape)
# [10, b]
print("pred-transpose: ", pred, "target_:", target_)
correct = tf.equal(pred, target_)
print(correct)
res = []
for k in top_k:
correct_k = tf.cast(tf.reshape(correct[:k], [-1]),dtype=tf.float32)
print("correct[:%d]: %s" % (k, correct[:k]))
print("correct_k: " , correct_k)
correct_k = tf.reduce_sum(correct_k)
print("corrent_k:%d, batch_size:%d" % (correct_k, batch_size))
acc = float(correct_k * (100 / batch_size))
res.append(acc)
return res
if __name__ == '__main__':
# 输出结果,模拟结果输出是每一个类别的概率
output = tf.random.normal([10, 6])
# 使得输出结果为六个类别的概率和为1
output = tf.math.softmax(output, axis=1)
# 目标值(10个样本, 6个类别)
target = tf.random.uniform([10], maxval=6, dtype=tf.int32)
# 打印结果
print("prob: ", output.numpy())
pred = tf.argmax(output, axis=1)
# 打印预测结果的最大值索引
print("pred: ", pred.numpy())
# 打印真实值
print("label: ", target.numpy())
acc = accuracy(output, target, top_k=(1, 2, 3, 4, 5, 6))
print("top-1-6 acc: ", acc)
4. 数据的填充与复制
4.1. Pad
- Image padding
4.2. tile
4.3. broadcast_to VS tile
- 广播与tile相比,不会开辟新内存,在运行的时候进行优化
5. 张量的限幅
5.1. clip_by_value
5.2. relu
5.3. clip_by_norm
5.4. gradient clipping
- Gradient Exploding or vanishing (梯度爆炸或消失)
- set lr=1
- new_grads, total_norm = tf.clip_by_global_norm(grads, 25)
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers, datasets, optimizers
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
print(tf.__version__)
(x, y), _ = datasets.mnist.load_data()
x = tf.convert_to_tensor(x, dtype=tf.float32) / 50
y = tf.convert_to_tensor(y)
y = tf.one_hot(y, depth=10)
print('x: ', x.shape, 'y:', y.shape)
train_db = tf.data.Dataset.from_tensor_slices((x, y)).batch(128).repeat(30)
x, y = next(iter(train_db))
print('sample:', x.shape, y.shape)
def main():
# 784 -> 512
w1, b1 = tf.Variable(tf.random.truncated_normal([784, 512], stddev=0.1)), tf.Variable(tf.zeros([512]))
# 512 -> 256
w2, b2 = tf.Variable(tf.random.truncated_normal([512, 256], stddev=0.1)), tf.Variable(tf.zeros([256]))
# 256 -> 10
w3, b3 = tf.Variable(tf.random.truncated_normal([256, 10], stddev=0.1)), tf.Variable(tf.zeros([10]))
optimizer = optimizers.SGD(lr=0.01)
for step, (x, y) in enumerate(train_db):
# [b, 28, 28] -> [b, 784]
x = tf.reshape(x, (-1, 784))
with tf.GradientTape() as tape:
# layer1
h1 = x @ w1 + b1
h1 = tf.nn.relu(h1)
# layer2
h2 = h1 @ w2 + b2
h2 = tf.nn.relu(h2)
# output
out = h2 @ w3 + b3
# compute loss
loss = tf.square(y - out)
# [b, 10] => [b]
loss = tf.reduce_mean(loss, axis=1)
loss = tf.reduce_mean(loss)
# compute gradient
grads = tape.gradient(loss, [w1, b1, w2, b2, w3, b3])
print('==before==')
for g in grads:
print(tf.norm(g))
grads, _ = tf.clip_by_global_norm(grads, 15)
print('==after==')
for g in grads:
print(tf.norm(g))
optimizer.apply_gradients(zip(grads, [w1, b1, w2, b2, w3, b3]))
if step % 100 == 0:
print(step, 'loss:', float(loss))
if __name__ == '__main__':
main()
- 注意: 一般tf.norm 范述范围在0~20内是比较理想的
6. 高阶OP
6.1. where
- where(cond, A, B)
6.2. scatter_nd
6.3. meshgrid
6.3.1. GPU acceleration
- x: [-2~2]
- y: [-2~2]
- Points: [N, 2]
6.3.2. Code
import tensorflow as tf
import matplotlib.pyplot as plt
def func(x):
"""
:param x: [b, 2]
:return:
"""
z = tf.math.sin(x[...,0]) + tf.math.sin(x[...,1])
return z
x = tf.linspace(0., 2*3.14, 500)
y = tf.linspace(0., 2*3.14, 500)
# [50, 50]
point_x, point_y = tf.meshgrid(x, y)
# [50, 50, 2]
points = tf.stack([point_x, point_y], axis=2)
# points = tf.reshape(points, [-1, 2])
print('points:', points.shape)
z = func(points)
print('z:', z.shape)
plt.figure('plot 2d func value')
plt.imshow(z, origin='lower', interpolation='none')
plt.colorbar()
plt.figure('plot 2d func contour')
plt.contour(point_x, point_y, z)
plt.colorbar()
plt.show()
7. 需要全套课程视频+PPT+代码资源可以私聊我
- 方式1:
CSDN
私信我! - 方式2:
QQ邮箱
:594042358@qq.com
或者直接加我QQ
:594042358
!