# 深度学习TF—2.TensorFlow2高阶操作

1147-柳同学

## 热门标签

,

### 一、合并与分割

#### 1. tf.concat-合并-原有的维度上进行累加

concat操作需要满足除拼接维度外，其余维度均相等

#### 2.tf.stack-合并-创造一个新的维度

stack操作需要Tensor维度均相等

### 三、张量排序

#### 3.案例

# 将无关信息屏蔽掉
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
import tensorflow as tf
tf.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形状
# 比较
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 res

if __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]



### 五、张量的限幅

#### 5.案例

# 将无关信息屏蔽掉
import os

os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
import tensorflow as tf
from tensorflow import keras
from 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 = 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))

# 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)

print('==before==')
print(tf.norm(g))

# 对所有可训练参数进行等比例裁剪

print('==after==')
print(tf.norm(g))
# update w' = w - lr*grad

if step % 100 == 0:
print(step, 'loss:', float(loss))

if __name__ == '__main__':
main()



### 六、高阶op

#### 1.tf.where—配合tf.gather_nd使用

tf.where(cond)—返回元素为True的坐标

tf.where(cond,A,B)—表示根据cond将A中的元素筛选后替换到B中相同位置

import tensorflow as tf

a = tf.ones([3, 3])
b = tf.zeros([3, 3])
mask = [[1, 0, 0], [0, 0, 1], [0, 1, 1]]
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)


#### 4.案例

import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'

import tensorflow as tf
import matplotlib.pyplot as plt

def fun(x):
"""

:param x: [b,2]
:return:
"""
z = tf.math.sin(x[...,0]) + tf.math.sin(x[...,1])
return z

if __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()


Vieu3.3主题

Q Q 登 录