【发布时间】:2016-04-07 20:03:08
【问题描述】:
编辑:
很抱歉,事实证明,我在进行测试时在我的 GPU 上运行了其他进程,我更新了空闲 GPU 上的计时结果,并且对于更大的矩阵,加速变得明显。
原帖:
正如this question 中发布的那样,L 是一个矩阵列表,其中每个项目M 是一个x*n 矩阵(x 是一个变量,n 是固定的)。
我想计算L 中所有项目的M'*M 总和(M' 是M 的转置),就像下面的 Python 代码一样。
for M in L:
res += np.dot(M.T, M)
以下是 Numpy 和 Theano 实现的一些示例(有关可执行脚本,请参阅@DanielRenshaw 对previous question 的回答)。
def numpy_version1(*L):
n = L[0].shape[1]
res = np.zeros((n, n), dtype=L[0].dtype)
for M in L:
res += np.dot(M.T, M)
return res
def compile_theano_version1(number_of_matrices, n, dtype):
L = [tt.matrix() for _ in xrange(number_of_matrices)]
res = tt.zeros(n, dtype=dtype)
for M in L:
res += tt.dot(M.T, M)
return theano.function(L, res)
def compile_theano_version2(number_of_matrices, n):
L = theano.typed_list.TypedListType(tt.TensorType(theano.config.floatX, broadcastable=(None, None)))()
res, _ = theano.reduce(fn=lambda i, tmp: tmp+tt.dot(L[i].T, L[i]),
outputs_info=tt.zeros((n, n), dtype=theano.config.floatX),
sequences=[theano.tensor.arange(number_of_matrices, dtype='int64')])
return theano.function([L], res)
我在 CPU 上运行 Numpy 版本,在 GPU 上运行不同设置的 Theano 版本,似乎 Theano 版本总是比 Numpy 版本慢(不管 matices 的数量和大小)。
但我期待可能会针对 GPU 进行一些优化,因为它是一个简单的 reduce 操作。
有人可以帮助我了解幕后发生的事情吗?
编辑:
以下是生成数据的脚本(来自@DanielRenshaw)、我已经厌倦的设置和结果。
L = [np.random.standard_normal(size=(x, n)).astype(dtype)
for x in range(min_x, number_of_matrices + min_x)]
dtype = 'float32'
theano.config.floatX = dtype
iteration_count = 10
min_x = 20
# base case:
# numpy_version1 0.100589990616
# theano_version1 0.243968963623
# theano_version2 0.198153018951
number_of_matrices = 200
n = 100
# increase matrix size:
# numpy_version1 4.90120816231
# theano_version1 0.984472036362
# theano_version2 3.56008815765
number_of_matrices = 200
n = 1000
# increase number of matrices:
# numpy_version1 5.11445093155
# theano_version1 compilation error
# theano_version2 6.54448604584
number_of_matrices = 2000
n = 100
【问题讨论】:
-
你有哪个 GPU?这可能是答案的关键。如果你有最新的 i7 和最糟糕的 GPU,不管阵列有多大,CPU 版本可能会更快,特别是如果你有使用 MKL、OpenBLAS 等构建的 numpy。点操作。
-
@imaluengo 正在使用 i7、nvidia titan x,并且 numpy 是使用 OpenBLAS 构建的
-
Ops,那不应该是这样的:P 你能添加你在测试中尝试过的
L的值和M的大小,以及你得到的结果(至少一些其中)? -
@imaluengo 谢谢,我会尽快添加它们。
-
只是为了复制准确的结果。更容易测试出了什么问题。
标签: python numpy parallel-processing gpgpu theano