环境搭建
1、安装Python3.9.7。
2、修改pip源。在C:\Users\{你的用户名}\目录下创建一个名称是pip的文件夹,然后在文件夹中创建一个名为pip.ini的文件,在里面输入以下内容。
[global]
index-url=http://mirrors.aliyun.com/pypi/simple/
[install]
trusted-host=mirrors.aliyun.com
3、升级pip。
pip install --user --upgrade pip
4、安装vc_redist。
下载地址:https://docs.microsoft.com/zh-CN/cpp/windows/latest-supported-vc-redist?view=msvc-170
5、安装tensorflow。
pip install --user --upgrade tensorflow
6、验证安装效果。
python -c "import tensorflow as tf; print(tf.reduce_sum(tf.random.normal([1000, 1000])))"
7、安装显卡驱动。
CUDA® 11.2 要求 450.80.02 或更高版本。
下载地址:https://www.nvidia.com/download/index.aspx?lang=en-us
8、安装CUDA® 11.2。(需要安装附带的CUPTI组件)
下载地址:https://developer.nvidia.com/cuda-toolkit-archive
9、安装cuDNN SDK 8.1.0。
下载地址:https://developer.nvidia.com/cudnn
将cuDNN解压到C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.2文件夹。
10、运行mnist_convnet.py查看结果。
2022-03-20 19:59:59.905384: I tensorflow/core/platform/cpu_feature_guard.cc:151] 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.
2022-03-20 20:00:00.481565: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1525] Created device /job:localhost/replica:0/task:0/device:GPU:0 with 1673 MB memory: -> device: 0, name: NVIDIA GeForce RTX 3050 Laptop GPU, pci bus id: 0000:01:00.0, compute capability: 8.6
Epoch 1/5
2022-03-20 20:00:02.261836: I tensorflow/stream_executor/cuda/cuda_dnn.cc:368] Loaded cuDNN version 8100
2022-03-20 20:00:04.870372: I tensorflow/stream_executor/cuda/cuda_blas.cc:1786] TensorFloat-32 will be used for the matrix multiplication. This will only be logged once.
938/938 [==============================] - 11s 6ms/step - loss: 0.1746 - accuracy: 0.9453
Epoch 2/5
938/938 [==============================] - 5s 6ms/step - loss: 0.0474 - accuracy: 0.9851
Epoch 3/5
938/938 [==============================] - 6s 6ms/step - loss: 0.0345 - accuracy: 0.9893
Epoch 4/5
938/938 [==============================] - 6s 6ms/step - loss: 0.0252 - accuracy: 0.9920
Epoch 5/5
938/938 [==============================] - 6s 6ms/step - loss: 0.0190 - accuracy: 0.9939
313/313 [==============================] - 1s 2ms/step - loss: 0.0275 - accuracy: 0.9912
- 9911999702453613