Tensorflow-gpu2.0.0安装以及tensorflow-gpu安装成功的测试程序

原本:

测试代码:

import tensorflow as tf
import timeit

with tf.device('/cpu:0'):
	cpu_a = tf.random.normal([10000, 1000])
	cpu_b = tf.random.normal([1000, 2000])
	print(cpu_a.device, cpu_b.device)

with tf.device('/gpu:0'):
	gpu_a = tf.random.normal([10000, 1000])
	gpu_b = tf.random.normal([1000, 2000])
	print(gpu_a.device, gpu_b.device)

def cpu_run():
	with tf.device('/cpu:0'):
		c = tf.matmul(cpu_a, cpu_b)
	return c

def gpu_run():
	with tf.device('/gpu:0'):
		c = tf.matmul(gpu_a, gpu_b)
	return c

# warm up
cpu_time = timeit.timeit(cpu_run, number=10)
gpu_time = timeit.timeit(gpu_run, number=10)
print('warmup:', cpu_time, gpu_time)

cpu_time = timeit.timeit(cpu_run, number=10)
gpu_time = timeit.timeit(gpu_run, number=10)
print('run time:', cpu_time, gpu_time)

输出信息:

E:\anaconda3\envs\tf_gpu200\python.exe E:/pycharmProject/helloWorld.py
2019-11-04 23:06:03.772940: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cudart64_100.dll
2019-11-04 23:06:04.772659: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library nvcuda.dll
2019-11-04 23:06:04.797803: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1618] Found device 0 with properties: 
name: GeForce RTX 2070 SUPER major: 7 minor: 5 memoryClockRate(GHz): 1.77
pciBusID: 0000:08:00.0
2019-11-04 23:06:04.797917: I tensorflow/stream_executor/platform/default/dlopen_checker_stub.cc:25] GPU libraries are statically linked, skip dlopen check.
2019-11-04 23:06:04.798299: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1746] Adding visible gpu devices: 0
2019-11-04 23:06:04.798570: I tensorflow/core/platform/cpu_feature_guard.cc:142] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2
2019-11-04 23:06:04.800913: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1618] Found device 0 with properties: 
name: GeForce RTX 2070 SUPER major: 7 minor: 5 memoryClockRate(GHz): 1.77
pciBusID: 0000:08:00.0
2019-11-04 23:06:04.801028: I tensorflow/stream_executor/platform/default/dlopen_checker_stub.cc:25] GPU libraries are statically linked, skip dlopen check.
2019-11-04 23:06:04.801406: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1746] Adding visible gpu devices: 0
2019-11-04 23:06:05.265320: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1159] Device interconnect StreamExecutor with strength 1 edge matrix:
2019-11-04 23:06:05.265399: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1165]      0 
2019-11-04 23:06:05.265443: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1178] 0:   N 
2019-11-04 23:06:05.266070: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1304] Created TensorFlow device (/job:localhost/replica:0/task:0/device:GPU:0 with 6283 MB memory) -> physical GPU (device: 0, name: GeForce RTX 2070 SUPER, pci bus id: 0000:08:00.0, compute capability: 7.5)
/job:localhost/replica:0/task:0/device:CPU:0 /job:localhost/replica:0/task:0/device:CPU:0
/job:localhost/replica:0/task:0/device:GPU:0 /job:localhost/replica:0/task:0/device:GPU:0
2019-11-04 23:06:06.226104: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cublas64_100.dll
warmup: 0.6706975 0.20009929999999998
run time: 0.6638971000000001 0.0005713999999998887

Process finished with exit code 0

安装过程:

        英伟达的驱动按照 这个 视频安装的,想安装驱动的可以去看一下(里面还给了地址,不过我下的很慢,你可以右键那个下载按钮,复制地址然后使用迅雷下载,超级快)。安装tensorflow-gpu的话正常安装就行了,先用conda创建虚拟环境,激活,然后使用下面的命令就能成功了

# -i https://pypi.doubanio.com/simple
pip install tensorflow-gpu -i https://pypi.tuna.tsinghua.edu.cn/simple

先前我想先不用tensorflow-gpu 2.0.0版本,安装一下低版本,安装了1.14版本,最后测试是否能使用tensoflow-gpu时,发现最后输出:

ImportError: Could not find 'cudnn64_7.dll'.

 从网上查找可知,cudnn版本和tensorflow版本有冲突。当然只能修改其中一个了,为了少折腾,最后还是安装了tensorflow-gpu 2.0.0版本的。

更新:

        原来使用pip安装tensorflow-gpu 1.14.0版本出现上面的错误。突然发现使用conda install tensorflow-gpu 1.14.0版本竟然成功了。不过,通过conda安装的tf有点奇怪,奇怪有两点:

  1. pip list输出的内容和conda list输出的内容不一样,如下(发现没?一个显示装了tensorflow-gpu的,一个显示装了tensorflow-cpu的):

     

     

            由于系统设置了PYTHONPATH,指向anaconda目录,所以使用Pip命令安装包的时候,默认装载anaconda下。
    conda除了管理anaconda这个环境之外,还管理其他的环境。所以说conda list下的包的数目比pip list下的数目要多。conda不管可以管理python的库,也可以管理非python的库,比如c/c++;而pip仅是官方推荐的python库管理命令。

  2. 最重要的是,conda list显示装了CPU和GPU版本。那用的时候到底是用CPU的,还是GPU的?

