我已经在我的ubuntu 16.04中安装了tensorflow,使用的是ubuntu内置的apt cuda安装。
现在我的问题是,我如何测试tensorflow是否真的使用gpu?我有一个gtx 960m gpu。当我导入tensorflow时,这是输出
I tensorflow/stream_executor/dso_loader.cc:105] successfully opened CUDA library libcublas.so locally
I tensorflow/stream_executor/dso_loader.cc:105] successfully opened CUDA library libcudnn.so locally
I tensorflow/stream_executor/dso_loader.cc:105] successfully opened CUDA library libcufft.so locally
I tensorflow/stream_executor/dso_loader.cc:105] successfully opened CUDA library libcuda.so.1 locally
I tensorflow/stream_executor/dso_loader.cc:105] successfully opened CUDA library libcurand.so locally
这个输出是否足够检查tensorflow是否使用gpu ?
下面的代码段应该给出tensorflow可用的所有设备。
from tensorflow.python.client import device_lib
print(device_lib.list_local_devices())
样例输出
(名字:“/ cpu: 0”
device_type:“CPU”
memory_limit: 268435456
位置{
}
化身:4402277519343584096,
名称:“/ gpu: 0”
device_type:“GPU”
memory_limit: 6772842168
位置{
bus_id: 1
}
化身:7471795903849088328
physical_device_desc: "设备:0,名称:GeForce GTX 1070, pci总线id: 0000:05:00.0"
]
Tensorflow 2.1
一个简单的计算,可以用nvidia-smi验证GPU上的内存使用情况。
import tensorflow as tf
c1 = []
n = 10
def matpow(M, n):
if n < 1: #Abstract cases where n < 1
return M
else:
return tf.matmul(M, matpow(M, n-1))
with tf.device('/gpu:0'):
a = tf.Variable(tf.random.uniform(shape=(10000, 10000)), name="a")
b = tf.Variable(tf.random.uniform(shape=(10000, 10000)), name="b")
c1.append(matpow(a, n))
c1.append(matpow(b, n))