本文整理汇总了Python中theano.sandbox.cuda.CudaNdarray方法的典型用法代码示例。如果您正苦于以下问题:Python cuda.CudaNdarray方法的具体用法?Python cuda.CudaNdarray怎么用?Python cuda.CudaNdarray使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类theano.sandbox.cuda
的用法示例。
在下文中一共展示了cuda.CudaNdarray方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_output_broadcast_cuda
# 需要导入模块: from theano.sandbox import cuda [as 别名]
# 或者: from theano.sandbox.cuda import CudaNdarray [as 别名]
def test_output_broadcast_cuda(self):
from theano.sandbox import cuda
if not cuda.cuda_available:
raise SkipTest("Optional package Cuda disabled")
if cuda.use.device_number is None:
# We should normally set VecAsRowAndCol as a GPUOp But we
# don't want to do this here as this will disable others
# tests in this file. So we manually init the GPU if
# needed to remove warning.
cuda.use("gpu",
force=True,
default_to_move_computation_to_gpu=False,
move_shared_float32_to_gpu=False,
enable_cuda=False)
v = cuda.fvector('v')
c, r = VecAsRowAndCol()(v)
f = theano.function([v], [c, r])
v_val = cuda.CudaNdarray(self.rng.randn(5).astype('float32'))
f(v_val)
示例2: garray_to_cudandarray
# 需要导入模块: from theano.sandbox import cuda [as 别名]
# 或者: from theano.sandbox.cuda import CudaNdarray [as 别名]
def garray_to_cudandarray(x):
""" take a gnumpy.garray and make a CudaNdarray that point to its memory
"""
if not isinstance(x, gnumpy.garray):
raise ValueError("We can transfer only gnumpy.garray to CudaNdarray")
# elif x.dtype != "float32":
# raise ValueError("CudaNdarray support only float32")
# We don't need this, because cudamat is always float32.
else:
strides = [1]
for i in x.shape[::-1][:-1]:
strides.append(strides[-1] * i)
strides = strides[::-1]
for i in range(len(strides)):
if x.shape[i] == 1:
strides[i] = 0
strides = tuple(strides)
import ctypes
ptr_long = long(ctypes.cast(x._base.mat.data_device, ctypes.c_void_p).value)
# seems legit.
z = cuda.from_gpu_pointer(ptr_long, x.shape, strides, x._base)
return z
示例3: test_to_cudandarray
# 需要导入模块: from theano.sandbox import cuda [as 别名]
# 或者: from theano.sandbox.cuda import CudaNdarray [as 别名]
def test_to_cudandarray():
px = pycuda.gpuarray.zeros((3, 4, 5), 'float32')
cx = to_cudandarray(px)
assert isinstance(cx, cuda.CudaNdarray)
assert numpy.allclose(px.get(),
numpy.asarray(cx))
assert px.dtype == cx.dtype
assert px.shape == cx.shape
assert all(numpy.asarray(cx._strides) * 4 == px.strides)
try:
px = pycuda.gpuarray.zeros((3, 4, 5), 'float64')
to_cudandarray(px)
assert False
except ValueError:
pass
try:
to_cudandarray(numpy.zeros(4))
assert False
except ValueError:
pass
示例4: test_pycuda_theano
# 需要导入模块: from theano.sandbox import cuda [as 别名]
# 或者: from theano.sandbox.cuda import CudaNdarray [as 别名]
def test_pycuda_theano():
"""Simple example with pycuda function and Theano CudaNdarray object."""
from pycuda.compiler import SourceModule
mod = SourceModule("""
__global__ void multiply_them(float *dest, float *a, float *b)
{
const int i = threadIdx.x;
dest[i] = a[i] * b[i];
}
""")
multiply_them = mod.get_function("multiply_them")
a = numpy.random.randn(100).astype(numpy.float32)
b = numpy.random.randn(100).astype(numpy.float32)
# Test with Theano object
ga = cuda_ndarray.CudaNdarray(a)
gb = cuda_ndarray.CudaNdarray(b)
dest = cuda_ndarray.CudaNdarray.zeros(a.shape)
multiply_them(dest, ga, gb,
block=(400, 1, 1), grid=(1, 1))
assert (numpy.asarray(dest) == a * b).all()
示例5: to_cudandarray
# 需要导入模块: from theano.sandbox import cuda [as 别名]
# 或者: from theano.sandbox.cuda import CudaNdarray [as 别名]
def to_cudandarray(x):
""" take a pycuda.gpuarray.GPUArray and make a CudaNdarray that point to its memory
:note: CudaNdarray support only float32, so only float32 GPUArray are accepted
"""
if not isinstance(x, pycuda.gpuarray.GPUArray):
raise ValueError("We can transfer only pycuda.gpuarray.GPUArray to CudaNdarray")
elif x.dtype != "float32":
raise ValueError("CudaNdarray support only float32")
else:
strides = [1]
for i in x.shape[::-1][:-1]:
strides.append(strides[-1] * i)
strides = tuple(strides[::-1])
ptr = int(x.gpudata) # in pycuda trunk, y.ptr also works, which is a little cleaner
z = cuda.from_gpu_pointer(ptr, x.shape, strides, x)
return z
示例6: test_host_to_device
# 需要导入模块: from theano.sandbox import cuda [as 别名]
# 或者: from theano.sandbox.cuda import CudaNdarray [as 别名]
def test_host_to_device():
#print >>sys.stdout, 'starting test_host_to_dev'
for shape in ((), (3,), (2, 3), (3, 4, 5, 6)):
a = theano._asarray(numpy.random.rand(*shape), dtype='float32')
b = cuda_ndarray.CudaNdarray(a)
c = numpy.asarray(b)
assert numpy.all(a == c)
# test with float32 dtype
d = numpy.asarray(b, dtype='float32')
assert numpy.all(a == d)
# test with not float32 dtype
try:
numpy.asarray(b, dtype='int8')
assert False
except TypeError:
pass
示例7: test_exp
# 需要导入模块: from theano.sandbox import cuda [as 别名]
# 或者: from theano.sandbox.cuda import CudaNdarray [as 别名]
def test_exp():
#print >>sys.stdout, 'starting test_exp'
for shape in ((), (3,), (2, 3),
(1, 10000000), (10, 1000000),
(100, 100000), (1000, 10000), (10000, 1000)):
a0 = theano._asarray(numpy.random.rand(*shape), dtype='float32')
a1 = a0.copy()
b0 = cuda_ndarray.CudaNdarray(a0)
b1 = cuda_ndarray.CudaNdarray(a1)
t0 = time.time()
bsum = b0.exp()
t1 = time.time()
gpu_dt = t1 - t0
t0 = time.time()
asum = numpy.exp(a1)
t1 = time.time()
cpu_dt = t1 - t0
# print shape, 'adding ', a0.size, 'cpu', cpu_dt, 'advantage', advantage(cpu_dt, gpu_dt)
#c = numpy.asarray(b0+b1)
if asum.shape:
assert numpy.allclose(asum, numpy.asarray(bsum))
示例8: test_copy
# 需要导入模块: from theano.sandbox import cuda [as 别名]
# 或者: from theano.sandbox.cuda import CudaNdarray [as 别名]
def test_copy():
#print >>sys.stdout, 'starting test_copy'
shape = (500, 499)
a = theano._asarray(numpy.random.rand(*shape), dtype='float32')
#print >>sys.stdout, '.. creating device object'
b = cuda_ndarray.CudaNdarray(a)
#print >>sys.stdout, '.. copy'
c = copy.copy(b)
#print >>sys.stdout, '.. deepcopy'
d = copy.deepcopy(b)
#print >>sys.stdout, '.. comparisons'
assert numpy.allclose(a, numpy.asarray(b))
assert numpy.allclose(a, numpy.asarray(c))
assert numpy.allclose(a, numpy.asarray(d))
b += b
assert numpy.allclose(a+a, numpy.asarray(b))
assert numpy.allclose(a+a, numpy.asarray(c))
assert numpy.allclose(a, numpy.asarray(d))
示例9: test_getshape
# 需要导入模块: from theano.sandbox import cuda [as 别名]
# 或者: from theano.sandbox.cuda import CudaNdarray [as 别名]
def test_getshape():
shapelist = [
((1, 2, 3), (1, 2, 3)),
((1,), (1,)),
((1, 2, 3), (3, 2, 1)),
((1, 2, 3), (6,)),
((1, 2, 3, 2), (6, 2)),
((2, 3, 2), (6, 2))
]
def subtest(shape):
a = theano._asarray(numpy.random.rand(*shape_1), dtype='float32')
b = cuda_ndarray.CudaNdarray(a)
assert b.shape == a.shape
for shape_1, shape_2 in shapelist:
subtest(shape_1)
subtest(shape_2)
示例10: test_stride_manipulation
# 需要导入模块: from theano.sandbox import cuda [as 别名]
# 或者: from theano.sandbox.cuda import CudaNdarray [as 别名]
def test_stride_manipulation():
a = theano._asarray([[0, 1, 2], [3, 4, 5]], dtype='float32')
b = cuda_ndarray.CudaNdarray(a)
v = b.view()
v._dev_data += 0
c = numpy.asarray(v)
assert numpy.all(a == c)
sizeof_float = 4
offset = 0
b_strides = b._strides
for i in xrange(len(b.shape)):
offset += (b.shape[i]-1) * b_strides[i]
v._set_stride(i, -b_strides[i])
v._dev_data += offset * sizeof_float
c = numpy.asarray(v)
assert numpy.all(c == [[5, 4, 3], [2, 1, 0]])
示例11: test_setitem_matrixscalar0
# 需要导入模块: from theano.sandbox import cuda [as 别名]
# 或者: from theano.sandbox.cuda import CudaNdarray [as 别名]
def test_setitem_matrixscalar0():
a = theano._asarray([[0, 1, 2], [3, 4, 5]], dtype='float32')
_a = cuda_ndarray.CudaNdarray(a)
b = theano._asarray(8, dtype='float32')
_b = cuda_ndarray.CudaNdarray(b)
# set an element to 8
_a[1, 1] = _b
a[1, 1] = b
assert numpy.allclose(a, numpy.asarray(_a))
# test direct transfert from numpy
_a[1, 1] = theano._asarray(888, dtype='float32')
a[1, 1] = theano._asarray(888, dtype='float32')
assert numpy.allclose(a, numpy.asarray(_a))
# broadcast a 0
_a[1, 1] = 0
_a[0:2] = 0
_a[1:] = 0
示例12: test_setitem_matrixvector1
# 需要导入模块: from theano.sandbox import cuda [as 别名]
# 或者: from theano.sandbox.cuda import CudaNdarray [as 别名]
def test_setitem_matrixvector1():
a = theano._asarray([[0, 1, 2], [3, 4, 5]], dtype='float32')
_a = cuda_ndarray.CudaNdarray(a)
b = theano._asarray([8, 9], dtype='float32')
_b = cuda_ndarray.CudaNdarray(b)
# set second column to 8,9
_a[:, 1] = _b
a[:, 1] = b
assert numpy.allclose(a, numpy.asarray(_a))
# test direct transfert from numpy
_a[:, 1] = b*100
a[:, 1] = b*100
assert numpy.allclose(a, numpy.asarray(_a))
row = theano._asarray([777, 888, 999], dtype='float32')
_a[1, :] = row
a[1, :] = row
assert numpy.allclose(a, numpy.asarray(_a))
示例13: test_setitem_matrix_bad_shape
# 需要导入模块: from theano.sandbox import cuda [as 别名]
# 或者: from theano.sandbox.cuda import CudaNdarray [as 别名]
def test_setitem_matrix_bad_shape():
a = numpy.arange(27)
a.resize((3, 3, 3))
a = theano._asarray(a, dtype='float32')
_a = cuda_ndarray.CudaNdarray(a)
b = theano._asarray([7, 8], dtype='float32')
_b = cuda_ndarray.CudaNdarray(b)
try:
# attempt to assign the ndarray b with setitem
_a[:, 1, 1] = _b
assert False
except ValueError as e:
# print e
assert True
# test direct transfert from numpy
try:
# attempt to assign the ndarray b with setitem
_a[1, 1, :] = b
assert False
except ValueError as e:
# print e
assert True
示例14: test_setitem_matrix_bad_ndim
# 需要导入模块: from theano.sandbox import cuda [as 别名]
# 或者: from theano.sandbox.cuda import CudaNdarray [as 别名]
def test_setitem_matrix_bad_ndim():
a = numpy.arange(27)
a.resize((3, 3, 3))
a = theano._asarray(a, dtype='float32')
_a = cuda_ndarray.CudaNdarray(a)
b = theano._asarray([7, 8], dtype='float32')
_b = cuda_ndarray.CudaNdarray(b)
try:
# attempt to assign the ndarray b with setitem
_a[:, :, 1] = _b
assert False
except ValueError as e:
# print e
assert True
# test direct transfert from numpy
try:
# attempt to assign the ndarray b with setitem
_a[1, :, :] = b
assert False
except ValueError as e:
# print e
assert True
示例15: test_setitem_matrix_bad_type
# 需要导入模块: from theano.sandbox import cuda [as 别名]
# 或者: from theano.sandbox.cuda import CudaNdarray [as 别名]
def test_setitem_matrix_bad_type():
a = numpy.arange(27)
a.resize((3, 3, 3))
a = theano._asarray(a, dtype='float32')
_a = cuda_ndarray.CudaNdarray(a)
b = theano._asarray([7, 8], dtype='float64')
# test direct transfert from numpy
try:
# attempt to assign the ndarray b with setitem
_a[1, :, :] = b
assert False
except TypeError as e:
# print e
assert True