提交 3343d912 authored 作者: ballasn's avatar ballasn 提交者: GitHub

Merge pull request #5459 from bscellier/import_numpy_misc

Update "import numpy" to "import numpy as np" (theano/misc)
......@@ -13,7 +13,7 @@ import time
from optparse import OptionParser
import subprocess
import numpy
import numpy as np
import theano
import theano.tensor as T
......@@ -47,10 +47,10 @@ def execute(execute=True, verbose=True, M=2000, N=2000, K=2000,
print()
print('Numpy config: (used when the Theano flag'
' "blas.ldflags" is empty)')
numpy.show_config()
print('Numpy dot module:', numpy.dot.__module__)
print('Numpy location:', numpy.__file__)
print('Numpy version:', numpy.__version__)
np.show_config()
print('Numpy dot module:', np.dot.__module__)
print('Numpy location:', np.__file__)
print('Numpy version:', np.__version__)
if (theano.config.device.startswith("gpu") or
theano.config.init_gpu_device.startswith("gpu")):
print('nvcc version:')
......@@ -58,12 +58,12 @@ def execute(execute=True, verbose=True, M=2000, N=2000, K=2000,
"--version"))
print()
a = theano.shared(numpy.ones((M, N), dtype=theano.config.floatX,
order=order))
b = theano.shared(numpy.ones((N, K), dtype=theano.config.floatX,
order=order))
c = theano.shared(numpy.ones((M, K), dtype=theano.config.floatX,
order=order))
a = theano.shared(np.ones((M, N), dtype=theano.config.floatX,
order=order))
b = theano.shared(np.ones((N, K), dtype=theano.config.floatX,
order=order))
c = theano.shared(np.ones((M, K), dtype=theano.config.floatX,
order=order))
f = theano.function([], updates=[(c, 0.4 * c + .8 * T.dot(a, b))])
if any([x.op.__class__.__name__ == 'Gemm' for x in
......
......@@ -9,7 +9,7 @@ from __future__ import absolute_import, print_function, division
import threading
import time
import numpy
import numpy as np
import theano
from theano.gpuarray import init_dev
......@@ -21,7 +21,7 @@ def main(dev1, dev2):
init_dev(dev2, 'ctx2')
size = 1024 * 16
data = numpy.random.randn(size, size).astype('float32')
data = np.random.randn(size, size).astype('float32')
val1a = theano.shared(data, target='ctx1')
val1b = theano.shared(data, target='ctx1')
val1c = theano.shared(data, target='ctx1')
......
......@@ -2,18 +2,18 @@ from __future__ import absolute_import, print_function, division
import time
import numpy
import numpy as np
import theano
y = theano.tensor.fvector()
x = theano.shared(numpy.zeros(1, dtype='float32'))
x = theano.shared(np.zeros(1, dtype='float32'))
f1 = theano.function([y], updates={x: y})
f2 = theano.function([], theano.sandbox.cuda.host_from_gpu(x))
print(f1.maker.fgraph.toposort())
print(f2.maker.fgraph.toposort())
for i in [1, 10, 100, 1000, 10000, 100000, 1000000, 10000000]:
o = numpy.zeros(i, dtype='float32')
o = np.zeros(i, dtype='float32')
t0 = time.time()
f1(o)
t1 = time.time()
......
......@@ -4,7 +4,7 @@ numpy version support only ndarray.
"""
from __future__ import absolute_import, print_function, division
import numpy
import numpy as np
from theano.tensor.basic import TensorType
try:
......@@ -42,8 +42,8 @@ else:
def may_share_memory(a, b, raise_other_type=True):
a_ndarray = isinstance(a, numpy.ndarray)
b_ndarray = isinstance(b, numpy.ndarray)
a_ndarray = isinstance(a, np.ndarray)
b_ndarray = isinstance(b, np.ndarray)
if a_ndarray and b_ndarray:
return TensorType.may_share_memory(a, b)
a_cuda = _is_cuda(a)
......
......@@ -5,7 +5,7 @@ These pickled graphs can be used, for instance, as cases for
unit tests or regression tests.
"""
from __future__ import absolute_import, print_function, division
import numpy
import numpy as np
import os
import pickle
import sys
......@@ -188,10 +188,10 @@ class PersistentNdarrayID(object):
return name
def __call__(self, obj):
if type(obj) is numpy.ndarray:
if type(obj) is np.ndarray:
if id(obj) not in self.seen:
def write_array(f):
numpy.lib.format.write_array(f, obj)
np.lib.format.write_array(f, obj)
name = self._resolve_name(obj)
zipadd(write_array, self.zip_file, name)
self.seen[id(obj)] = 'ndarray.{0}'.format(name)
......@@ -204,7 +204,7 @@ class PersistentCudaNdarrayID(PersistentNdarrayID):
type(obj) is cuda_ndarray.cuda_ndarray.CudaNdarray):
if id(obj) not in self.seen:
def write_array(f):
numpy.lib.format.write_array(f, numpy.asarray(obj))
np.lib.format.write_array(f, np.asarray(obj))
name = self._resolve_name(obj)
zipadd(write_array, self.zip_file, name)
self.seen[id(obj)] = 'cuda_ndarray.{0}'.format(name)
......@@ -283,7 +283,7 @@ class PersistentNdarrayLoad(object):
if name in self.cache:
return self.cache[name]
ret = None
array = numpy.lib.format.read_array(self.zip_file.open(name))
array = np.lib.format.read_array(self.zip_file.open(name))
if array_type == 'cuda_ndarray':
if config.experimental.unpickle_gpu_on_cpu:
# directly return numpy array
......@@ -335,10 +335,10 @@ def dump(obj, file_handler, protocol=DEFAULT_PROTOCOL,
>>> foo_1 = theano.shared(0, name='foo')
>>> foo_2 = theano.shared(1, name='foo')
>>> with open('model.zip', 'wb') as f:
... dump((foo_1, foo_2, numpy.array(2)), f)
>>> numpy.load('model.zip').keys()
... dump((foo_1, foo_2, np.array(2)), f)
>>> np.load('model.zip').keys()
['foo', 'foo_2', 'array_0', 'pkl']
>>> numpy.load('model.zip')['foo']
>>> np.load('model.zip')['foo']
array(0)
>>> with open('model.zip', 'rb') as f:
... foo_1, foo_2, array = load(f)
......
......@@ -22,7 +22,7 @@ TheanoElementwiseKernel.
from __future__ import absolute_import, print_function, division
from itertools import chain
import numpy
import numpy as np
import theano
from six.moves import xrange
......@@ -257,13 +257,13 @@ class PycudaElemwiseSourceModuleOp(GpuOp):
" inputs don't have the same shape!")
if inputs[0].size > 512:
grid = (int(numpy.ceil(inputs[0].size / 512.)), 1)
grid = (int(np.ceil(inputs[0].size / 512.)), 1)
block = (512, 1, 1)
else:
grid = (1, 1)
block = (inputs[0].shape[0], inputs[0].shape[1], 1)
self.pycuda_fct(inputs[0], inputs[1], z[0],
numpy.intc(inputs[1].size), block=block, grid=grid)
np.intc(inputs[1].size), block=block, grid=grid)
class PycudaElemwiseSourceModuleMakeThunkOp(Op):
......@@ -349,13 +349,13 @@ class PycudaElemwiseSourceModuleMakeThunkOp(Op):
" inputs don't have the same shape!")
if inputs[0][0].size > 512:
grid = (int(numpy.ceil(inputs[0][0].size / 512.)), 1)
grid = (int(np.ceil(inputs[0][0].size / 512.)), 1)
block = (512, 1, 1)
else:
grid = (1, 1)
block = (inputs[0][0].shape[0], inputs[0][0].shape[1], 1)
pycuda_fct(inputs[0][0], inputs[1][0], z[0],
numpy.intc(inputs[1][0].size), block=block,
np.intc(inputs[1][0].size), block=block,
grid=grid)
thunk.inputs = inputs
thunk.outputs = outputs
......
......@@ -3,7 +3,7 @@ Helper function to safely convert an array to a new data type.
"""
from __future__ import absolute_import, print_function, division
import numpy
import numpy as np
import theano
......@@ -30,8 +30,8 @@ def _asarray(a, dtype, order=None):
"""
if str(dtype) == 'floatX':
dtype = theano.config.floatX
dtype = numpy.dtype(dtype) # Convert into dtype object.
rval = numpy.asarray(a, dtype=dtype, order=order)
dtype = np.dtype(dtype) # Convert into dtype object.
rval = np.asarray(a, dtype=dtype, order=order)
# Note that dtype comparison must be done by comparing their `num`
# attribute. One cannot assume that two identical data types are pointers
# towards the same object (e.g. under Windows this appears not to be the
......
from __future__ import absolute_import, print_function, division
import numpy
import numpy as np
import theano
from theano.misc.cudamat_utils import cudamat_available
......@@ -20,7 +20,7 @@ def test(shape=(3, 4)):
U = gpu(theano.tensor.fmatrix('U'))
ii = theano.function([U], gpu(U + 1))
A_cpu = numpy.asarray(numpy.random.rand(*shape), dtype="float32")
A_cpu = np.asarray(np.random.rand(*shape), dtype="float32")
A_cnd = theano.sandbox.cuda.CudaNdarray(A_cpu)
A_cmat = cudandarray_to_cudamat(A_cnd)
......@@ -28,9 +28,9 @@ def test(shape=(3, 4)):
B_cnd = ii(A_cnd)
u = A_cnd.copy()
u += theano.sandbox.cuda.CudaNdarray(numpy.asarray([[1]], dtype='float32'))
u = numpy.asarray(u)
v = numpy.asarray(B_cnd)
u += theano.sandbox.cuda.CudaNdarray(np.asarray([[1]], dtype='float32'))
u = np.asarray(u)
v = np.asarray(B_cnd)
w = A_cmat.add(1).asarray()
assert abs(u - v).max() == 0
......
from __future__ import absolute_import, print_function, division
import numpy
import numpy as np
import theano
from theano.misc.gnumpy_utils import gnumpy_available
......@@ -31,11 +31,10 @@ def test(shape=(3, 4, 5)):
B_cnd = ii(A_cnd)
B = cudandarray_to_garray(B_cnd)
assert A_cnd.shape == A.shape
from numpy import array
u = (A + 1).asarray()
v = B.asarray()
w = array(B_cnd)
w = np.array(B_cnd)
assert (u == v).all()
assert (u == w).all()
......@@ -49,7 +48,7 @@ def test2(shape=(3, 4, 5)):
U = gpu(theano.tensor.ftensor3('U'))
theano.function([U], gpu(U + 1))
A = numpy.random.rand(*shape).astype('float32')
A = np.random.rand(*shape).astype('float32')
A_cnd = theano.sandbox.cuda.CudaNdarray(A)
A_gar = cudandarray_to_garray(A_cnd)
assert A_cnd.shape == A_gar.shape
......@@ -62,7 +61,7 @@ def test2(shape=(3, 4, 5)):
# dtype always float32
assert A_cnd._strides == B._strides
assert A_cnd.gpudata == B.gpudata
v = numpy.asarray(B)
v = np.asarray(B)
assert (v == A).all()
......
......@@ -3,7 +3,7 @@ test the tensor and sparse type. The CudaNdarray type is tested in
sandbox/cuda/tests/test_tensor_op.py.test_may_share_memory_cuda
"""
from __future__ import absolute_import, print_function, division
import numpy
import numpy as np
import theano
try:
......@@ -16,8 +16,8 @@ from theano.misc.may_share_memory import may_share_memory
def test_may_share_memory():
a = numpy.random.rand(5, 4)
b = numpy.random.rand(5, 4)
a = np.random.rand(5, 4)
b = np.random.rand(5, 4)
va = a.view()
vb = b.view()
ra = a.reshape((4, 5))
......
......@@ -4,8 +4,7 @@ import shutil
import unittest
from tempfile import mkdtemp
import numpy
from numpy.testing import assert_allclose
import numpy as np
from nose.plugins.skip import SkipTest
import theano
......@@ -44,7 +43,7 @@ class T_dump_load(unittest.TestCase):
x = load(f)
assert x.name == 'x'
assert_allclose(x.get_value(), [[1]])
np.testing.assert_allclose(x.get_value(), [[1]])
def test_dump_load_mrg(self):
rng = MRG_RandomStreams(use_cuda=cuda_ndarray.cuda_enabled)
......@@ -62,14 +61,14 @@ class T_dump_load(unittest.TestCase):
foo_2 = theano.shared(1, name='foo')
foo_3 = theano.shared(2, name='foo')
with open('model.zip', 'wb') as f:
dump((foo_1, foo_2, foo_3, numpy.array(3)), f)
keys = list(numpy.load('model.zip').keys())
dump((foo_1, foo_2, foo_3, np.array(3)), f)
keys = list(np.load('model.zip').keys())
assert keys == ['foo', 'foo_2', 'foo_3', 'array_0', 'pkl']
foo_3 = numpy.load('model.zip')['foo_3']
assert foo_3 == numpy.array(2)
foo_3 = np.load('model.zip')['foo_3']
assert foo_3 == np.array(2)
with open('model.zip', 'rb') as f:
foo_1, foo_2, foo_3, array = load(f)
assert array == numpy.array(3)
assert array == np.array(3)
class TestStripPickler(unittest.TestCase):
......
from __future__ import absolute_import, print_function, division
import numpy
import numpy as np
import theano
import theano.misc.pycuda_init
......@@ -58,11 +58,11 @@ def test_pycuda_elemwise_source_module():
PycudaElemwiseSourceModuleMakeThunkOp)
for node in f4.maker.fgraph.toposort()])
val1 = numpy.asarray(numpy.random.rand(*shape), dtype='float32')
val2 = numpy.asarray(numpy.random.rand(*shape), dtype='float32')
assert numpy.allclose(f(val1, val2), f2(val1, val2))
assert numpy.allclose(f(val1, val2), f3(val1, val2))
assert numpy.allclose(f(val1, val2), f4(val1, val2))
val1 = np.asarray(np.random.rand(*shape), dtype='float32')
val2 = np.asarray(np.random.rand(*shape), dtype='float32')
assert np.allclose(f(val1, val2), f2(val1, val2))
assert np.allclose(f(val1, val2), f3(val1, val2))
assert np.allclose(f(val1, val2), f4(val1, val2))
# print f(val1,val2)
# print f2(val1,val2)
......@@ -82,10 +82,10 @@ def test_pycuda_elemwise_kernel():
assert any([isinstance(node.op, PycudaElemwiseKernelOp)
for node in f2.maker.fgraph.toposort()])
val1 = numpy.asarray(numpy.random.rand(5, 5), dtype='float32')
val2 = numpy.asarray(numpy.random.rand(5, 5), dtype='float32')
#val1 = numpy.ones((5,5))
#val2 = numpy.arange(25).reshape(5,5)
val1 = np.asarray(np.random.rand(5, 5), dtype='float32')
val2 = np.asarray(np.random.rand(5, 5), dtype='float32')
#val1 = np.ones((5,5))
#val2 = np.arange(25).reshape(5,5)
assert (f(val1, val2) == f2(val1, val2)).all()
print(f(val1, val2))
print(f2(val1, val2))
......@@ -99,8 +99,8 @@ def test_pycuda_elemwise_kernel():
assert any([isinstance(node.op, PycudaElemwiseKernelOp)
for node in f4.maker.fgraph.toposort()])
val1 = numpy.random.rand(2, 2, 2)
val1 = np.random.rand(2, 2, 2)
print(val1)
print(f4(val1, val1, val1))
assert numpy.allclose(f4(val1, val1, val1), val1 * val1 + val1)
assert np.allclose(f4(val1, val1, val1), val1 * val1 + val1)
"""
......@@ -8,7 +8,7 @@ from __future__ import absolute_import, print_function, division
import sys
import numpy
import numpy as np
import theano
import theano.sandbox.cuda as cuda_ndarray
......@@ -42,9 +42,9 @@ __global__ void multiply_them(float *dest, float *a, float *b)
multiply_them = mod.get_function("multiply_them")
# Test with pycuda in/out of numpy.ndarray
a = numpy.random.randn(100).astype(numpy.float32)
b = numpy.random.randn(100).astype(numpy.float32)
dest = numpy.zeros_like(a)
a = np.random.randn(100).astype(np.float32)
b = np.random.randn(100).astype(np.float32)
dest = np.zeros_like(a)
multiply_them(
drv.Out(dest), drv.In(a), drv.In(b),
block=(400, 1, 1), grid=(1, 1))
......@@ -64,8 +64,8 @@ __global__ void multiply_them(float *dest, float *a, float *b)
multiply_them = mod.get_function("multiply_them")
a = numpy.random.randn(100).astype(numpy.float32)
b = numpy.random.randn(100).astype(numpy.float32)
a = np.random.randn(100).astype(np.float32)
b = np.random.randn(100).astype(np.float32)
# Test with Theano object
ga = cuda_ndarray.CudaNdarray(a)
......@@ -73,7 +73,7 @@ __global__ void multiply_them(float *dest, float *a, float *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()
assert (np.asarray(dest) == a * b).all()
def test_pycuda_memory_to_theano():
......@@ -87,7 +87,7 @@ def test_pycuda_memory_to_theano():
print("gpuarray ref count before creating a CudaNdarray", end=' ')
print(sys.getrefcount(y))
assert sys.getrefcount(y) == initial_refcount
rand = numpy.random.randn(*y.shape).astype(numpy.float32)
rand = np.random.randn(*y.shape).astype(np.float32)
cuda_rand = cuda_ndarray.CudaNdarray(rand)
strides = [1]
......@@ -102,7 +102,7 @@ def test_pycuda_memory_to_theano():
z = cuda_ndarray.from_gpu_pointer(y_ptr, y.shape, strides, y)
print("gpuarray ref count after creating a CudaNdarray", sys.getrefcount(y))
assert sys.getrefcount(y) == initial_refcount + 1
assert (numpy.asarray(z) == 0).all()
assert (np.asarray(z) == 0).all()
assert z.base is y
# Test that we can take a view from this cuda view on pycuda memory
......@@ -112,17 +112,17 @@ def test_pycuda_memory_to_theano():
del zz
assert sys.getrefcount(y) == initial_refcount + 1
cuda_ones = cuda_ndarray.CudaNdarray(numpy.asarray([[[1]]],
dtype='float32'))
cuda_ones = cuda_ndarray.CudaNdarray(np.asarray([[[1]]],
dtype='float32'))
z += cuda_ones
assert (numpy.asarray(z) == numpy.ones(y.shape)).all()
assert (numpy.asarray(z) == 1).all()
assert (np.asarray(z) == np.ones(y.shape)).all()
assert (np.asarray(z) == 1).all()
assert cuda_rand.shape == z.shape
assert cuda_rand._strides == z._strides, (cuda_rand._strides, z._strides)
assert (numpy.asarray(cuda_rand) == rand).all()
assert (np.asarray(cuda_rand) == rand).all()
z += cuda_rand
assert (numpy.asarray(z) == (rand + 1)).all()
assert (np.asarray(z) == (rand + 1)).all()
# Check that the ref count to the gpuarray is right.
del z
......
from __future__ import absolute_import, print_function, division
import numpy
import numpy as np
import theano.sandbox.cuda as cuda
import theano.misc.pycuda_init
......@@ -22,30 +22,30 @@ def test_to_gpuarray():
px = to_gpuarray(cx)
assert isinstance(px, pycuda.gpuarray.GPUArray)
cx[0, 0] = numpy.asarray(1, dtype="float32")
cx[0, 0] = np.asarray(1, dtype="float32")
# Check that they share the same memory space
assert px.gpudata == cx.gpudata
assert numpy.asarray(cx[0, 0]) == 1
assert np.asarray(cx[0, 0]) == 1
assert numpy.allclose(numpy.asarray(cx), px.get())
assert np.allclose(np.asarray(cx), px.get())
assert px.dtype == cx.dtype
assert px.shape == cx.shape
assert all(numpy.asarray(cx._strides) * 4 == px.strides)
assert all(np.asarray(cx._strides) * 4 == px.strides)
# Test when the CudaNdarray is strided
cx = cx[::2, ::]
px = to_gpuarray(cx, copyif=True)
assert isinstance(px, pycuda.gpuarray.GPUArray)
cx[0, 0] = numpy.asarray(2, dtype="float32")
cx[0, 0] = np.asarray(2, dtype="float32")
# Check that they do not share the same memory space
assert px.gpudata != cx.gpudata
assert numpy.asarray(cx[0, 0]) == 2
assert not numpy.allclose(numpy.asarray(cx), px.get())
assert np.asarray(cx[0, 0]) == 2
assert not np.allclose(np.asarray(cx), px.get())
assert px.dtype == cx.dtype
assert px.shape == cx.shape
assert not all(numpy.asarray(cx._strides) * 4 == px.strides)
assert not all(np.asarray(cx._strides) * 4 == px.strides)
# Test that we return an error
try:
......@@ -59,11 +59,11 @@ 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 np.allclose(px.get(),
np.asarray(cx))
assert px.dtype == cx.dtype
assert px.shape == cx.shape
assert all(numpy.asarray(cx._strides) * 4 == px.strides)
assert all(np.asarray(cx._strides) * 4 == px.strides)
try:
px = pycuda.gpuarray.zeros((3, 4, 5), 'float64')
......@@ -73,7 +73,7 @@ def test_to_cudandarray():
pass
try:
to_cudandarray(numpy.zeros(4))
to_cudandarray(np.zeros(4))
assert False
except ValueError:
pass
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论