2017-01-23 113 views
2

由于很多机器学习算法依赖于矩阵乘法(或至少可以使用矩阵乘法来实现)来测试我的GPU是我计划建立矩阵A,B,它们相乘并记录花费的时间为了计算完成。测试GPU与tensorflow矩阵乘法

这里是代码,会生成尺寸300000,20000的两个矩阵和繁殖他们:

import tensorflow as tf 
import numpy as np 

init = tf.global_variables_initializer() 
sess = tf.Session() 
sess.run(init) 


#a = np.array([[1, 2, 3], [4, 5, 6]]) 
#b = np.array([1, 2, 3]) 

a = np.random.rand(300000,20000) 
b = np.random.rand(300000,20000) 

println("Init complete"); 

result = tf.mul(a , b) 
v = sess.run(result) 

print(v) 

这是一个足够的测试来比较GPU的性能?我应该考虑哪些其他因素?

回答

7

这是matmul基准的example,它避免了常见的缺陷,并且与Titan X Pascal上的官方11 TFLOP标记相匹配。

import os 
import sys 
os.environ["CUDA_VISIBLE_DEVICES"]="1" 
import tensorflow as tf 
import time 

n = 8192 
dtype = tf.float32 
with tf.device("/gpu:0"): 
    matrix1 = tf.Variable(tf.ones((n, n), dtype=dtype)) 
    matrix2 = tf.Variable(tf.ones((n, n), dtype=dtype)) 
    product = tf.matmul(matrix1, matrix2) 


# avoid optimizing away redundant nodes 
config = tf.ConfigProto(graph_options=tf.GraphOptions(optimizer_options=tf.OptimizerOptions(opt_level=tf.OptimizerOptions.L0))) 
sess = tf.Session(config=config) 

sess.run(tf.global_variables_initializer()) 
iters = 10 

# pre-warming 
sess.run(product.op) 

start = time.time() 
for i in range(iters): 
    sess.run(product.op) 
end = time.time() 
ops = n**3 + (n-1)*n**2 # n^2*(n-1) additions, n^3 multiplications 
elapsed = (end - start) 
rate = iters*ops/elapsed/10**9 
print('\n %d x %d matmul took: %.2f sec, %.2f G ops/sec' % (n, n, 
                  elapsed/iters, 
                  rate,)) 
+0

很酷,我认为应该将您的代码发布到您的答案中,除了引用代码外。未发现除非'os.environ [ “CUDA_VISIBLE_DEVICES”] = –

+0

GPU “1”'被注释。与Windows 10的作品,tensorflow-GPU(1.4),cuda_8.0.61_win10和cudnn-8.0-windows10-64-V6.0。 – BSalita

+0

错误是'不能分配操作“Variable_1”的设备:操作被明确指定为/设备:GPU:0,但可用的设备[/职业:本地主机/副本:0 /任务:0 /设备:CPU:0]。确保设备规格指的是有效device.' – BSalita