如果网站曾被挂木马,企业网站建设的策划书,招聘软件开发工程师,长春网站建设q479185700強01 深度学习框架TensorFlow2快速入门教程
目录结构
01 概述 02 准备OVF虚拟机镜像 03 导入Ubuntu22的初始化环境 04 使用VMWare拍摄快照进行备份 05 Docker环境的测试和使用 06 安装Nvidia容器工具包 07 GPU支持的TensorFlow的环境搭建和踩坑 08 拉取非GPU支持的TensorFlow镜…01 深度学习框架TensorFlow2快速入门教程
目录结构
01 概述 02 准备OVF虚拟机镜像 03 导入Ubuntu22的初始化环境 04 使用VMWare拍摄快照进行备份 05 Docker环境的测试和使用 06 安装Nvidia容器工具包 07 GPU支持的TensorFlow的环境搭建和踩坑 08 拉取非GPU支持的TensorFlow镜像 09 启动TensorFlow环境并访问测试 10 优化TensorFlow环境 11 导出TensorFlow环境为OVF镜像 12 在Ubuntu22中安装Anaconda3 13 在Ubuntu22中安装Vscode 14 在Ubuntu22中使用pip安装tensorflow 15 在Ubuntu22中安装CUDA驱动 16 导出支持GPU的TensorFlow环境为OVF镜像 17 创建各种维度的张量 18 将张量转换为numpy 19 将两个张量相加 20 将两个张量进行元素相乘 21 将两个张量进行矩阵相乘 22 张量的一些简单的聚合运算 23 变量的基本用法 24 总结
视频教程 环境搭建
安装
官方地址https://tensorflow.google.cn/install?hlzh-cn
通过pip安装
请从 PyPI 中选择以下某个 TensorFlow 软件包进行安装
tensorflow支持 CPU 和 GPU 的最新稳定版适用于 Ubuntu 和 Windows。tf-nightly预览 build不稳定。Ubuntu 和 Windows 均包含 GPU 支持。tensorflow1.15TensorFlow 1.x 的最终版本。
pip install tensorflow安装驱动包
参考地址https://docs.nvidia.com/datacenter/cloud-native/container-toolkit/latest/install-guide.html
Configure the production repository:
curl -fsSL https://nvidia.github.io/libnvidia-container/gpgkey | sudo gpg --dearmor -o /usr/share/keyrings/nvidia-container-toolkit-keyring.gpg curl -s -L https://nvidia.github.io/libnvidia-container/stable/deb/nvidia-container-toolkit.list | sed s#deb https://#deb [signed-by/usr/share/keyrings/nvidia-container-toolkit-keyring.gpg] https://#g | sudo tee /etc/apt/sources.list.d/nvidia-container-toolkit.listOptionally, configure the repository to use experimental packages:
sudo sed -i -e /experimental/ s/^#//g /etc/apt/sources.list.d/nvidia-container-toolkit.listUpdate the packages list from the repository:
sudo apt-get updateInstall the NVIDIA Container Toolkit packages:
sudo apt-get install -y nvidia-container-toolkit添加GPU支持
Docker 是在 GPU 上运行 TensorFlow 的最简单方法因为主机只需安装 NVIDIA® 驱动程序而不必安装 NVIDIA® CUDA® 工具包。
安装 Nvidia 容器工具包以向 Docker 添加 NVIDIA® GPU 支持。nvidia-container-runtime 仅适用于 Linux。
检查 GPU 是否可用
lspci | grep -i nvidia验证 nvidia-docker 安装效果
docker run --gpus all --rm nvidia/cuda nvidia-smi通过Docker安装
官方 TensorFlow Docker 映像位于 tensorflow/tensorflow Docker Hub 代码库中。映像版本按照以下格式进行标记
标记说明latestTensorFlow CPU 二进制映像的最新版本。默认版本nightlyTensorFlow 映像的每夜版。不稳定version指定 TensorFlow 二进制映像的版本例如2.1.0develTensorFlow master 开发环境的每夜版。包含 TensorFlow 源代码。custom-op用于开发 TF 自定义操作的特殊实验性映像。
每个基本标记都有会添加或更改功能的变体
标记变体说明tag-gpu支持 GPU 的指定标记版本。tag-jupyter针对 Jupyter 的指定标记版本包含 TensorFlow 教程笔记本
下载支持GPU的TensorFlow
docker pull tensorflow/tensorflow:latest-gpu-jupyterTensorFlow Docker 映像已经过配置可运行 TensorFlow。Docker 容器可在虚拟环境中运行是设置 GPU 支持的最简单方法。
docker run -it -p 8888:8888 tensorflow/tensorflow:latest-gpu-jupyter浏览器访问http://localhost:8888
使用非GPU支持的TensorFlow
docker pull tensorflow/tensorflow:latest-jupyter
docker run -it -p 8888:8888 tensorflow/tensorflow:latest-jupyter之前的命令有两个问题
不能开机自动启动的每次关闭以后会有一个垃圾容器存在需要手动的去删除
这两个问题怎么优化呢
docker run --restartalways --name tensorflow -itd -p 8888:8888 tensorflow/tensorflow:latest-jupyter这个时候有个新的问题怎么获取token
docker logs -f --tail100 tensorflow使用pip安装tensorflow
命令
pip install tensorflow在Ubuntu22中安装CUDA驱动
# 第一步安装gcc
sudo apt install gcc -y# 第二步下载CUDA
wget https://developer.download.nvidia.com/compute/cuda/11.7.1/local_installers/cuda_11.7.1_515.65.01_linux.run# 第三步安装
sudo sh cuda_11.7.1_515.65.01_linux.run# 第四步配置环境变量
export PATH/usr/local/cuda-11.7/bin${PATH::${PATH}}
export LD_LIBRARY_PATH/usr/local/cuda-11.7/lib64${LD_LIBRARY_PATH::${LD_LIBRARY_PATH}}# 第五步安装依赖
sudo apt install nvidia-cuda-toolkit# 第六步检查
nvcc -V张量
张量基本用法
张量本质上是一种数组。
导入依赖
import tensorflow as tf
import numpy as np创建基础的张量
# 创建int32类型的0维张量
rank_0_tensor tf.constant(4)
print(rank_0_tensor)# 创建float32类型的1维张量
t1 tf.constant([2.0, 3, 4])
print(t1)# 创建二维张量
t2 tf.constant([[1,2],[3,4],[5,6]], dtypetf.float32)
print(t2)创建一个三维的张量
t3 tf.constant([[[1],[2]],[[3],[4]],[[5],[6]]])
print(t3)张量是通过tf.constant()创建闯入的值是一个数组这个数组是几维的张量就是几维。比较特殊的是如果传入的是一个常量那么这个张量就是0维的张量。
张量转Numpy
方法1
np.array(t2)方法2
t2.numpy()张量求和
要求两个张量的形状是一样的。会让对应索引的元素分别相加。
举例
a [[1,2],[3,4]]
b [[3,3],[4,4]]a b 每个索引位置对应相加 [[13,23],[34,44]] [[4,5],[7,8]]示例代码
a tf.constant([[1,2],[3,4]])
b tf.constant([[3,3],[4,4]])tf.add(a, b)张量元素乘法
指的是让每个对应索引的元素分别相乘。
距离
a [[1,2],[3,4]]
b [[3,3],[4,4]]a b 每个索引位置对应相乘 [[1x3,2x3],[3x4,4x4]] [[3,6],[12,16]]示例代码
a tf.constant([[1,2],[3,4]])
b tf.constant([[3,3],[4,4]])tf.multiply(a, b)张量矩阵乘法
矩阵的乘法比较复杂我们从简单到难度去记忆。
当一个矩阵和一个向量相乘的时候要求矩阵行的元素个数等于向量的元素个数。结果是矩阵的每行和向量相乘然后求和这些和组成的新的一列形成最后的结果。这个说起来比较复杂举个简单的例子
[[1,2],[3,4]] 乘 [3,3] [1, 2] 乘 [3, 3] [1*3 2*3] 9[3, 4] 乘 [3, 3] [3*3 4*3] 21所以结果是[9, 21]当一个矩阵和另一个矩阵相乘的时候要求矩阵和行数和列数是相同的。然后用矩阵的每一行与另一个矩阵的每一列相乘再相加最终得到一个新的矩阵。还是举个简单的例子
[[1,2],[3,4]] 乘 [[2,3],[2,1]] [1,2] 乘 [2,2] , [1,2] 乘 [3,1] [6,5][3,4] 乘 [2,2] , [3,4] 乘 [3,1] [14,13]所以结果是[[6,5],[14,13]]举例2
a
[[1,2],[3,4]
]b [[3,3],[4,4]
]
[[1,2]x[3,4],[1,2]x[3,4],[3,4]x[3,4],[3,4]x[3,4]
]
[[1x32x4, 1x32x4],[3x34x4, 3x34x4]
]
[[11, 11],[25, 25]
]示例代码
# 矩阵乘以矩阵
a tf.constant([[1,2],[3,4]])
b tf.constant([[2,3],[2,1]])
tf.matmul(a, b)# 矩阵乘以矩阵
a tf.constant([[1,2],[3,4]])
b tf.constant([[3,3],[4,4]])
tf.matmul(a, b)张量求最大值
cst tf.constant([[1,2,3],[3,33,333]])
tf.reduce_max(cst)张量求最小值
cst tf.constant([[1,2,3],[3,33,333]])
tf.reduce_min(cst)张量求和
cst tf.constant([[1,2,3],[3,33,333]])
tf.reduce_sum(cst)张量求平均值
cst tf.constant([[1,2,3],[3,33,333]])
tf.reduce_mean(cst)张量求最大值索引
cst tf.constant([[1,2,3],[3,33,333]])
tf.argmax(cst)张量求最小值索引
cst tf.constant([[1,2,3],[3,33,333]])
tf.argmin(cst)变量
TensorFlow中的变量是一种特殊的张量形状不可以改变但是可以改变其中的参数值。定义的方法是
tv tf.Variable([[1,2],[3,4]])
tv通过.shape可以查看形状通过.dtype可以查看数据类型通过.numpy可以转换为numpy的数组类型。
print(tv.dtype)
print(tv.shape)
print(tv.numpy)通过.assign(数组)的方式可以修改变量的内容。需要注意的是新的数组必须和旧数组的形状是一样的。
tv.assign([[2,2],[3,4]])