测试结果如下:

import tensorflow as tf
a = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[2, 3], name='a')
b = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[3, 2], name='b')
c = tf.matmul(a, b)
sess = tf.Session(config=tf.ConfigProto(log_device_placement=True))
print(sess.run(c))

输出如下:

WARNING:tensorflow:From E:/pycharmProject/testpy.py:6: The name tf.Session is deprecated. Please use tf.compat.v1.Session instead.

WARNING:tensorflow:From E:/pycharmProject/testpy.py:6: The name tf.ConfigProto is deprecated. Please use tf.compat.v1.ConfigProto instead.

2019-11-25 23:45:23.874021: I tensorflow/core/platform/cpu_feature_guard.cc:142] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX AVX2
2019-11-25 23:45:23.876005: I tensorflow/stream_executor/platform/default/dso_loader.cc:42] Successfully opened dynamic library nvcuda.dll
2019-11-25 23:45:23.903390: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1640] Found device 0 with properties: 
name: GeForce RTX 2070 SUPER major: 7 minor: 5 memoryClockRate(GHz): 1.77
pciBusID: 0000:08:00.0
2019-11-25 23:45:23.903510: I tensorflow/stream_executor/platform/default/dlopen_checker_stub.cc:25] GPU libraries are statically linked, skip dlopen check.
2019-11-25 23:45:23.903933: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1763] Adding visible gpu devices: 0
2019-11-25 23:45:24.423957: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1181] Device interconnect StreamExecutor with strength 1 edge matrix:
2019-11-25 23:45:24.424051: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1187]      0 
2019-11-25 23:45:24.424097: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1200] 0:   N 
2019-11-25 23:45:24.425108: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1326] Created TensorFlow device (/job:localhost/replica:0/task:0/device:GPU:0 with 6292 MB memory) -> physical GPU (device: 0, name: GeForce RTX 2070 SUPER, pci bus id: 0000:08:00.0, compute capability: 7.5)
Device mapping:
/job:localhost/replica:0/task:0/device:GPU:0 -> device: 0, name: GeForce RTX 2070 SUPER, pci bus id: 0000:08:00.0, compute capability: 7.5
MatMul: (MatMul): /job:localhost/replica:0/task:0/device:GPU:0
a: (Const): /job:localhost/replica:0/task:0/device:GPU:0
b: (Const): /job:localhost/replica:0/task:0/device:GPU:0
2019-11-25 23:45:24.428183: I tensorflow/core/common_runtime/direct_session.cc:296] Device mapping:
/job:localhost/replica:0/task:0/device:GPU:0 -> device: 0, name: GeForce RTX 2070 SUPER, pci bus id: 0000:08:00.0, compute capability: 7.5

2019-11-25 23:45:24.429366: I tensorflow/core/common_runtime/placer.cc:54] MatMul: (MatMul)/job:localhost/replica:0/task:0/device:GPU:0
2019-11-25 23:45:24.429471: I tensorflow/core/common_runtime/placer.cc:54] a: (Const)/job:localhost/replica:0/task:0/device:GPU:0
2019-11-25 23:45:24.429570: I tensorflow/core/common_runtime/placer.cc:54] b: (Const)/job:localhost/replica:0/task:0/device:GPU:0
[[22. 28.]
 [49. 64.]]

Process finished with exit code 0

        从上面的信息来看,可以得出是在用GPU。不过从网上了解到,使用conda安装tensorflow-gpu时,它会帮我们下载依赖项,比如最重要的cuda和cudnn。自己安装cuda和cudnn明显比它自己下载的大,不知道这会不会是隐患。先用一段时间看看,有问题后续再补。还需要补充一点,conda下载的依赖项不一定是匹配的,有可能会下载高版本,所以在使用conda安装tensorflow时,搜索与错误相关的东西,看看是不是版本问题(我出现了numpy版本过高的问题)。

其他tensorflow测试代码:

# -*- coding: utf-8 -*-#
# Author:      weiz
# Date:        2020/3/4 上午9:41
# Name:        test-tf.py
# Description:
import tensorflow as tf
import numpy as np
import time

value = np.random.randn(5000, 1000)
a = tf.constant(value)

b = a * a

c = 0
tic = time.time()
with tf.Session() as sess:
    for i in range(1000):
        sess.run(b)

        c += 1
        if c % 100 == 0:
            d = c / 10
            print("计算进度:%s%%" % d)

toc = time.time()
t_cost = toc - tic

print("当前设备测试所用时间:%s" % t_cost)
# GPU
print("Ubuntu上GPU为1050Ti测试时间为:6.079401016235352")
print("CenOs上GPU为1080Ti测试时间为:4.752010345458984")
print("Ubuntu上GPU为2070s测试时间为:3.87003231048584")  # CPU为R7-3700x运行时间为8.4秒
print("Ubuntu上GPU为2060s测试时间为:4.5342936515808105")
# CPU
print("Ubuntu上CPU(i7-8700k)测试时间为:11.881832599639893")
print("CenOs上CPU(i9-9900k)测试时间为:7.578999996185303")

 

  • 2
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 13
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 13
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值