一、TensorFlow简介
TensorFlow是由Google Brain团队开发的一款开源的机器学习框架,是目前广泛应用于各种深度学习领域的重要工具之一。TensorFlow可以处理各种不同类型的数据,如图像、语音、文本等,具有很高的灵活性和可扩展性。
二、安装TensorFlow-cpu版本
1、安装tensorflow2.6
以tensorflow_gpu-2.6.0的安装为例,查表可获知该版本依赖于python3.6-3.9。
conda create -n tensorflow2-6-p3-9 python=3.9
conda activate tensorflow2-6-p3-9
conda install tensorflow==2.6
2、安装tensorflow2.15
conda未支持tensorflow=2.6以上版本的安装,采用pip方式进行安装。解决对numpy和protobuf依赖冲突问题。同时以在Linux环境下安装tensorflow_decision_forests为例:
conda create -n tensorflow215-p39 python=3.9
conda activate tensorflow215-p39
pip install tensorflow==2.15.0
pip install numpy==1.24.3 protobuf==3.20.3
pip install tensorflow_decision_forests==1.8.1
TensorFlow Decision Forests(TF-DF)是一个基于TensorFlow的开源库,用于训练、解释和服务决策森林模型,包括随机森林和梯度提升树等。在 Windows 环境中,tensorflow_decision_forests依赖的inference.so 文件等自定义 C++ 操作并不会被加载,因此某些高级功能可能不可用。
三、安装CUDA
CUDA(Compute Unified Device Architecture)是NVIDIA推出的一个并行计算平台和编程模型,它允许开发者使用NVIDIA的GPU进行通用计算。而cuDNN(CUDA Deep Neural Network Library)是基于CUDA的一个专门针对深度神经网络加速的库,它提供了一系列高度优化的算法,用于加速深度学习模型的训练和推理。
1、查看GPU驱动版本
查看GPU驱动版本,也就是我们“CUDA Version”,Windows 11 版本中一般是(最高支持)12.0版本,键盘上同时按win +r,输入cmd,打开命令窗口,在命令窗口输入:nvidia-smi
nvidia-smi
Wed Oct 30 14:38:15 2024
+---------------------------------------------------------------------------------------+
| NVIDIA-SMI 538.18 Driver Version: 538.18 CUDA Version: 12.2 |
|-----------------------------------------+----------------------+----------------------+
| GPU Name TCC/WDDM | Bus-Id Disp.A | Volatile Uncorr. ECC |
| Fan Temp Perf Pwr:Usage/Cap | Memory-Usage | GPU-Util Compute M. |
| | | MIG M. |
|=========================================+======================+======================|
| 0 NVIDIA GeForce MX150 WDDM | 00000000:02:00.0 Off | N/A |
| N/A 42C P0 N/A / ERR! | 0MiB / 2048MiB | 0% Default |
| | | N/A |
+-----------------------------------------+----------------------+----------------------+
+---------------------------------------------------------------------------------------+
| Processes: |
| GPU GI CI PID Type Process name GPU Memory |
| ID ID Usage |
|=======================================================================================|
| No running processes found |
+---------------------------------------------------------------------------------------+
2、cuda下载和安装
下载cuda和cuDNN。在官网上下载对应的cuda和cuDNN,版本可以低于上面查到的CUDA版本但不能高于电脑支持的版本。
cuda下载地址:CUDA Toolkit Archive | NVIDIA Developer
cudnn下载地址:cuDNN Archive | NVIDIA Developer
参考本社区文章《安装CUDA和cuDNN》。
四、安装tensorflow-gpu版本
安装前一定要查看自己电脑的环境配置,然后查询Tensorflow-gpu、Python、 cuda 、 cuDNN 版本关系,必须 一 一对应!
1、Tensorflow-gpu 与 Python、 cuda、cuDNN 版本关系
查询官方网址:
然后可以去NIVIDIA官网查询一下自己电脑显卡的算力:https://developer.nvidia.com/cuda-gpus,建议算力>=3.5安装。
GPU:
Version | Python version | Compiler | Build tools | cuDNN | CUDA |
---|---|---|---|---|---|
tensorflow-2.18.0 | 3.9-3.12 | Clang 17.0.6 | Bazel 6.5.0 | 9.3 | 12.5 |
tensorflow-2.17.0 | 3.9-3.12 | Clang 17.0.6 | Bazel 6.5.0 | 8.9 | 12.3 |
tensorflow-2.16.1 | 3.9-3.12 | Clang 17.0.6 | Bazel 6.5.0 | 8.9 | 12.3 |
tensorflow-2.15.0 | 3.9-3.11 | Clang 16.0.0 | Bazel 6.1.0 | 8.9 | 12.2 |
tensorflow-2.14.0 | 3.9-3.11 | Clang 16.0.0 | Bazel 6.1.0 | 8.7 | 11.8 |
tensorflow-2.13.0 | 3.8-3.11 | Clang 16.0.0 | Bazel 5.3.0 | 8.6 | 11.8 |
tensorflow-2.12.0 | 3.8-3.11 | GCC 9.3.1 | Bazel 5.3.0 | 8.6 | 11.8 |
tensorflow-2.11.0 | 3.7-3.10 | GCC 9.3.1 | Bazel 5.3.0 | 8.1 | 11.2 |
tensorflow-2.10.0 | 3.7-3.10 | GCC 9.3.1 | Bazel 5.1.1 | 8.1 | 11.2 |
tensorflow-2.9.0 | 3.7-3.10 | GCC 9.3.1 | Bazel 5.0.0 | 8.1 | 11.2 |
tensorflow-2.8.0 | 3.7-3.10 | GCC 7.3.1 | Bazel 4.2.1 | 8.1 | 11.2 |
tensorflow-2.7.0 | 3.7-3.9 | GCC 7.3.1 | Bazel 3.7.2 | 8.1 | 11.2 |
tensorflow-2.6.0 | 3.6-3.9 | GCC 7.3.1 | Bazel 3.7.2 | 8.1 | 11.2 |
tensorflow-2.5.0 | 3.6-3.9 | GCC 7.3.1 | Bazel 3.7.2 | 8.1 | 11.2 |
tensorflow-2.4.0 | 3.6-3.8 | GCC 7.3.1 | Bazel 3.1.0 | 8.0 | 11.0 |
tensorflow-2.3.0 | 3.5-3.8 | GCC 7.3.1 | Bazel 3.1.0 | 7.6 | 10.1 |
tensorflow-2.2.0 | 3.5-3.8 | GCC 7.3.1 | Bazel 2.0.0 | 7.6 | 10.1 |
tensorflow-2.1.0 | 2.7, 3.5-3.7 | GCC 7.3.1 | Bazel 0.27.1 | 7.6 | 10.1 |
tensorflow-2.0.0 | 2.7, 3.3-3.7 | GCC 7.3.1 | Bazel 0.26.1 | 7.4 | 10.0 |
tensorflow_gpu-1.15.0 | 2.7, 3.3-3.7 | GCC 7.3.1 | Bazel 0.26.1 | 7.4 | 10.0 |
tensorflow_gpu-1.14.0 | 2.7, 3.3-3.7 | GCC 4.8 | Bazel 0.24.1 | 7.4 | 10.0 |
tensorflow_gpu-1.13.1 | 2.7, 3.3-3.7 | GCC 4.8 | Bazel 0.19.2 | 7.4 | 10.0 |
tensorflow_gpu-1.12.0 | 2.7, 3.3-3.6 | GCC 4.8 | Bazel 0.15.0 | 7 | 9 |
tensorflow_gpu-1.11.0 | 2.7, 3.3-3.6 | GCC 4.8 | Bazel 0.15.0 | 7 | 9 |
tensorflow_gpu-1.10.0 | 2.7, 3.3-3.6 | GCC 4.8 | Bazel 0.15.0 | 7 | 9 |
tensorflow_gpu-1.9.0 | 2.7, 3.3-3.6 | GCC 4.8 | Bazel 0.11.0 | 7 | 9 |
tensorflow_gpu-1.8.0 | 2.7, 3.3-3.6 | GCC 4.8 | Bazel 0.10.0 | 7 | 9 |
tensorflow_gpu-1.7.0 | 2.7, 3.3-3.6 | GCC 4.8 | Bazel 0.9.0 | 7 | 9 |
tensorflow_gpu-1.6.0 | 2.7, 3.3-3.6 | GCC 4.8 | Bazel 0.9.0 | 7 | 9 |
tensorflow_gpu-1.5.0 | 2.7, 3.3-3.6 | GCC 4.8 | Bazel 0.8.0 | 7 | 9 |
tensorflow_gpu-1.4.0 | 2.7, 3.3-3.6 | GCC 4.8 | Bazel 0.5.4 | 6 | 8 |
tensorflow_gpu-1.3.0 | 2.7, 3.3-3.6 | GCC 4.8 | Bazel 0.4.5 | 6 | 8 |
tensorflow_gpu-1.2.0 | 2.7, 3.3-3.6 | GCC 4.8 | Bazel 0.4.5 | 5.1 | 8 |
tensorflow_gpu-1.1.0 | 2.7, 3.3-3.6 | GCC 4.8 | Bazel 0.4.2 | 5.1 | 8 |
tensorflow_gpu-1.0.0 | 2.7, 3.3-3.6 | GCC 4.8 | Bazel 0.4.2 | 5.1 | 8 |
2、安装tensorflow-gpu=1.14
以tensorflow_gpu-1.14的安装为例,查表可获知该版本依赖于python3.6~3.7。
conda config --add channels anaconda
conda create -n tensorflow114-p36 python=3.6
conda activate tensorflow114-p36
conda install tensorflow-gpu=1.14
查看已安装的tensorflow版本信息:
(tensorflow114-p36) C:\Users\86138>conda list tensor
# packages in environment at D:\App-Data\conda3\envs\tensorflow114-p36:
#
# Name Version Build Channel
tensorboard 1.14.0 py36he3c9ec2_0 anaconda
tensorflow 1.14.0 gpu_py36heb2afb7_0 anaconda
tensorflow-base 1.14.0 gpu_py36h9ee611f_0 anaconda
tensorflow-estimator 1.14.0 py_0 anaconda
tensorflow-gpu 1.14.0 h0d30ee6_0 anaconda
常用开发库numpy、pandas、scikit-learn版本信息:
conda list | findstr "numpy pandas scikit-learn"
numpy 1.19.2 py36hadc3359_0 anaconda
numpy-base 1.19.2 py36ha3acd2a_0 anaconda
pandas 1.1.5 py36hd77b12b_0 anaconda
scikit-learn 0.24.2 py36hf11a4ad_1 anaconda
验证:执行python
python -c "import tensorflow as tf; print(tf.__version__)"
# 输出 1.14.0
检查 TensorFlow 是否可以访问 GPU
import tensorflow as tf
# 检查 TensorFlow 是否可以访问 GPU
print("GPU 是否可用: ", tf.test.is_gpu_available())
# 获取 TensorFlow 可用的 GPU 设备列表
gpus = tf.test.gpu_device_name()
if gpus:
print("可用的 GPU 设备: ", gpus)
else:
print("没有检测到 GPU 设备")
输出:
2024-10-30 12:06:31.004756: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1326] Created TensorFlow device (/device:GPU:0 with 1350 MB memory) -> physical GPU (device: 0, name: NVIDIA GeForce MX150, pci bus id: 0000:02:00.0, compute capability: 6.1)
GPU 是否可用: True
可用的 GPU 设备: /device:GPU:0
基于tensorflow1.x,安装独立的Keras库,可以在命令行使用以下命令进行安装:
conda install keras
conda list keras
# keras 2.3.1 0 anaconda
3、安装tensorflow-gpu=2.6.0
以tensorflow_gpu-2.6.0的安装为例,查表可获知该版本依赖于python3.6-3.9。
conda create -n tensorflow260 python=3.9
conda activate tensorflow260
conda install tensorflow-gpu==2.6.0
a、依赖NumPy版本1.20.3
查看已安装的tensorflow版本信息:
conda list tensor
# packages in environment at D:\App-Data\conda3\envs\tensorflow260:
#
# Name Version Build Channel
tensorboard 2.6.0 py_1 anaconda
tensorboard-data-server 0.6.1 py39haa95532_0 anaconda
tensorboard-plugin-wit 1.8.1 py39haa95532_0 anaconda
tensorflow 2.6.0 gpu_py39he88c5ba_0 anaconda
tensorflow-base 2.6.0 gpu_py39hb3da07e_0 anaconda
tensorflow-estimator 2.6.0 pyh7b7c402_0 anaconda
tensorflow-gpu 2.6.0 h17022bd_0 anaconda
导入tensorflow开发包后发现有个警告,说numpy的版本太高了。
(tensorflow260) C:\>python -c "import tensorflow as tf; print(tf.__version__)"
C:\AppData\Conda-Data\envs\tensorflow260\lib\site-packages\tensorflow\python\framework\dtypes.py:585: FutureWarning: In the future `np.object` will be defined as the corresponding NumPy scalar.
np.object,
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "C:\AppData\Conda-Data\envs\tensorflow260\lib\site-packages\tensorflow\__init__.py", line 41, in <module>
from tensorflow.python.tools import module_util as _module_util
...
(tensorflow260) C:\>conda list numpy
# packages in environment at C:\AppData\Conda-Data\envs\tensorflow260:
#
# Name Version Build Channel
numpy 1.26.4 py39h055cbcc_0
numpy-base 1.26.4 py39h65a83cf_0
根据 TensorFlow 2.6 的实际需求,NumPy 版本一般为 1.19.x 或 1.20.x。检测到我的环境中已安装的版本是1.26,需降低NumPy 版本号。
conda install numpy==1.20.3
# 若使用 pandas
conda install pandas==1.3.5
常用开发库numpy、pandas、scikit-learn、tqdm版本信息:
conda list | findstr "numpy pandas scikit-learn tqdm"
numpy 1.20.3 py39h749eb61_1 anaconda
numpy-base 1.20.3 py39h5bfbeaa_1 anaconda
pandas 1.3.5 py39h6214cd6_0 anaconda
scikit-learn 1.6.1 py39hdd013cc_0 conda-forge
tqdm 4.67.1 py39h9909e9c_0 anaconda
执行tensorflow开发包的函数,查看版本信息:
python -c "import tensorflow as tf; print(tf.__version__)"
# 输出:2.6.0
检测tensorflow对GPU的支持情况:
test-tensorflow-gpu.py
import tensorflow as tf
# 检查 TensorFlow 是否可以访问 GPU
# print("GPU 是否可用: ", tf.test.is_gpu_available())
print("GPU 是否可用: ", tf.config.list_physical_devices('GPU'))
# 获取 TensorFlow 可用的 GPU 设备列表
gpus = tf.test.gpu_device_name()
if gpus:
print("可用的 GPU 设备: ", gpus)
else:
print("没有检测到 GPU 设备")
执行:python test-tensorflow-gpu.py
GPU 是否可用: [PhysicalDevice(name='/physical_device:GPU:0', device_type='GPU')]
2024-11-28 12:36:21.185202: I tensorflow/core/platform/cpu_feature_guard.cc:142] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations: AVX AVX2
To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.
2024-11-28 12:36:22.293439: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1510] Created device /device:GPU:0 with 1328 MB memory: -> device: 0, name: NVIDIA GeForce MX150, pci bus id: 0000:02:00.0, compute capability: 6.1
可用的 GPU 设备: /device:GPU:0
说明:从 TensorFlow 2.0 开始,Keras 被官方整合为 TensorFlow 的高级 API(tensorflow.keras),成为其核心组件之一。tensorflow.keras 包含了所有核心 Keras 功能(模型构建、层、优化器、损失函数等),并与 TensorFlow 生态(如 tensorflow.data、SavedModel)深度集成。
b、tf.keras与独立 Keras 包的区别
独立 Keras 包:如果通过 pip install keras 安装,会安装独立的 Keras 库(非 TensorFlow 集成版本)。 但 TensorFlow 2.6 不需要此操作,且可能导致版本冲突。
推荐做法:始终优先使用 tensorflow.keras,避免混合使用独立 Keras 和 TensorFlow 内置 Keras。
测试 Keras 基础功能方法:
python -c "import tensorflow as tf; print('TF Version:', tf.__version__);"
# 输出 TF Version: 2.6.0
python -c "from tensorflow.keras import layers; print('Dense layer test:', layers.Dense(10))"
# 输出:Dense layer test: <tensorflow.python.keras.layers.core.Dense object at 0x00000201583C42B0>
4、安装tensorflow-gpu=2.15.0
conda未支持tensorflow-gpu=2.6以上版本的安装,采用pip安装方式。
conda create -n tensorflow-gpu215-p39 python=3.9
conda activate tensorflow-gpu215-p39
pip install tensorflow==2.15
安装结果:
Successfully installed MarkupSafe-3.0.2 absl-py-2.1.0 astunparse-1.6.3 cachetools-5.5.1 certifi-2024.12.14 charset-normalizer-3.4.1 flatbuffers-25.1.21 gast-0.6.0 google-auth-2.37.0 google-auth-oauthlib-1.2.1 google-pasta-0.2.0 grpcio-1.69.0 h5py-3.12.1 idna-3.10 importlib-metadata-8.6.1 keras-2.15.0 libclang-18.1.1 markdown-3.7 ml-dtypes-0.2.0 numpy-1.26.4 oauthlib-3.2.2 opt-einsum-3.4.0 packaging-24.2 protobuf-4.25.5 pyasn1-0.6.1 pyasn1-modules-0.4.1 requests-2.32.3 requests-oauthlib-2.0.0 rsa-4.9 six-1.17.0 tensorboard-2.15.2 tensorboard-data-server-0.7.2 tensorflow-2.15.0 tensorflow-estimator-2.15.0 tensorflow-intel-2.15.0 tensorflow-io-gcs-filesystem-0.31.0 termcolor-2.5.0 typing-extensions-4.12.2 urllib3-2.3.0 werkzeug-3.1.3 wrapt-1.14.1 zipp-3.21.0
验证:
python -c "import tensorflow as tf; print(tf.__version__)"
# 输出:2.15.0