提交 2730024a authored 作者: amrithasuresh's avatar amrithasuresh

Updated numpy as np

上级 5e5d1bb2
...@@ -2,7 +2,7 @@ from __future__ import absolute_import, print_function, division ...@@ -2,7 +2,7 @@ from __future__ import absolute_import, print_function, division
import sys import sys
from copy import copy from copy import copy
import numpy import numpy as np
from six import iteritems, integer_types from six import iteritems, integer_types
from six.moves import xrange from six.moves import xrange
...@@ -21,7 +21,7 @@ from theano.misc.frozendict import frozendict ...@@ -21,7 +21,7 @@ from theano.misc.frozendict import frozendict
config = theano.config config = theano.config
_numpy_ver = [int(n) for n in numpy.__version__.split('.')[:2]] _numpy_ver = [int(n) for n in np.__version__.split('.')[:2]]
# tensor depends on elemwise to provide definitions for several ops # tensor depends on elemwise to provide definitions for several ops
...@@ -148,7 +148,7 @@ class DimShuffle(Op): ...@@ -148,7 +148,7 @@ class DimShuffle(Op):
# isinstance(x, integer_types) returning False for # isinstance(x, integer_types) returning False for
# numpy integers. See # numpy integers. See
# <http://projects.scipy.org/numpy/ticket/2235>. # <http://projects.scipy.org/numpy/ticket/2235>.
if not isinstance(j, (integer_types, numpy.integer)): if not isinstance(j, (integer_types, np.integer)):
raise TypeError( raise TypeError(
"DimShuffle indices must be python ints. " "DimShuffle indices must be python ints. "
"Got: '%s' of type '%s'.", "Got: '%s' of type '%s'.",
...@@ -228,7 +228,7 @@ class DimShuffle(Op): ...@@ -228,7 +228,7 @@ class DimShuffle(Op):
storage, = out storage, = out
# drop # drop
res = input res = input
if type(res) != numpy.ndarray and type(res) != numpy.memmap: if type(res) != np.ndarray and type(res) != np.memmap:
raise TypeError(res) raise TypeError(res)
# transpose # transpose
...@@ -242,9 +242,9 @@ class DimShuffle(Op): ...@@ -242,9 +242,9 @@ class DimShuffle(Op):
# copy (if not inplace) # copy (if not inplace)
if not self.inplace: if not self.inplace:
res = numpy.copy(res) res = np.copy(res)
storage[0] = numpy.asarray(res) # asarray puts scalars back into array storage[0] = np.asarray(res) # asarray puts scalars back into array
def infer_shape(self, node, shapes): def infer_shape(self, node, shapes):
ishp, = shapes ishp, = shapes
...@@ -487,7 +487,7 @@ second dimension ...@@ -487,7 +487,7 @@ second dimension
nfunc_spec = getattr(scalar_op, 'nfunc_spec', None) nfunc_spec = getattr(scalar_op, 'nfunc_spec', None)
self.nfunc_spec = nfunc_spec self.nfunc_spec = nfunc_spec
if nfunc_spec: if nfunc_spec:
self.nfunc = getattr(numpy, nfunc_spec[0]) self.nfunc = getattr(np, nfunc_spec[0])
super(Elemwise, self).__init__(openmp=openmp) super(Elemwise, self).__init__(openmp=openmp)
...@@ -504,9 +504,9 @@ second dimension ...@@ -504,9 +504,9 @@ second dimension
self.nfunc = None self.nfunc = None
self.inplace_pattern = frozendict(self.inplace_pattern) self.inplace_pattern = frozendict(self.inplace_pattern)
if getattr(self, 'nfunc_spec', None): if getattr(self, 'nfunc_spec', None):
self.nfunc = getattr(numpy, self.nfunc_spec[0]) self.nfunc = getattr(np, self.nfunc_spec[0])
elif 0 < self.scalar_op.nin < 32: elif 0 < self.scalar_op.nin < 32:
self.ufunc = numpy.frompyfunc(self.scalar_op.impl, self.ufunc = np.frompyfunc(self.scalar_op.impl,
self.scalar_op.nin, self.scalar_op.nin,
self.scalar_op.nout) self.scalar_op.nout)
...@@ -723,7 +723,7 @@ second dimension ...@@ -723,7 +723,7 @@ second dimension
# the gradient contains a constant, translate it as # the gradient contains a constant, translate it as
# an equivalent TensorType of size 1 and proper number of # an equivalent TensorType of size 1 and proper number of
# dimensions # dimensions
res = theano.tensor.constant(numpy.asarray(r.data), res = theano.tensor.constant(np.asarray(r.data),
dtype=r.type.dtype) dtype=r.type.dtype)
return DimShuffle((), ['x'] * nd)(res) return DimShuffle((), ['x'] * nd)(res)
...@@ -750,7 +750,7 @@ second dimension ...@@ -750,7 +750,7 @@ second dimension
self.ufunc is None and self.ufunc is None and
impl == 'py'): impl == 'py'):
ufunc = numpy.frompyfunc(self.scalar_op.impl, ufunc = np.frompyfunc(self.scalar_op.impl,
len(node.inputs), len(node.inputs),
self.scalar_op.nout) self.scalar_op.nout)
if self.scalar_op.nin > 0: if self.scalar_op.nin > 0:
...@@ -772,9 +772,9 @@ second dimension ...@@ -772,9 +772,9 @@ second dimension
# when the input is complex. So add it only when inputs is int. # when the input is complex. So add it only when inputs is int.
out_dtype = node.outputs[0].dtype out_dtype = node.outputs[0].dtype
if (out_dtype in theano.tensor.float_dtypes and if (out_dtype in theano.tensor.float_dtypes and
isinstance(self.nfunc, numpy.ufunc) and isinstance(self.nfunc, np.ufunc) and
node.inputs[0].dtype in theano.tensor.discrete_dtypes): node.inputs[0].dtype in theano.tensor.discrete_dtypes):
char = numpy.sctype2char(out_dtype) char = np.sctype2char(out_dtype)
sig = char * node.nin + '->' + char * node.nout sig = char * node.nin + '->' + char * node.nout
node.tag.sig = sig node.tag.sig = sig
node.tag.fake_node = Apply( node.tag.fake_node = Apply(
...@@ -870,7 +870,7 @@ second dimension ...@@ -870,7 +870,7 @@ second dimension
if getattr(variable, "dtype", "") == 'object': if getattr(variable, "dtype", "") == 'object':
# Since numpy 1.6, function created with numpy.frompyfunc # Since numpy 1.6, function created with numpy.frompyfunc
# always return an ndarray with dtype object # always return an ndarray with dtype object
variable = numpy.asarray(variable, dtype=nout.dtype) variable = np.asarray(variable, dtype=nout.dtype)
if i in self.inplace_pattern: if i in self.inplace_pattern:
odat = inputs[self.inplace_pattern[i]] odat = inputs[self.inplace_pattern[i]]
...@@ -879,15 +879,15 @@ second dimension ...@@ -879,15 +879,15 @@ second dimension
# Sometimes NumPy return a Python type. # Sometimes NumPy return a Python type.
# Some Theano op return a different dtype like floor, ceil, # Some Theano op return a different dtype like floor, ceil,
# trunc, eq, ... # trunc, eq, ...
elif (not isinstance(variable, numpy.ndarray) or elif (not isinstance(variable, np.ndarray) or
variable.dtype != nout.dtype): variable.dtype != nout.dtype):
variable = numpy.asarray(variable, nout.dtype) variable = np.asarray(variable, nout.dtype)
# The next line is needed for numpy 1.9. Otherwise # The next line is needed for numpy 1.9. Otherwise
# there are tests that fail in DebugMode. # there are tests that fail in DebugMode.
# Normally we would call theano.misc._asarray, but it # Normally we would call theano.misc._asarray, but it
# is faster to inline the code. We know that the dtype # is faster to inline the code. We know that the dtype
# are the same string, just different typenum. # are the same string, just different typenum.
if numpy.dtype(nout.dtype).num != variable.dtype.num: if np.dtype(nout.dtype).num != variable.dtype.num:
variable = variable.view(dtype=nout.dtype) variable = variable.view(dtype=nout.dtype)
storage[0] = variable storage[0] = variable
# numpy.real return a view! # numpy.real return a view!
...@@ -1302,9 +1302,9 @@ class CAReduce(Op): ...@@ -1302,9 +1302,9 @@ class CAReduce(Op):
# There is a bug in numpy that results in isinstance(x, # There is a bug in numpy that results in isinstance(x,
# integer_types) returning False for numpy integers. See # integer_types) returning False for numpy integers. See
# <http://projects.scipy.org/numpy/ticket/2235>. # <http://projects.scipy.org/numpy/ticket/2235>.
elif isinstance(axis, (integer_types, numpy.integer)): elif isinstance(axis, (integer_types, np.integer)):
self.axis = (axis,) self.axis = (axis,)
elif isinstance(axis, numpy.ndarray) and axis.ndim == 0: elif isinstance(axis, np.ndarray) and axis.ndim == 0:
self.axis = (int(axis),) self.axis = (int(axis),)
else: else:
self.axis = list(set(int(a) for a in axis)) self.axis = list(set(int(a) for a in axis))
...@@ -1316,26 +1316,26 @@ class CAReduce(Op): ...@@ -1316,26 +1316,26 @@ class CAReduce(Op):
def set_ufunc(self, scalar_op): def set_ufunc(self, scalar_op):
# This is probably a speed up of the implementation # This is probably a speed up of the implementation
if isinstance(scalar_op, theano.scalar.basic.Add): if isinstance(scalar_op, theano.scalar.basic.Add):
self.ufunc = numpy.add self.ufunc = np.add
elif isinstance(scalar_op, theano.scalar.basic.Mul): elif isinstance(scalar_op, theano.scalar.basic.Mul):
self.ufunc = numpy.multiply self.ufunc = np.multiply
elif isinstance(scalar_op, theano.scalar.basic.Maximum): elif isinstance(scalar_op, theano.scalar.basic.Maximum):
self.ufunc = numpy.maximum self.ufunc = np.maximum
elif isinstance(scalar_op, theano.scalar.basic.Minimum): elif isinstance(scalar_op, theano.scalar.basic.Minimum):
self.ufunc = numpy.minimum self.ufunc = np.minimum
elif (isinstance(scalar_op, theano.scalar.basic.AND) and elif (isinstance(scalar_op, theano.scalar.basic.AND) and
_numpy_ver >= [1, 12]): _numpy_ver >= [1, 12]):
# numpy.bitwise_and.identity was incorrect for versions before # numpy.bitwise_and.identity was incorrect for versions before
# 1.12 (it was 1 instead of -1), so we skip it in that case. # 1.12 (it was 1 instead of -1), so we skip it in that case.
# We will fall back to the "else:" case, which defines a # We will fall back to the "else:" case, which defines a
# ufunc without identity. # ufunc without identity.
self.ufunc = numpy.bitwise_and self.ufunc = np.bitwise_and
elif isinstance(scalar_op, theano.scalar.basic.OR): elif isinstance(scalar_op, theano.scalar.basic.OR):
self.ufunc = numpy.bitwise_or self.ufunc = np.bitwise_or
elif isinstance(scalar_op, theano.scalar.basic.XOR): elif isinstance(scalar_op, theano.scalar.basic.XOR):
self.ufunc = numpy.bitwise_xor self.ufunc = np.bitwise_xor
else: else:
self.ufunc = numpy.frompyfunc(scalar_op.impl, 2, 1) self.ufunc = np.frompyfunc(scalar_op.impl, 2, 1)
def _output_dtype(self, input_dtype): def _output_dtype(self, input_dtype):
return input_dtype return input_dtype
...@@ -1415,7 +1415,7 @@ class CAReduce(Op): ...@@ -1415,7 +1415,7 @@ class CAReduce(Op):
# Compute the shape of the output # Compute the shape of the output
v_shape = list(variable.shape) v_shape = list(variable.shape)
del v_shape[dimension] del v_shape[dimension]
variable = numpy.empty(tuple(v_shape), variable = np.empty(tuple(v_shape),
dtype=acc_dtype) dtype=acc_dtype)
variable.fill(self.scalar_op.identity) variable.fill(self.scalar_op.identity)
else: else:
...@@ -1427,8 +1427,8 @@ class CAReduce(Op): ...@@ -1427,8 +1427,8 @@ class CAReduce(Op):
variable = self.ufunc.reduce(variable, dimension, variable = self.ufunc.reduce(variable, dimension,
dtype=acc_dtype) dtype=acc_dtype)
variable = numpy.asarray(variable) variable = np.asarray(variable)
if numpy.may_share_memory(variable, input): if np.may_share_memory(variable, input):
# perhaps numpy is clever for reductions of size 1? # perhaps numpy is clever for reductions of size 1?
# We don't want this. # We don't want this.
variable = variable.copy() variable = variable.copy()
...@@ -1436,7 +1436,7 @@ class CAReduce(Op): ...@@ -1436,7 +1436,7 @@ class CAReduce(Op):
dtype=node.outputs[0].type.dtype) dtype=node.outputs[0].type.dtype)
else: else:
# Force a copy # Force a copy
output[0] = numpy.array(variable, copy=True, output[0] = np.array(variable, copy=True,
dtype=node.outputs[0].type.dtype) dtype=node.outputs[0].type.dtype)
def infer_shape(self, node, shapes): def infer_shape(self, node, shapes):
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论