在 theano 中添加 broadcastble 矩阵
adding broadcastble matrix in theano
我有两个矩阵,它们的形状分别为 (1,3) 和 (3,1)
我想添加它们并输出一个矩阵 (3,3)
在 numpy 中,它是这样工作的:
import numpy as np
a = np.array([0,1,2])
b = a.reshape(3,1)
a+b
它输出:
array([0,1,2],
[1,2,3],
[2,3,4]]
现在我想用theano做同样的事情来加速代码。
我的代码如下:
label_vec1 = T.imatrix('label_vector')
label_vec2 = T.imatrix('label_vector')
alpha_matrix = T.add(label_vec1, label_vec2)
alpha_matrix_compute = theano.function([label_vec1,label_vec2],alpha_matrix)
a = numpy.array([[0,1,2]])
b = numpy.array([[0],[1],[2]])#
a1=theano.shared(numpy.asarray(a), broadcastable =(True,False))
b1 = theano.shared(numpy.asarray(b),broadcastable=(False, True))
c = alpha_matrix_compute(a1,b1)
但它输出
TypeError: ('Bad input argument to theano function at index 0(0-based)', 'Expected an array-like object, but found a Variable: maybe you are trying to call a function on a (possibly shared) variable instead of a numeric array?')
我很困惑,为什么会这样?
顺便说一句,使用带有 GPU 的 theano 会比使用 numpy 更快吗?
经过几个小时的搜索和阅读,我找到了答案 here。
当定义一个numpy数组到共享变量时,它变成了符号变量,不再是数值数组。
使用共享变量计算,代码修改如下:
a = numpy.array([[0,1,2]])
b = numpy.array([[0],[1],[2]])#
#b = a.reshape(a.shape[1],a.shape[0])
a1=theano.shared(numpy.asarray(a), broadcastable =(True,False), borrow =True)
b1 = theano.shared(numpy.asarray(b),broadcastable=(False, True),borrow = True)
alpha_matrix = T.add(a1, b1)
alpha_matrix_compute = theano.function([], alpha_matrix)
s_t_1 = timeit.default_timer()
for i in range(10000):
c = alpha_matrix_compute()
e_t_1 = timeit.default_timer()
for i in range(10000):
c = numpy.add(a,b)
e_t_2 = timeit.default_timer()
print('t1:',e_t_1-s_t_1)
print('t2:',e_t_2-e_t_1)
另外,我比较了使用theano和numpy进行broadcastable matrix add的耗时。结果是
t1: 0.25077449798077067
t2: 0.022930744192201424
好像numpy更快。事实上,GPU 和 CPU 之间的数据传输花费了很多时间。这就是为什么 t2 比 t1 小得多的原因。
我有两个矩阵,它们的形状分别为 (1,3) 和 (3,1) 我想添加它们并输出一个矩阵 (3,3) 在 numpy 中,它是这样工作的:
import numpy as np
a = np.array([0,1,2])
b = a.reshape(3,1)
a+b
它输出:
array([0,1,2],
[1,2,3],
[2,3,4]]
现在我想用theano做同样的事情来加速代码。 我的代码如下:
label_vec1 = T.imatrix('label_vector')
label_vec2 = T.imatrix('label_vector')
alpha_matrix = T.add(label_vec1, label_vec2)
alpha_matrix_compute = theano.function([label_vec1,label_vec2],alpha_matrix)
a = numpy.array([[0,1,2]])
b = numpy.array([[0],[1],[2]])#
a1=theano.shared(numpy.asarray(a), broadcastable =(True,False))
b1 = theano.shared(numpy.asarray(b),broadcastable=(False, True))
c = alpha_matrix_compute(a1,b1)
但它输出
TypeError: ('Bad input argument to theano function at index 0(0-based)', 'Expected an array-like object, but found a Variable: maybe you are trying to call a function on a (possibly shared) variable instead of a numeric array?')
我很困惑,为什么会这样? 顺便说一句,使用带有 GPU 的 theano 会比使用 numpy 更快吗?
经过几个小时的搜索和阅读,我找到了答案 here。
当定义一个numpy数组到共享变量时,它变成了符号变量,不再是数值数组。
使用共享变量计算,代码修改如下:
a = numpy.array([[0,1,2]])
b = numpy.array([[0],[1],[2]])#
#b = a.reshape(a.shape[1],a.shape[0])
a1=theano.shared(numpy.asarray(a), broadcastable =(True,False), borrow =True)
b1 = theano.shared(numpy.asarray(b),broadcastable=(False, True),borrow = True)
alpha_matrix = T.add(a1, b1)
alpha_matrix_compute = theano.function([], alpha_matrix)
s_t_1 = timeit.default_timer()
for i in range(10000):
c = alpha_matrix_compute()
e_t_1 = timeit.default_timer()
for i in range(10000):
c = numpy.add(a,b)
e_t_2 = timeit.default_timer()
print('t1:',e_t_1-s_t_1)
print('t2:',e_t_2-e_t_1)
另外,我比较了使用theano和numpy进行broadcastable matrix add的耗时。结果是
t1: 0.25077449798077067
t2: 0.022930744192201424
好像numpy更快。事实上,GPU 和 CPU 之间的数据传输花费了很多时间。这就是为什么 t2 比 t1 小得多的原因。