提交 7c480a16 authored 作者: amrithasuresh's avatar amrithasuresh

Updated numpy as np

上级 6c1db380
...@@ -4,7 +4,7 @@ from __future__ import absolute_import, print_function, division ...@@ -4,7 +4,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 string_types from six import string_types
from six.moves import reduce, xrange from six.moves import reduce, xrange
...@@ -38,7 +38,7 @@ class RandomStateType(gof.Type): ...@@ -38,7 +38,7 @@ class RandomStateType(gof.Type):
raise TypeError() raise TypeError()
def is_valid_value(self, a): def is_valid_value(self, a):
return type(a) == numpy.random.RandomState return type(a) == np.random.RandomState
def values_eq(self, a, b): def values_eq(self, a, b):
sa = a.get_state() sa = a.get_state()
...@@ -47,7 +47,7 @@ class RandomStateType(gof.Type): ...@@ -47,7 +47,7 @@ class RandomStateType(gof.Type):
if sa[0] != sb[0]: if sa[0] != sb[0]:
return False return False
# 1-D array of 624 unsigned integer keys # 1-D array of 624 unsigned integer keys
if not numpy.all(sa[1] == sb[1]): if not np.all(sa[1] == sb[1]):
return False return False
# integer "pos" representing the position in the array # integer "pos" representing the position in the array
if sa[2] != sb[2]: if sa[2] != sb[2]:
...@@ -67,17 +67,17 @@ class RandomStateType(gof.Type): ...@@ -67,17 +67,17 @@ class RandomStateType(gof.Type):
def get_size(self, shape_info): def get_size(self, shape_info):
# The size is the data, that have constant size. # The size is the data, that have constant size.
state = numpy.random.RandomState().get_state() state = np.random.RandomState().get_state()
size = 0 size = 0
for elem in state: for elem in state:
if isinstance(elem, str): if isinstance(elem, str):
size += len(elem) size += len(elem)
elif isinstance(elem, numpy.ndarray): elif isinstance(elem, np.ndarray):
size += elem.size * elem.itemsize size += elem.size * elem.itemsize
elif isinstance(elem, int): elif isinstance(elem, int):
size += numpy.dtype("int").itemsize size += np.dtype("int").itemsize
elif isinstance(elem, float): elif isinstance(elem, float):
size += numpy.dtype("float").itemsize size += np.dtype("float").itemsize
else: else:
raise NotImplementedError() raise NotImplementedError()
return size return size
...@@ -151,7 +151,7 @@ class RandomFunction(gof.Op): ...@@ -151,7 +151,7 @@ class RandomFunction(gof.Op):
fn, outtype, inplace, ndim_added = state fn, outtype, inplace, ndim_added = state
self.fn = fn self.fn = fn
if isinstance(fn, string_types): if isinstance(fn, string_types):
self.exec_fn = getattr(numpy.random.RandomState, fn) self.exec_fn = getattr(np.random.RandomState, fn)
else: else:
self.exec_fn = fn self.exec_fn = fn
self.outtype = outtype self.outtype = outtype
...@@ -240,7 +240,7 @@ class RandomFunction(gof.Op): ...@@ -240,7 +240,7 @@ class RandomFunction(gof.Op):
# Numbers are drawn from r if self.inplace is True, and from a # Numbers are drawn from r if self.inplace is True, and from a
# copy of r if self.inplace is False # copy of r if self.inplace is False
r, shape, args = inputs[0], inputs[1], inputs[2:] r, shape, args = inputs[0], inputs[1], inputs[2:]
assert type(r) == numpy.random.RandomState, (type(r), r) assert type(r) == np.random.RandomState, (type(r), r)
# If shape == [], that means no shape is enforced, and numpy is # If shape == [], that means no shape is enforced, and numpy is
# trusted to draw the appropriate number of samples, numpy uses # trusted to draw the appropriate number of samples, numpy uses
...@@ -260,7 +260,7 @@ class RandomFunction(gof.Op): ...@@ -260,7 +260,7 @@ class RandomFunction(gof.Op):
r = copy(r) r = copy(r)
rout[0] = r rout[0] = r
rval = self.exec_fn(r, *(args + [shape])) rval = self.exec_fn(r, *(args + [shape]))
if (not isinstance(rval, numpy.ndarray) or if (not isinstance(rval, np.ndarray) or
str(rval.dtype) != node.outputs[1].type.dtype): str(rval.dtype) != node.outputs[1].type.dtype):
rval = theano._asarray(rval, dtype=node.outputs[1].type.dtype) rval = theano._asarray(rval, dtype=node.outputs[1].type.dtype)
...@@ -527,13 +527,13 @@ def binomial(random_state, size=None, n=1, p=0.5, ndim=None, ...@@ -527,13 +527,13 @@ def binomial(random_state, size=None, n=1, p=0.5, ndim=None,
""" """
if prob is not None: if prob is not None:
p = prob p = prob
print("DEPRECATION WARNING: the parameter prob to the binomal fct have been renamed to p to have the same name as numpy.", file=sys.stderr) print("DEPRECATION WARNING: the parameter prob to the binomal fct have been renamed to p to have the same name as np.", file=sys.stderr)
n = tensor.as_tensor_variable(n) n = tensor.as_tensor_variable(n)
p = tensor.as_tensor_variable(p) p = tensor.as_tensor_variable(p)
ndim, size, bcast = _infer_ndim_bcast(ndim, size, n, p) ndim, size, bcast = _infer_ndim_bcast(ndim, size, n, p)
if n.dtype == 'int64': if n.dtype == 'int64':
try: try:
numpy.random.binomial(n=numpy.asarray([2, 3, 4], dtype='int64'), p=numpy.asarray([.1, .2, .3], dtype='float64')) np.random.binomial(n=np.asarray([2, 3, 4], dtype='int64'), p=np.asarray([.1, .2, .3], dtype='float64'))
except TypeError: except TypeError:
# THIS WORKS AROUND A NUMPY BUG on 32bit machine # THIS WORKS AROUND A NUMPY BUG on 32bit machine
n = tensor.cast(n, 'int32') n = tensor.cast(n, 'int32')
...@@ -583,7 +583,7 @@ def random_integers_helper(random_state, low, high, size): ...@@ -583,7 +583,7 @@ def random_integers_helper(random_state, low, high, size):
out_size = out_size + (dim_len,) out_size = out_size + (dim_len,)
# Build the indices over which to loop # Build the indices over which to loop
out = numpy.ndarray(out_size) out = np.ndarray(out_size)
broadcast_ind = _generate_broadcasting_indices(out_size, low.shape, broadcast_ind = _generate_broadcasting_indices(out_size, low.shape,
high.shape) high.shape)
# Iterate over these indices, drawing one sample at a time from numpy # Iterate over these indices, drawing one sample at a time from numpy
...@@ -716,8 +716,8 @@ def permutation_helper(random_state, n, shape): ...@@ -716,8 +716,8 @@ def permutation_helper(random_state, n, shape):
shape = () shape = ()
out_shape = list(shape) out_shape = list(shape)
out_shape.append(n) out_shape.append(n)
out = numpy.empty(out_shape, int) out = np.empty(out_shape, int)
for i in numpy.ndindex(*shape): for i in np.ndindex(*shape):
out[i] = random_state.permutation(n) out[i] = random_state.permutation(n)
# print 'RETURNING', out.shape # print 'RETURNING', out.shape
...@@ -801,7 +801,7 @@ def multinomial_helper(random_state, n, pvals, size): ...@@ -801,7 +801,7 @@ def multinomial_helper(random_state, n, pvals, size):
# Build the indices over which to loop # Build the indices over which to loop
# Note that here, the rows (inner-most 1D subtensors) of pvals and out # Note that here, the rows (inner-most 1D subtensors) of pvals and out
# are indexed, not their individual elements # are indexed, not their individual elements
out = numpy.ndarray(out_size) out = np.ndarray(out_size)
broadcast_ind = _generate_broadcasting_indices(size, n.shape, broadcast_ind = _generate_broadcasting_indices(size, n.shape,
pvals.shape[:-1]) pvals.shape[:-1])
# Iterate over these indices, drawing from one multinomial at a # Iterate over these indices, drawing from one multinomial at a
...@@ -815,16 +815,16 @@ def multinomial_helper(random_state, n, pvals, size): ...@@ -815,16 +815,16 @@ def multinomial_helper(random_state, n, pvals, size):
# of probabilities meets or exceeds 1.0. # of probabilities meets or exceeds 1.0.
# In perfect arithmetic this would be correct, but in float32 or # In perfect arithmetic this would be correct, but in float32 or
# float64 it is too strict. # float64 it is too strict.
pisum = numpy.sum(pvi) pisum = np.sum(pvi)
if 1.0 < pisum < 1.0 + 1e-5: # correct if we went a little over if 1.0 < pisum < 1.0 + 1e-5: # correct if we went a little over
# because mtrand.pyx has a ValueError that will trigger if # because mtrand.pyx has a ValueError that will trigger if
# sum(pvals[:-1]) > 1.0 # sum(pvals[:-1]) > 1.0
pvi = pvi * (1.0 - 5e-5) pvi = pvi * (1.0 - 5e-5)
# pvi = pvi * .9 # pvi = pvi * .9
pisum = numpy.sum(pvi) pisum = np.sum(pvi)
elif pvi[-1] < 5e-5: # will this even work? elif pvi[-1] < 5e-5: # will this even work?
pvi = pvi * (1.0 - 5e-5) pvi = pvi * (1.0 - 5e-5)
pisum = numpy.sum(pvi) pisum = np.sum(pvi)
assert pisum <= 1.0, pisum assert pisum <= 1.0, pisum
out[mi] = random_state.multinomial(n=n[ni], out[mi] = random_state.multinomial(n=n[ni],
pvals=pvi.astype('float64')) pvals=pvi.astype('float64'))
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论