繁体   English   中英

Theano:使用CPU与GPU的矩阵点时的差异与Numpy的比较

[英]Theano: Discrepancy vs Numpy when taking matrix dot with CPU vs GPU

我最近让Theano使用CUDA v7.5,CUDNN v3和Visual Studio 2013 Community Edition在Windows 10上工作。 为了验证它是否正常工作,我使用CPU和GPU测试了Theano Windows安装页面中的以下代码:

import numpy as np
import time
import theano
A = np.random.rand(10000,10000).astype(theano.config.floatX)
B = np.random.rand(10000,10000).astype(theano.config.floatX)
np_start = time.time()
AB = A.dot(B)
np_end = time.time()
X,Y = theano.tensor.matrices('XY')
mf = theano.function([X,Y],X.dot(Y))
t_start = time.time()
tAB = mf(A,B)
t_end = time.time()
print "NP time: %f[s], theano time: %f[s] (times should be close when run on CPU!)" %(
                                           np_end-np_start, t_end-t_start)
print "Result difference: %f" % (np.abs(AB-tAB).max(), )

我得到了以下结果:

G:\ml\Theano\Projects>python Test.py
NP time: 10.585000[s], theano time: 10.587000[s] (times should be close when run on CPU!)
Result difference: 0.000000

G:\ml\Theano\Projects>python Test.py
Using gpu device 0: GeForce GTX 970 (CNMeM is disabled)
NP time: 10.838000[s], theano time: 1.294000[s] (times should be close when run on CPU!)
Result difference: 0.022461

如您所见,在GPU上进行计算时,存在相当显着的差异0.022。 只是想知道这是否是预期的,或者我做错了什么。

这是我的.theanorc:

[global]
device = gpu
floatX = float32

[nvcc]
fastmath = True

GPU不以相同的顺序进行加法和乘法。 由于浮标不准确,看到一些差异是正常的。

如果相对差异很小,则该大小的绝对差异可以是正常的。

要更“正确”地比较它们,请使用theano.tensor.basic._allclose(result1, result2)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM