Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 为什么';scikit cuda减去像numpy一样的广播?_Python_Numpy_Gpu - Fatal编程技术网

Python 为什么';scikit cuda减去像numpy一样的广播?

Python 为什么';scikit cuda减去像numpy一样的广播?,python,numpy,gpu,Python,Numpy,Gpu,skcuda.misc.subtract没有像我预期的那样广播。使用此代码: import numpy as np import pycuda.gpuarray as gpuarray import skcuda.misc as gpumisc import pycuda.autoinit a = np.ones((3, 1)) b = np.ones((1, 3)) c = a - b assert np.allclose(c, np.zeros((3, 3))) a_gpu = gpua

skcuda.misc.subtract没有像我预期的那样广播。使用此代码:

import numpy as np
import pycuda.gpuarray as gpuarray
import skcuda.misc as gpumisc
import pycuda.autoinit

a = np.ones((3, 1))
b = np.ones((1, 3))
c = a - b
assert np.allclose(c, np.zeros((3, 3)))

a_gpu = gpuarray.to_gpu(a)
b_gpu = gpuarray.to_gpu(b)
c_gpu = gpumisc.subtract(a_gpu, b_gpu)
assert np.allclose(c_gpu.get(), np.zeros((3, 3)))
我得到这个错误:

TypeError                                 Traceback (most recent call last)
<ipython-input-1-63f3109ba0df> in <module>()
     11 a_gpu = gpuarray.to_gpu(a)
     12 b_gpu = gpuarray.to_gpu(b)
---> 13 c_gpu = gpumisc.subtract(a_gpu, b_gpu)
     14 assert np.allclose(c_gpu.get(), np.zeros((3, 3)))

/appl/pm/vendor/dev/python/lx-x86_64/anaconda2/lib/python2.7/site-packages/skcuda/misc.pyc in subtract(x_gpu, y_gpu)
   1032     """
   1033 
-> 1034     return binaryop_2d("-", operator.sub, False, x_gpu, y_gpu)
   1035 
   1036 def multiply(x_gpu, y_gpu):

/appl/pm/vendor/dev/python/lx-x86_64/anaconda2/lib/python2.7/site-packages/skcuda/misc.pyc in binaryop_2d(c_op, py_op, commutative, x_gpu, y_gpu)
    982             return binaryop_matvec(c_op, y_gpu, x_gpu.ravel(), axis=0)
    983 
--> 984     raise TypeError("unsupported combination of shapes")
    985 
    986 def add(x_gpu, y_gpu):

TypeError: unsupported combination of shapes
TypeError回溯(最近一次调用)
在()
11 a_gpu=gpuarray.to_gpu(a)
12 b_gpu=gpuarray.to_gpu(b)
--->13 c_gpu=gpumisc.减法(a_gpu,b_gpu)
14 assert np.allclose(c_gpu.get(),np.zeros((3,3)))
/appl/pm/vendor/dev/python/lx-x86_64/anaconda2/lib/python2.7/site-packages/skcuda/misc.pyc in subtract(x_gpu,y_gpu)
1032     """
1033
->1034返回二进制OP_2d(“-”,operator.sub,False,x_gpu,y_gpu)
1035
1036 def倍增(x_gpu,y_gpu):
/binaryop_2d中的appl/pm/vendor/dev/python/lx-x86_64/anaconda2/lib/python2.7/site-packages/skcuda/misc.pyc(c_-op,py_-op,可交换,x_-gpu,y_-gpu)
982返回二进制操作(c_op,y_gpu,x_gpu.ravel(),axis=0)
983
-->984 raise TYPE错误(“不支持的形状组合”)
985
986 def添加(x_gpu,y_gpu):
TypeError:不支持的形状组合
有人知道我做错了什么吗


(我似乎不能不写更多的字就发帖。明天、明天和明天以这种微小的步调从一天一天爬到记录时间的最后一个音节…

cuda的记忆模式(以及一般的GPU)使任意广播操作变得非常不吸引人。只有连续的读/写操作是有效的;因此线程应该沿着连续的步幅对齐。这使得编写一个在许多情况下不会产生严重次优性能的通用广播函数非常困难。

的内存模型cuda(一般包括GPU)使任意广播操作变得非常不吸引人。只有连续的读/写操作是有效的;因此线程应该沿着连续的步幅对齐。这使得编写一般广播函数非常困难,因为在许多情况下,该函数不会产生严重的次优性能。

如果不绑定的话对于scikit cuda,我建议使用TensorFlow作为cuda后端。它支持许多运营商的本地广播。例如:

import numpy as np
import tensorflow as tf

a = np.ones((3, 1)) 
b = np.ones((1, 3))
c = a - b
assert np.allclose(c, np.zeros((3, 3)))

with tf.device('/cpu:0'):
    a_gpu = tf.constant(a)
    b_gpu = tf.constant(b)
    c_gpu = tf.sub(a_gpu, b_gpu)

sess = tf.Session(config=tf.ConfigProto(log_device_placement=False))
c_gpu_out = sess.run(c_gpu)

np.testing.assert_allclose(c_gpu_out, np.zeros((3, 3)))

如果您没有绑定到scikit cuda,我建议使用TensorFlow作为cuda后端。它支持在许多运营商上进行本机广播。例如:

import numpy as np
import tensorflow as tf

a = np.ones((3, 1)) 
b = np.ones((1, 3))
c = a - b
assert np.allclose(c, np.zeros((3, 3)))

with tf.device('/cpu:0'):
    a_gpu = tf.constant(a)
    b_gpu = tf.constant(b)
    c_gpu = tf.sub(a_gpu, b_gpu)

sess = tf.Session(config=tf.ConfigProto(log_device_placement=False))
c_gpu_out = sess.run(c_gpu)

np.testing.assert_allclose(c_gpu_out, np.zeros((3, 3)))

谢谢。我如何沿着连续的步幅对齐以使我的简单示例工作?谢谢。我如何沿着连续的步幅对齐以使我的简单示例工作?