用Anaconda创建虚拟环境+tensorflow gpu 2.1.0+pycharm引入自建的虚拟环境+测试安装是否成功

打开Anaconda Prompt

在开始菜单找到Anaconda Prompt打开
在这里插入图片描述
或者打开Anaconda Navigator——CMD.exe Prompt
在这里插入图片描述
在这里插入图片描述

创建虚拟环境

在Anaconda Prompt内输入命令

conda crate -n 自取虚拟环境名称 python=所需版本号

如:
在这里插入图片描述
中间会让我们确认一下,输入y,再回车即可
在这里插入图片描述
创建成功后,进入新创建的环境:

activate 环境名

如:
在这里插入图片描述
在新创建的环境(我的是cnn-gpu)下,安装tensorflow gpu 2.1.0版本:
(注意这里是有两个等号,前面创建虚拟环境是一个等号)

conda install tensorflow-gpu==2.1.0

在这过程中,也需要输入y进行确认
在这里插入图片描述
果然,在这安装过程中,就那么不幸的报错了,有三个包没安装成功,分别是cuda、cudnn、mlk…呜呜呜,可能网络不好吧,http error一般是网络问题,那我就再重新输一次conda install tensorflow-gpu==2.1.0试试看
在这里插入图片描述
果然刚刚是网络问题,这次剩余的3个安装失败的包,成功了2个,就剩cudnn了,那就再来一次conda install吧
在这里插入图片描述
好啦,终于成功啦!
在这里插入图片描述

安装完毕后,我们需要补齐该虚拟环境缺失的其他库:

conda install anaconda

至此,就安装完毕啦。
接下来,打开pychram,创建一个py文件
右下角 选择Add Interpreter
在这里插入图片描述
这里选择Conda Environment,勾选Existing environment那项,然后Interpreter那里选择刚刚创建的虚拟环境的地址
在这里插入图片描述
刚刚创建的虚拟环境地址是在anaconda的安装路径下,envs文件内,cnn-gpu是刚刚创建的虚拟环境的名称,选择cnn-gpu文件内的python.exe,点击OK,其它内容默认不变即可。
在这里插入图片描述
简单小测试一下

import tensorflow as tf
gpu_device_name = tf.test.gpu_device_name()
print(gpu_device_name)

结果

C:\Users\txh\anaconda3\envs\cnn-gpu\python.exe "D:/Project/protein function prediction/test.py"
2021-08-29 17:12:12.164126: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cudart64_101.dll
2021-08-29 17:12:13.926861: I tensorflow/core/platform/cpu_feature_guard.cc:142] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX AVX2
2021-08-29 17:12:13.930080: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library nvcuda.dll
2021-08-29 17:12:15.418699: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1555] Found device 0 with properties: 
pciBusID: 0000:01:00.0 name: GeForce RTX 3060 Laptop GPU computeCapability: 8.6
coreClock: 1.425GHz coreCount: 30 deviceMemorySize: 6.00GiB deviceMemoryBandwidth: 312.97GiB/s
2021-08-29 17:12:15.418827: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cudart64_101.dll
2021-08-29 17:12:15.422981: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cublas64_10.dll
2021-08-29 17:12:15.425944: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cufft64_10.dll
2021-08-29 17:12:15.427487: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library curand64_10.dll
2021-08-29 17:12:15.430812: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cusolver64_10.dll
2021-08-29 17:12:15.432614: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cusparse64_10.dll
2021-08-29 17:12:15.439222: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cudnn64_7.dll
2021-08-29 17:12:15.439344: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1697] Adding visible gpu devices: 0
/device:GPU:0
2021-08-29 17:12:16.231530: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1096] Device interconnect StreamExecutor with strength 1 edge matrix:
2021-08-29 17:12:16.231623: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1102]      0 
2021-08-29 17:12:16.231677: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1115] 0:   N 
2021-08-29 17:12:16.231843: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1241] Created TensorFlow device (/device:GPU:0 with 4747 MB memory) -> physical GPU (device: 0, name: GeForce RTX 3060 Laptop GPU, pci bus id: 0000:01:00.0, compute capability: 8.6)

Process finished with exit code 0

