本文共 5967 字,大约阅读时间需要 19 分钟。
concat操作需要满足除拼接维度外,其余维度均相等
stack操作需要Tensor维度均相等
不能指定打散的数量,只能按维度进行分割
可以指定打散的数量
二范数
# 将无关信息屏蔽掉import osos.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'import tensorflow as tftf.random.set_seed(2467)# output->[b,n] target->[b,]def accuracy(output,target,topk=(1,)): maxk = max(topk) batch_size = target.shape[0] # 返回最大值前maxk个的索引 pred = tf.math.top_k(output,maxk).indices # 转置 pred = tf.transpose(pred,perm=[1,0]) # 将target广播成pred形状 target_ = tf.broadcast_to(target,pred.shape) # 比较 correct = tf.equal(pred,target_) res = [] for k in topk: correct_k = tf.cast(tf.reshape(correct[:k],[-1]),dtype=tf.float32) # print('123=',correct_k) correct_k = tf.reduce_sum(correct_k) acc = float(correct_k / batch_size) res.append(acc) return resif __name__ == '__main__': # 正态分布 output = tf.random.normal([10,6]) # 使6类概率总和为1 output = tf.math.softmax(output,axis=1) # 均匀分布 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,topk=(1,2,3,4,5,6)) print('top-1-6 acc:',acc)
prob: [[0.25310278 0.21715644 0.16043882 0.13088997 0.04334083 0.19507109] [0.05892418 0.04548917 0.00926314 0.14529602 0.66777605 0.07325139] [0.09742808 0.08304427 0.07460099 0.04067177 0.626185 0.07806987] [0.20478569 0.12294924 0.12010485 0.13751231 0.36418733 0.05046057] [0.11872064 0.31072393 0.12530336 0.1552888 0.2132587 0.07670452] [0.01519807 0.09672114 0.1460476 0.00934331 0.5649092 0.16778067] [0.04199061 0.18141054 0.06647632 0.6006175 0.03198383 0.07752118] [0.09226219 0.2346089 0.13022321 0.16295874 0.05362028 0.3263266 ] [0.07019574 0.0861177 0.10912605 0.10521299 0.2152082 0.4141393 ] [0.01882887 0.26597694 0.19122466 0.24109262 0.14920162 0.13367532]]pred: [0 4 4 4 1 4 3 5 5 1]label: [0 2 3 4 2 4 2 3 5 5]top-1-6 acc: [0.4000000059604645, 0.4000000059604645, 0.5, 0.699999988079071, 0.800000011920929, 1.0]
Gradient clipping梯度裁剪
# 将无关信息屏蔽掉import osos.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'import tensorflow as tffrom tensorflow import kerasfrom tensorflow.keras import datasets, layers, optimizers# 列出你所有的物理GPU,设置内存自动增长gpus = tf.config.experimental.list_physical_devices('GPU')for gpu in gpus: tf.config.experimental.set_memory_growth(gpu, True)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)# print(x[0], y[0])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 # out = tf.nn.relu(out) # compute loss # [b, 10] - [b, 10] loss = tf.square(y - out) # [b, 10] => [b] loss = tf.reduce_mean(loss, axis=1) # [b] => scalar 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)) # update w' = w - lr*grad 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.where(cond)—返回元素为True的坐标
import tensorflow as tfa = tf.ones([3, 3])b = tf.zeros([3, 3])mask = [[1, 0, 0], [0, 0, 1], [0, 1, 1]]c = tf.convert_to_tensor(mask)cc = tf.cast(c, dtype=tf.bool)print(tf.where(cc, b, a))
tf.Tensor([[0. 1. 1.] [1. 1. 0.] [1. 0. 0.]], shape=(3, 3), dtype=float32)
import osos.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'import tensorflow as tfimport matplotlib.pyplot as pltdef fun(x): """ :param x: [b,2] :return: """ z = tf.math.sin(x[...,0]) + tf.math.sin(x[...,1]) return zif __name__ == '__main__': x = tf.linspace(0.,2*3.14,500) y = tf.linspace(0.,2*3.14,500) # [500,500] point_x, point_y = tf.meshgrid(x,y) # [500,500,2] points = tf.stack([point_x,point_y],axis=2) print('points:',points.shape) z = fun(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()
转载地址:http://uzsv.baihongyu.com/