提交 de366285 authored 作者: Benjamin Scellier's avatar Benjamin Scellier

file theano/gpuarray/type.py

上级 8d809317
from __future__ import absolute_import, print_function, division from __future__ import absolute_import, print_function, division
import numpy import numpy as np
import six.moves.copyreg as copyreg import six.moves.copyreg as copyreg
from six import iteritems from six import iteritems
import warnings import warnings
...@@ -226,7 +226,7 @@ class GpuArrayType(Type): ...@@ -226,7 +226,7 @@ class GpuArrayType(Type):
converted_data = theano._asarray(data, self.dtype) converted_data = theano._asarray(data, self.dtype)
# We use the `values_eq` static function from TensorType # We use the `values_eq` static function from TensorType
# to handle NaN values. # to handle NaN values.
if TensorType.values_eq(numpy.asarray(data), if TensorType.values_eq(np.asarray(data),
converted_data, converted_data,
force_same_dtype=False): force_same_dtype=False):
data = converted_data data = converted_data
...@@ -293,18 +293,18 @@ class GpuArrayType(Type): ...@@ -293,18 +293,18 @@ class GpuArrayType(Type):
return False return False
if force_same_dtype and a.typecode != b.typecode: if force_same_dtype and a.typecode != b.typecode:
return False return False
a_eq_b = numpy.asarray(compare(a, '==', b)) a_eq_b = np.asarray(compare(a, '==', b))
if a_eq_b.all(): if a_eq_b.all():
return True return True
# maybe the trouble is that there are NaNs # maybe the trouble is that there are NaNs
a = numpy.asarray(a) a = np.asarray(a)
b = numpy.asarray(b) b = np.asarray(b)
a_missing = numpy.isnan(a) a_missing = np.isnan(a)
if a_missing.any(): if a_missing.any():
b_missing = numpy.isnan(b) b_missing = np.isnan(b)
return numpy.all(a_eq_b + (a_missing == b_missing)) return np.all(a_eq_b + (a_missing == b_missing))
else: else:
return False return False
...@@ -326,16 +326,16 @@ class GpuArrayType(Type): ...@@ -326,16 +326,16 @@ class GpuArrayType(Type):
rtol_ = rtol rtol_ = rtol
if atol is not None: if atol is not None:
atol_ = atol atol_ = atol
res = elemwise2(a, '', b, a, odtype=numpy.dtype('bool'), res = elemwise2(a, '', b, a, odtype=np.dtype('bool'),
op_tmpl="res = (fabs(a - b) <" op_tmpl="res = (fabs(a - b) <"
"(%(atol_)s + %(rtol_)s * fabs(b)))" % "(%(atol_)s + %(rtol_)s * fabs(b)))" %
locals()) locals())
ret = numpy.asarray(res).all() ret = np.asarray(res).all()
if ret: if ret:
return True return True
# maybe the trouble is that there are NaNs # maybe the trouble is that there are NaNs
an = numpy.asarray(a) an = np.asarray(a)
bn = numpy.asarray(b) bn = np.asarray(b)
return tensor.TensorType.values_eq_approx( return tensor.TensorType.values_eq_approx(
an, bn, allow_remove_inf=allow_remove_inf, an, bn, allow_remove_inf=allow_remove_inf,
allow_remove_nan=allow_remove_nan, rtol=rtol, atol=atol) allow_remove_nan=allow_remove_nan, rtol=rtol, atol=atol)
...@@ -408,9 +408,9 @@ class GpuArrayType(Type): ...@@ -408,9 +408,9 @@ class GpuArrayType(Type):
def get_size(self, shape_info): def get_size(self, shape_info):
if shape_info: if shape_info:
return numpy.prod(shape_info) * numpy.dtype(self.dtype).itemsize return np.prod(shape_info) * np.dtype(self.dtype).itemsize
else: else:
return numpy.dtype(self.dtype).itemsize return np.dtype(self.dtype).itemsize
def c_declare(self, name, sub, check_input=True): def c_declare(self, name, sub, check_input=True):
return """ return """
...@@ -470,7 +470,7 @@ class GpuArrayType(Type): ...@@ -470,7 +470,7 @@ class GpuArrayType(Type):
'<gpuarray_api.h>'] '<gpuarray_api.h>']
def c_header_dirs(self): def c_header_dirs(self):
return [pygpu.get_include(), numpy.get_include()] return [pygpu.get_include(), np.get_include()]
def c_libraries(self): def c_libraries(self):
return ['gpuarray'] return ['gpuarray']
...@@ -509,7 +509,7 @@ class GpuArrayVariable(_operators, Variable): ...@@ -509,7 +509,7 @@ class GpuArrayVariable(_operators, Variable):
# override the default # override the default
def __repr_test_value__(self): def __repr_test_value__(self):
return repr(numpy.array(theano.gof.op.get_test_value(self))) return repr(np.array(theano.gof.op.get_test_value(self)))
GpuArrayType.Variable = GpuArrayVariable GpuArrayType.Variable = GpuArrayVariable
...@@ -534,13 +534,13 @@ class GpuArrayConstant(_operators, Constant): ...@@ -534,13 +534,13 @@ class GpuArrayConstant(_operators, Constant):
""" """
def signature(self): def signature(self):
return GpuArraySignature((self.type, numpy.asarray(self.data))) return GpuArraySignature((self.type, np.asarray(self.data)))
def __str__(self): def __str__(self):
if self.name is not None: if self.name is not None:
return self.name return self.name
try: try:
np_data = numpy.asarray(self.data) np_data = np.asarray(self.data)
except gpuarray.GpuArrayException: except gpuarray.GpuArrayException:
np_data = self.data np_data = self.data
return "GpuArrayConstant{%s}" % np_data return "GpuArrayConstant{%s}" % np_data
...@@ -568,7 +568,7 @@ class GpuArraySharedVariable(_operators, SharedVariable): ...@@ -568,7 +568,7 @@ class GpuArraySharedVariable(_operators, SharedVariable):
else: else:
return self.container.value.copy() return self.container.value.copy()
else: else:
return numpy.asarray(self.container.value) return np.asarray(self.container.value)
def set_value(self, value, borrow=False): def set_value(self, value, borrow=False):
if isinstance(value, pygpu.gpuarray.GpuArray): if isinstance(value, pygpu.gpuarray.GpuArray):
...@@ -601,7 +601,7 @@ def gpuarray_shared_constructor(value, name=None, strict=False, ...@@ -601,7 +601,7 @@ def gpuarray_shared_constructor(value, name=None, strict=False,
if target == 'gpu' or target == 'cpu': if target == 'gpu' or target == 'cpu':
raise TypeError('not for me') raise TypeError('not for me')
if not isinstance(value, (numpy.ndarray, pygpu.gpuarray.GpuArray)): if not isinstance(value, (np.ndarray, pygpu.gpuarray.GpuArray)):
raise TypeError('ndarray or GpuArray required') raise TypeError('ndarray or GpuArray required')
if target is notset: if target is notset:
...@@ -809,7 +809,7 @@ copyreg.constructor(GpuArray_unpickler) ...@@ -809,7 +809,7 @@ copyreg.constructor(GpuArray_unpickler)
def GpuArray_pickler(cnda): def GpuArray_pickler(cnda):
ctx_name = _name_for_ctx(cnda.context) ctx_name = _name_for_ctx(cnda.context)
return (GpuArray_unpickler, (numpy.asarray(cnda), ctx_name)) return (GpuArray_unpickler, (np.asarray(cnda), ctx_name))
# In case pygpu is not imported. # In case pygpu is not imported.
if pygpu is not None: if pygpu is not None:
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论