TensorFlow学习日记38

1.启动tensorboard
解析:tensorboard --logdir ./data/autograph/

2.Tensorflow自动微分机制
解析:Tensorflow一般使用梯度磁带tf.GradientTape来记录正向运算过程,然后反播磁带自动得到梯度值。这种利用tf.GradientTape求微分的方法叫做Tensorflow的自动微分机制。

3.tf.GradientTape()求二阶导数
解析:

with tf.GradientTape() as tape2:
    with tf.GradientTape() as tape1:   
        y = a*tf.pow(x,2) + b*x + c
    dy_dx = tape1.gradient(y,x)   
dy2_dx2 = tape2.gradient(dy_dx,x)
print(dy2_dx2)

4.使用autograph求导数
解析:

@tf.function
def f(x):   
    a = tf.constant(1.0)
    b = tf.constant(-2.0)
    c = tf.constant(1.0)
    # 自变量转换成tf.float32
    x = tf.cast(x,tf.float32)
    with tf.GradientTape() as tape:
        tape.watch(x)
        y = a*tf.pow(x,2)+b*x+c
    dy_dx = tape.gradient(y,x) 
    return((dy_dx,y))
tf.print(f(tf.constant(0.0)))
tf.print(f(tf.constant(1.0)))

5.optimizer.minimize
解析:optimizer.minimize相当于先用tape求gradient,再apply_gradient

6.TensorFlow API
解析:
[1]TensorFlow的低阶API主要包括张量操作,计算图和自动微分。
[2]TensorFlow的中阶API主要包括各种模型层,损失函数,优化器,数据管道,特征列等等。
[3]TensorFlow的高阶API主要为tf.keras.models提供的模型的类接口。

7.使用Keras接口3种方式构建模型
[1]使用Sequential按层顺序构建模型
[2]使用函数式API构建任意结构模型
[3]继承Model基类构建自定义模型

8.MirroredStrategy过程
解析:
[1]训练开始前,该策略在所有N个计算设备上均各复制一份完整的模型;
[2]每次训练传入一个批次的数据时,将数据分成N份,分别传入N个计算设备[即数据并行];
[3]N个计算设备使用本地变量[镜像变量]分别计算自己所获得的部分数据的梯度;
[4]使用分布式计算的All-reduce操作,在计算设备间高效交换梯度数据并进行求和,使得最终每个设备都有了所有设备的梯度之和;
[5]使用梯度求和的结果更新本地变量[镜像变量];
[6]当所有设备均更新本地变量后,进行下一轮训练[即该并行策略是同步的]。

9.Ubuntu 18.04安装CUDA 10.2
解析:

sudo /usr/local/cuda-10.2/bin/cuda-uninstaller
sudo rm -rf /usr/local/cuda-10.2

# 彻底卸载N卡驱动
sudo apt remove --purge nvidia*

10.CUPTI
解析:The CUDA Profiling Tools Interface (CUPTI) enables the creation of profiling and tracing tools that target CUDA applications. CUPTI provides the following APIs:
[1]the Activity API
[2]the Callback API
[3]the Event API
[4]the Metric API
[5]the Profiler API

11.使用apt安装CUDA,Ubuntu 18.04 [CUDA 10.1]
解析:

# Add NVIDIA package repositories
wget https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64/cuda-repo-ubuntu1804_10.1.243-1_amd64.deb
sudo dpkg -i cuda-repo-ubuntu1804_10.1.243-1_amd64.deb
sudo apt-key adv --fetch-keys https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64/7fa2af80.pub
sudo apt-get update
wget http://developer.download.nvidia.com/compute/machine-learning/repos/ubuntu1804/x86_64/nvidia-machine-learning-repo-ubuntu1804_1.0.0-1_amd64.deb
sudo apt install ./nvidia-machine-learning-repo-ubuntu1804_1.0.0-1_amd64.deb
sudo apt-get update

# Install NVIDIA driver
sudo apt-get install --no-install-recommends nvidia-driver-418
# Reboot. Check that GPUs are visible using the command: nvidia-smi

# Install development and runtime libraries (~4GB)
sudo apt-get install --no-install-recommends \
   cuda-10-1 \
   libcudnn7=7.6.4.38-1+cuda10.1  \
   libcudnn7-dev=7.6.4.38-1+cuda10.1

# Install TensorRT. Requires that libcudnn7 is installed above.
sudo apt-get install -y --no-install-recommends libnvinfer6=6.0.1-1+cuda10.1 \
   libnvinfer-dev=6.0.1-1+cuda10.1 \
   libnvinfer-plugin6=6.0.1-1+cuda10.1

12.nvGRAPH
解析:图分析库。

