[英]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.