tensorflow对应版本cuda和cudnn版本对照参考
anaconda安装
设定anaconda镜像源
安装CUDA
安装cudnn
配置环境变量
tensorflow-gpu安装
pycharm安装
附上MNIST代码
conda 常用命令
tensorflow | CUDA | cuDNN | Python | Anaconda |
---|---|---|---|---|
2.4.0 | 11.0 | 8.0 | Python3.8 | Anaconda 2020.11 |
需要的可以自取
链接:https://pan.baidu.com/s/1UcvTWXQSgId4p9mEpccrkg
提取码:dhcc
返回目录
从官网中下载windows
版本anaconda。
下载个人版本够用了,下面放的截止至文档编写完成当日最新版的anaconda
下载链接
anaconda
windows64位下载链接anaconda
Linux64位下载链接选择 All user
安装路径选择
D:\ProgramFIles\anaconda
全部勾选
返回目录
打开
Anaconda prompt
首先创建.condarc
文件
.condarc
文件默认是不存在的,但当用户第一次运行conda config
命令时,将会在windows系统中的C:\Users\dhcc
路径下创建该文件。
用记事本打开该文件将下面任一脚本代码替代原有代码复制并保存。#beiji channels: - defaults show_channel_urls: true channel_alias: https://mirrors.bfsu.edu.cn/anaconda default_channels: - https://mirrors.bfsu.edu.cn/anaconda/pkgs/main - https://mirrors.bfsu.edu.cn/anaconda/pkgs/free - https://mirrors.bfsu.edu.cn/anaconda/pkgs/r - https://mirrors.bfsu.edu.cn/anaconda/pkgs/pro - https://mirrors.bfsu.edu.cn/anaconda/pkgs/msys2 custom_channels: conda-forge: https://mirrors.bfsu.edu.cn/anaconda/cloud msys2: https://mirrors.bfsu.edu.cn/anaconda/cloud bioconda: https://mirrors.bfsu.edu.cn/anaconda/cloud menpo: https://mirrors.bfsu.edu.cn/anaconda/cloud pytorch: https://mirrors.bfsu.edu.cn/anaconda/cloud simpleitk: https://mirrors.bfsu.edu.cn/anaconda/cloud
channels: - defaults show_channel_urls: true default_channels: - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/r - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/msys2 custom_channels: conda-forge: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud msys2: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud bioconda: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud menpo: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud pytorch: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud simpleitk: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
#在conda prompt输入使config生效 conda config --set show_channel_urls yes
返回目录
CUDA官网下载
安装路径选择``
同意并继续
选择自定义安装
去掉Visual Studio Integration
若
Driver components
中的Display Driver
当前版本大于新版本也将钩去掉
选择安装位置
选择默认即可, 记住安装路径。配置环境变量需要用到。
完成安装
返回目录
这里放一个cudnn官网链接,下载cudnn
需要注册Nvidia
账号,可能需要科学上网。
解压
cudnn
并将cuda
文件夹名改为cudnn
放在C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.0
目录下
将
CUDA
和cuDNN
库添加到环境变量中C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.0\cudnn\bin C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.0\extras\CUPTI\lib64 C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.0\bin C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.0\libnvvp
配置环境变量后重启电脑
返回目录
(tensorflow1) C:\Users\dhcc>python
Python 3.8.6 | packaged by conda-forge | (default, Jan 25 2021, 22:54:47) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow as tf
2021-02-27 16:24:32.243502: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library cudart64_110.dll
>>> tf.test.is_gpu_available()
2021-02-27 16:27:25.706998: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1720] Found device 0 with properties:
pciBusID: 0000:83:00.0 name: Tesla V100-SXM2-16GB computeCapability: 7.0
coreClock: 1.53GHz coreCount: 80 deviceMemorySize: 15.88GiB deviceMemoryBandwidth: 836.37GiB/s
2021-02-27 16:27:25.719334: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library cudart64_110.dll
2021-02-27 16:27:25.725227: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library cublas64_11.dll
2021-02-27 16:27:25.731455: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library cublasLt64_11.dll
2021-02-27 16:27:25.737729: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library cufft64_10.dll
2021-02-27 16:27:25.744441: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library curand64_10.dll
2021-02-27 16:27:25.752021: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library cusolver64_10.dll
2021-02-27 16:27:25.759194: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library cusparse64_11.dll
2021-02-27 16:27:25.766735: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library cudnn64_8.dll
2021-02-27 16:27:25.782919: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1862] Adding visible gpu devices: 0
2021-02-27 16:27:26.528352: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1261] Device interconnect StreamExecutor with strength 1 edge matrix:
2021-02-27 16:27:26.534888: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1267] 0
2021-02-27 16:27:26.539445: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1280] 0: N
2021-02-27 16:27:26.558320: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1406] Created TensorFlow device (/device:GPU:0 with 14827 MB memory) -> physical GPU (device: 0, name: Tesla V100-SXM2-16GB, pci bus id: 0000:83:00.0, compute capability: 7.0)
2021-02-27 16:27:26.569970: I tensorflow/compiler/jit/xla_gpu_device.cc:99] Not creating XLA devices, tf_xla_enable_xla_devices not set
True
>>> tf.config.list_physical_devices('GPU')
2021-02-27 16:40:04.176880: I tensorflow/compiler/jit/xla_cpu_device.cc:41] Not creating XLA devices, tf_xla_enable_xla_devices not set
2021-02-27 16:40:04.187789: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1720] Found device 0 with properties:
pciBusID: 0000:83:00.0 name: Tesla V100-SXM2-16GB computeCapability: 7.0
coreClock: 1.53GHz coreCount: 80 deviceMemorySize: 15.88GiB deviceMemoryBandwidth: 836.37GiB/s
2021-02-27 16:40:04.199739: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library cudart64_110.dll
2021-02-27 16:40:04.205925: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library cublas64_11.dll
2021-02-27 16:40:04.211750: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library cublasLt64_11.dll
2021-02-27 16:40:04.218167: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library cufft64_10.dll
2021-02-27 16:40:04.224249: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library curand64_10.dll
2021-02-27 16:40:04.229874: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library cusolver64_10.dll
2021-02-27 16:40:04.235953: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library cusparse64_11.dll
2021-02-27 16:40:04.242297: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library cudnn64_8.dll
2021-02-27 16:40:04.259506: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1862] Adding visible gpu devices: 0
[PhysicalDevice(name='/physical_device:GPU:0', device_type='GPU')]
返回目录
安装Pycharm一直点下一步就可以
安装完成后需要将anaconda
环境变量添加进pycharm中
Create New Project
选项Interpreter
下拉框后面的按钮Conda Environment
后面的按钮Exiting environment
返回目录
import os
os.environ['TF_CPP_MIN_LOG_LEVEL']='2'
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers, optimizers, datasets
(x, y), (x_val, y_val) = datasets.mnist.load_data()
x = tf.convert_to_tensor(x, dtype=tf.float32) / 255.
y = tf.convert_to_tensor(y, dtype=tf.int32)
y = tf.one_hot(y, depth=10)
print(x.shape, y.shape)
train_dataset = tf.data.Dataset.from_tensor_slices((x, y))
train_dataset = train_dataset.batch(200)
model = keras.Sequential([
layers.Dense(512, activation='relu'),
layers.Dense(256, activation='relu'),
layers.Dense(10)])
optimizer = optimizers.Adam(learning_rate=0.001)
def train_epoch(epoch):
# Step4.loop
for step, (x, y) in enumerate(train_dataset):
with tf.GradientTape() as tape:
# [b, 28, 28] => [b, 784]
x = tf.reshape(x, (-1, 28*28))
# Step1. compute output
# [b, 784] => [b, 10]
out = model(x)
# Step2. compute loss
loss = tf.reduce_sum(tf.square(out - y)) / x.shape[0]
# Step3. optimize and update w1, w2, w3, b1, b2, b3
grads = tape.gradient(loss, model.trainable_variables)
# w' = w - lr * grad
optimizer.apply_gradients(zip(grads, model.trainable_variables))
if step % 100 == 0:
print(epoch, step, 'loss:', loss.numpy())
def train():
for epoch in range(30):
train_epoch(epoch)
if __name__ == '__main__':
train()
返回目录
1. conda --version #查看conda版本,验证是否安装
2. conda update conda #更新至最新版本,也会更新其它相关包
3. conda update --all #更新所有包
4. conda update package_name #更新指定的包
5. conda create -n env_name package_name #创建名为env_name的新环境,并在该环境下安装名为package_name 的包
可以指定新环境的版本号,例如:
conda create -n python2 python=python2.7 numpy pandas,
创建了python2环境,python版本为2.7,同时还安装了numpy pandas包
6. source activate env_name #切换至env_name环境
7. source deactivate #退出环境
8. conda info -e #显示所有已经创建的环境
9. conda create --name new_env_name --clone old_env_name #复制old_env_name为new_env_name
10. conda remove --name env_name –all #删除环境
11. conda list #查看所有已经安装的包
12. conda install package_name #在当前环境中安装包
13. conda install --name env_name package_name #在指定环境中安装包
14. conda remove -- name env_name package #删除指定环境中的包
15. conda remove package #删除当前环境中的包
16. conda create -n tensorflow_env tensorflow
conda activate tensorflow_env #conda 安装tensorflow的CPU版本
17. conda create -n tensorflow_gpuenv tensorflow-gpu
conda activate tensorflow_gpuenv #conda安装tensorflow的GPU版本
18. conda env remove -n env_name #采用第10条的方法删除环境失败时,可采用这种方法
19. conda install --channel https://conda.anaconda.org/anaconda tensorflow=1.8.0
提供一个下载地址,使用上面命令就可安装1.8.0版本tensorflow
conda clean -p 删除一些没用的包,这个命令会检查哪些包没有在包缓存中被硬依赖到其他地方,并删除它们
conda clean -t可以将conda保存下来的tar打包。
conda clean -y -all //删除所有的安装包及cache
这里给出tensorflow对应版本cuda和cudnn版本对照参考。