还可以进一步测试一下是否调用到gpu,而不是只用cpu

import tensorflow as tf

tf.compat.v1.disable_eager_execution()

with tf.device('/cpu:0'):
    a = tf.constant([1.0, 2.0, 3.0], shape=[3], name='a')
    b = tf.constant([1.0, 2.0, 3.0], shape=[3], name='b')
with tf.device('/gpu:1'):
    c = a + b

sess = tf.compat.v1.Session(config=tf.compat.v1.ConfigProto(allow_soft_placement=True, log_device_placement=True))
sess.run(tf.compat.v1.global_variables_initializer())
print(sess.run(c))

结果:

C:\Users\txh\anaconda3\envs\cnn-gpu\python.exe "D:/Project/protein function prediction/test.py"
2021-08-29 16:55:52.149657: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cudart64_101.dll
2021-08-29 16:55:53.881584: I tensorflow/core/platform/cpu_feature_guard.cc:142] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX AVX2
2021-08-29 16:55:53.884395: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library nvcuda.dll
2021-08-29 16:55:55.445288: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1555] Found device 0 with properties: 
pciBusID: 0000:01:00.0 name: GeForce RTX 3060 Laptop GPU computeCapability: 8.6
coreClock: 1.425GHz coreCount: 30 deviceMemorySize: 6.00GiB deviceMemoryBandwidth: 312.97GiB/s
2021-08-29 16:55:55.445421: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cudart64_101.dll
2021-08-29 16:55:55.448492: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cublas64_10.dll
2021-08-29 16:55:55.451295: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cufft64_10.dll
2021-08-29 16:55:55.452412: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library curand64_10.dll
2021-08-29 16:55:55.455760: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cusolver64_10.dll
2021-08-29 16:55:55.457529: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cusparse64_10.dll
2021-08-29 16:55:55.463702: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cudnn64_7.dll
2021-08-29 16:55:55.463828: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1697] Adding visible gpu devices: 0
Device mapping:
/job:localhost/replica:0/task:0/device:GPU:0 -> device: 0, name: GeForce RTX 3060 Laptop GPU, pci bus id: 0000:01:00.0, compute capability: 8.6
add: (AddV2): /job:localhost/replica:0/task:0/device:GPU:0
init: (NoOp): /job:localhost/replica:0/task:0/device:GPU:0
a: (Const): /job:localhost/replica:0/task:0/device:CPU:0
b: (Const): /job:localhost/replica:0/task:0/device:CPU:0
2021-08-29 16:56:45.490608: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1096] Device interconnect StreamExecutor with strength 1 edge matrix:
2021-08-29 16:56:45.490694: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1102]      0 
2021-08-29 16:56:45.490742: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1115] 0:   N 
2021-08-29 16:56:45.490908: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1241] Created TensorFlow device (/job:localhost/replica:0/task:0/device:GPU:0 with 4747 MB memory) -> physical GPU (device: 0, name: GeForce RTX 3060 Laptop GPU, pci bus id: 0000:01:00.0, compute capability: 8.6)
2021-08-29 16:56:45.493253: I tensorflow/core/common_runtime/direct_session.cc:358] Device mapping:
/job:localhost/replica:0/task:0/device:GPU:0 -> device: 0, name: GeForce RTX 3060 Laptop GPU, pci bus id: 0000:01:00.0, compute capability: 8.6

2021-08-29 16:56:45.494291: I tensorflow/core/common_runtime/placer.cc:54] add: (AddV2): /job:localhost/replica:0/task:0/device:GPU:0
2021-08-29 16:56:45.494378: I tensorflow/core/common_runtime/placer.cc:54] init: (NoOp): /job:localhost/replica:0/task:0/device:GPU:0
2021-08-29 16:56:45.494466: I tensorflow/core/common_runtime/placer.cc:54] a: (Const): /job:localhost/replica:0/task:0/device:CPU:0
2021-08-29 16:56:45.494557: I tensorflow/core/common_runtime/placer.cc:54] b: (Const): /job:localhost/replica:0/task:0/device:CPU:0
[2. 4. 6.]

Process finished with exit code 0

出现以上结果都说明成功啦。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值