【发布时间】:2020-07-14 13:59:59
【问题描述】:
我安装了 TensorFlow-GPU 2.1.0 但当被问及版本时,答案是 1.13.1
import tensorflow as tf
print(tf.__version__)
最大的问题是当我运行 GPU 测试时,答案是 False
tf.test.is_gpu_available()
我试过了
print("Num GPUs Available:",len(tf.config.experimental.list_physical_devices('GPU')))
答案是 AttributeError: module 'tensorflow' has no attribute 'config' 但他我运行这个脚本
from numba import vectorize, jit, cuda
# to measure exec time
from timeit import default_timer as timer
# normal function to run on cpu
def func(a):
for i in range(10000000):
a[i]+= 1
# function optimized to run on gpu
@vectorize(['float64(float64)'], target ="cuda")
def func2(x):
return x+1
# kernel to run on gpu
@cuda.jit
def func3(a, N):
tid = cuda.grid(1)
if tid < N:
a[tid] += 1
if __name__=="__main__":
n = 10000000
a = np.ones(n, dtype = np.float64)
for i in range(0,5):
start = timer()
func(a)
print(i, " without GPU:", timer()-start)
for i in range(0,5):
start = timer()
func2(a)
print(i, " with GPU ufunc:", timer()-start)
threadsperblock = 1024
blockspergrid = (a.size + (threadsperblock - 1)) // threadsperblock
for i in range(0,5):
start = timer()
func3[blockspergrid, threadsperblock](a, n)
print(i, " with GPU kernel:", timer()-start)
答案是 0 没有 GPU:5.481482100000051 1 没有 GPU:5.6241342000000145 2 不带 GPU:5.62558580000001 3 不带 GPU:5.299320899999998 4 不带 GPU:5.424306600000023 0 带 GPU ufunc:0.4764495000000011 1 带 GPU ufunc:0.118225099999961 2带GPU ufunc:0.12550920000001042 3 带 GPU ufunc:0.11633530000000292 4 带 GPU ufunc:0.11252430000001823 0 带 GPU 内核:0.20753619999999273 1 带 GPU 内核:0.08865670000000136 2 带 GPU 内核:0.08246159999998781 3 带 GPU 内核:0.08481519999998 4 带 GPU 内核:0.08220890000001191 如何使 GPU 对 TensorFlow 可见?
【问题讨论】:
标签: python tensorflow gpu