13.successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
解析:意味着内核没有NUMA支持。非统一内存访问[NUMA]是一种用于多处理器的电脑记忆体设计,内存访问时间取决于处理器的内存位置。在NUMA下,处理器访问它自己的本地存储器的速度比非本地存储器[存储器的地方到另一个处理器之间共享的处理器或存储器]快一些。

14.tf.lite.TFLiteConverter
解析:TensorFlow Lite提供了转换TensorFlow模型,并在移动端[mobile]、嵌入式[embeded]和物联网[IoT]设备上运行TensorFlow模型所需的所有工具。

tf.lite.TFLiteConverter(funcs, trackable_obj=None)

15.AP和mAP
解析:
[1]AP是Precision-recall曲线下面的面积,通常来说一个越好的分类器,AP值越高。
[2]mAP是多个类别AP的平均值。这个mean的意思是对每个类的AP再求平均,得到的就是mAP的值,mAP的大小一定在[0,1]区间,越大越好。该指标是目标检测算法中最重要的一个。

16.cv2.waitkey(delaytime)
解析:不断刷新图像,频率时间为delay,单位为ms。

17.cv.waitKey(1)
解析:1毫秒关闭当前捕获的图像,显示下一个。

18.cv2.COLOR_BGR2RGB
解析:使用cv2.imread()读取彩色图像后得到的格式是BGR格式,像素值范围在0~255之间,通道格式为(H,W,C),想要显示RGB类型的图像要进行一步格式转换。

19.np.random.shuffle
解析:生成随机列表。

20.tf.transpose函数
解析:tf.transpose(a, perm = None, name = ‘transpose’)。将a进行转置,并且根据perm参数重新排列输出维度,这是对数据的维度的进行操作的形式。

21.tf.tile()函数
解析:TensorFlow中的tile()函数是用来对张量[Tensor]进行扩展的,其特点是对当前张量内的数据进行一定规则的复制,最终的输出张量维度不变。如下所示:

import tensorflow as tf

a = tf.constant([[1, 2], [3, 4], [5, 6]], dtype=tf.float32)
a1 = tf.tile(a, [2, 3])
with tf.Session() as sess:
    print(sess.run(a))
    print(sess.run(tf.shape(a)))
    print(sess.run(a1))
    print(sess.run(tf.shape(a1)))
=======
[[1. 2.]
 [3. 4.]
 [5. 6.]]
[3 2]
[[1. 2. 1. 2. 1. 2.]
 [3. 4. 3. 4. 3. 4.]
 [5. 6. 5. 6. 5. 6.]
 [1. 2. 1. 2. 1. 2.]
 [3. 4. 3. 4. 3. 4.]
 [5. 6. 5. 6. 5. 6.]]
[6 6]

22.np.nonzero
解析:np.nonzero函数是numpy中用于得到数组array中非零元素的位置[数组索引]的函数。

a=np.array([[True,False,True],[True,False,False]])
b=np.nonzero(a)
print(b)

返回结果为(array([0, 0, 1], dtype=int64), array([0, 2, 0], dtype=int64)),即下标为(0,0)、(0,2)、(1,0)的元素值不为0。

23.np.argmax()
解析:np.argmax()是numpy中获取array的某一个维度中数值最大的那个元素的索引。

24.Python中的::-1
解析:

import numpy as np
a=np.random.rand(5)
print(a)
[ 0.64061262  0.8451399   0.965673    0.89256687  0.48518743]

print(a[::-1]) # 取从后向前[相反]的元素
[ 0.48518743  0.89256687  0.965673    0.8451399   0.64061262]
print(a[2::-1]) #取从下标为2的元素翻转读取
[ 0.965673  0.8451399   0.64061262]

25.np.argsort()函数
解析:将元素从小到大排列,提取其对应的索引。

参考文献:
[1]自动微分机制:https://lyhue1991.github.io/eat_tensorflow2_in_30_days/2-3,%E8%87%AA%E5%8A%A8%E5%BE%AE%E5%88%86%E6%9C%BA%E5%88%B6.html
[2]GPU支持:https://www.tensorflow.org/install/gpu#linux_setup
[3]如何解释TensorFlow输出:https://cloud.tencent.com/developer/ask/62370/answer/105694
[4]如何将模型转换成tflite模型:https://www.cnblogs.com/happyamyhope/p/11822111.html
[5]tf.lite.TFLiteConverter:https://tensorflow.google.cn/versions/r2.1/api_docs/python/tf/lite/TFLiteConverter
[6]如何用TF Serving部署TensorFlow模型:https://cloud.tencent.com/developer/article/1422844

你可能感兴趣的:(深度学习)