提交 19f7768f authored 作者: David Warde-Farley's avatar David Warde-Farley

in range() -> in xrange() where possible

上级 94349257
...@@ -94,7 +94,7 @@ class VM(object): ...@@ -94,7 +94,7 @@ class VM(object):
profile.apply_cimpl[node] = hasattr(thunk,'cthunk') profile.apply_cimpl[node] = hasattr(thunk,'cthunk')
# clear the timer info out of the buffers # clear the timer info out of the buffers
for i in range(len(self.call_times)): for i in xrange(len(self.call_times)):
self.call_times[i] = 0.0 self.call_times[i] = 0.0
self.call_counts[i] = 0 self.call_counts[i] = 0
...@@ -459,9 +459,9 @@ class VM_Linker(link.LocalLinker): ...@@ -459,9 +459,9 @@ class VM_Linker(link.LocalLinker):
# put storage_map and compute_map into a int-based scheme # put storage_map and compute_map into a int-based scheme
n_applies = len(nodes) n_applies = len(nodes)
storage_map_list = [storage_map[vars_idx_inv[i]] storage_map_list = [storage_map[vars_idx_inv[i]]
for i in range(len(vars_idx_inv))] for i in xrange(len(vars_idx_inv))]
compute_map_list = [compute_map[vars_idx_inv[i]] compute_map_list = [compute_map[vars_idx_inv[i]]
for i in range(len(vars_idx_inv))] for i in xrange(len(vars_idx_inv))]
if nodes: if nodes:
assert type(storage_map_list[0]) is list assert type(storage_map_list[0]) is list
assert type(compute_map_list[0]) is list assert type(compute_map_list[0]) is list
......
...@@ -803,8 +803,8 @@ class GpuSum(Op): ...@@ -803,8 +803,8 @@ class GpuSum(Op):
assert N in [1,2,3] assert N in [1,2,3]
makecall = self._makecall(node, name, x, z, fail) makecall = self._makecall(node, name, x, z, fail)
N_pattern = ''.join(['1']*N) N_pattern = ''.join(['1']*N)
param_dim = ",".join(["CudaNdarray_HOST_DIMS(%(x)s)[%(i)s]"%locals() for i in range(N+1)]) param_dim = ",".join(["CudaNdarray_HOST_DIMS(%(x)s)[%(i)s]"%locals() for i in xrange(N+1)])
strides_dim = ",".join(["CudaNdarray_HOST_STRIDES(%(x)s)[%(i)s]"%locals() for i in range(N+1)]) strides_dim = ",".join(["CudaNdarray_HOST_STRIDES(%(x)s)[%(i)s]"%locals() for i in xrange(N+1)])
threads_y = """ threads_y = """
//get as many y threads as we can fit //get as many y threads as we can fit
while (n_threads.x * (n_threads.y+1) <= NUM_VECTOR_OP_THREADS_PER_BLOCK) while (n_threads.x * (n_threads.y+1) <= NUM_VECTOR_OP_THREADS_PER_BLOCK)
...@@ -1326,8 +1326,8 @@ class GpuSum(Op): ...@@ -1326,8 +1326,8 @@ class GpuSum(Op):
for_i3 = "for (int i3 = threadIdx.x; i3 < d3; i3 += blockDim.x)" for_i3 = "for (int i3 = threadIdx.x; i3 < d3; i3 += blockDim.x)"
reducebuf = self._k_reduce_buf('Z[i0 * sZ0]') reducebuf = self._k_reduce_buf('Z[i0 * sZ0]')
param_dim = ",".join(["const int d%(i)s"%locals() for i in range(nd_in)]) param_dim = ",".join(["const int d%(i)s"%locals() for i in xrange(nd_in)])
param_strides = ",".join(["const int sA%(i)s"%locals() for i in range(nd_in)]) param_strides = ",".join(["const int sA%(i)s"%locals() for i in xrange(nd_in)])
decl = self._k_decl(node,nodename) decl = self._k_decl(node,nodename)
init = self._k_init(node,nodename) init = self._k_init(node,nodename)
print >> sio, """ print >> sio, """
...@@ -1988,7 +1988,7 @@ class GpuContiguous(Op): ...@@ -1988,7 +1988,7 @@ class GpuContiguous(Op):
Py_INCREF(%(z)s); Py_INCREF(%(z)s);
} else if ((NULL == %(z)s)"""%locals() } else if ((NULL == %(z)s)"""%locals()
for i in range(len(node.inputs[0].type.broadcastable)): for i in xrange(len(node.inputs[0].type.broadcastable)):
str += "\n|| (CudaNdarray_HOST_DIMS(%(input)s)[%(i)s] != CudaNdarray_HOST_DIMS(%(z)s)[%(i)s])"%locals() str += "\n|| (CudaNdarray_HOST_DIMS(%(input)s)[%(i)s] != CudaNdarray_HOST_DIMS(%(z)s)[%(i)s])"%locals()
str += """) str += """)
{ {
......
...@@ -552,9 +552,9 @@ class NaiveAlgo(object): ...@@ -552,9 +552,9 @@ class NaiveAlgo(object):
print >> sio, 'std::cerr << "\\n";' print >> sio, 'std::cerr << "\\n";'
for ipos in xrange(len(node.inputs)): for ipos in xrange(len(node.inputs)):
print >> sio, 'std::cerr << " local_str inputs %(ipos)s: " <<'%locals()+' << " " << '.join(["local_str[%(ipos)s][%(x)s]"%locals() for x in range(nd)])+'<<"\\n";' print >> sio, 'std::cerr << " local_str inputs %(ipos)s: " <<'%locals()+' << " " << '.join(["local_str[%(ipos)s][%(x)s]"%locals() for x in xrange(nd)])+'<<"\\n";'
for ipos in xrange(len(node.outputs)): for ipos in xrange(len(node.outputs)):
print >> sio, 'std::cerr << " local_ostr inputs %(ipos)s: " <<'%locals()+' << " " << '.join(["local_ostr[%(ipos)s][%(x)s]"%locals() for x in range(nd)])+'<<"\\n";' print >> sio, 'std::cerr << " local_ostr inputs %(ipos)s: " <<'%locals()+' << " " << '.join(["local_ostr[%(ipos)s][%(x)s]"%locals() for x in xrange(nd)])+'<<"\\n";'
print >> sio, """ print >> sio, """
for(int id=0;id<nd_collapse;id++){ for(int id=0;id<nd_collapse;id++){
...@@ -593,17 +593,17 @@ class NaiveAlgo(object): ...@@ -593,17 +593,17 @@ class NaiveAlgo(object):
print >> sio, 'std::cerr << "\\n";' print >> sio, 'std::cerr << "\\n";'
for ipos in xrange(len(node.inputs)): for ipos in xrange(len(node.inputs)):
print >> sio, 'std::cerr << " local_str %(ipos)s: " <<'%locals()+' << " " << '.join(["local_str[%(ipos)s][%(x)s]"%locals() for x in range(nd)])+'<<"\\n";' print >> sio, 'std::cerr << " local_str %(ipos)s: " <<'%locals()+' << " " << '.join(["local_str[%(ipos)s][%(x)s]"%locals() for x in xrange(nd)])+'<<"\\n";'
for ipos in xrange(len(node.outputs)): for ipos in xrange(len(node.outputs)):
print >> sio, 'std::cerr << " local_ostr %(ipos)s: " <<'%locals()+' << " " << '.join(["local_ostr[%(ipos)s][%(x)s]"%locals() for x in range(nd)])+'<<"\\n";' print >> sio, 'std::cerr << " local_ostr %(ipos)s: " <<'%locals()+' << " " << '.join(["local_ostr[%(ipos)s][%(x)s]"%locals() for x in xrange(nd)])+'<<"\\n";'
# collapse contiguous dimensions (ignoring scalars, generic version(collapse any dimensions, right, left, middle)) # collapse contiguous dimensions (ignoring scalars, generic version(collapse any dimensions, right, left, middle))
# this is a good idea because we make less index calculation in the gpu. # this is a good idea because we make less index calculation in the gpu.
print >> sio, "int nd_collapse_[%(nd)s] = {"%locals() +','.join(['1' for x in range(nd)]) +"};" print >> sio, "int nd_collapse_[%(nd)s] = {"%locals() +','.join(['1' for x in xrange(nd)]) +"};"
for ipos in xrange(len(node.inputs)): for ipos in xrange(len(node.inputs)):
if not _logical_scalar(node.inputs[ipos]): if not _logical_scalar(node.inputs[ipos]):
print >> sio, """ print >> sio, """
int nd_collapse_%(ipos)s[%(nd)s] = {"""%locals() +','.join(['1' for x in range(nd)]) +"};" int nd_collapse_%(ipos)s[%(nd)s] = {"""%locals() +','.join(['1' for x in xrange(nd)]) +"};"
print >> sio, """ print >> sio, """
can_collapse_%(nodename)s(nd_collapse, local_dims, local_str[%(ipos)s], nd_collapse_%(ipos)s); can_collapse_%(nodename)s(nd_collapse, local_dims, local_str[%(ipos)s], nd_collapse_%(ipos)s);
for(int i=0;i<nd_collapse;i++){ for(int i=0;i<nd_collapse;i++){
...@@ -615,12 +615,12 @@ nd_collapse_[i]=0; ...@@ -615,12 +615,12 @@ nd_collapse_[i]=0;
print >>sio, """ print >>sio, """
std::cerr<< "nd_collapse_%(ipos)s "<< std::cerr<< "nd_collapse_%(ipos)s "<<
"""%locals() """%locals()
print >>sio, ' << " " << '.join(["nd_collapse_%(ipos)s["%locals()+str(i)+"]" for i in range(nd)]) print >>sio, ' << " " << '.join(["nd_collapse_%(ipos)s["%locals()+str(i)+"]" for i in xrange(nd)])
print >>sio, '<< "\\n";' print >>sio, '<< "\\n";'
print >>sio, """ print >>sio, """
std::cerr<< "nd_collapse_ "<< std::cerr<< "nd_collapse_ "<<
"""%locals() """%locals()
print >>sio, ' << " " << '.join(["nd_collapse_["%locals()+str(i)+"]" for i in range(nd)]) print >>sio, ' << " " << '.join(["nd_collapse_["%locals()+str(i)+"]" for i in xrange(nd)])
print >>sio, '<< "\\n";' print >>sio, '<< "\\n";'
# update the local stride. # update the local stride.
...@@ -664,8 +664,8 @@ nd_collapse_[i]=0; ...@@ -664,8 +664,8 @@ nd_collapse_[i]=0;
if(nd_collapse_[i]==1)nd_collapse--; if(nd_collapse_[i]==1)nd_collapse--;
} }
if(nd_collapse == 1 """%locals() if(nd_collapse == 1 """%locals()
l=["local_str[%(ipos)s][nd_collapse-1]==1 "%locals()for ipos in range(len(node.inputs)) if not _logical_scalar(node.inputs[ipos])] l=["local_str[%(ipos)s][nd_collapse-1]==1 "%locals()for ipos in xrange(len(node.inputs)) if not _logical_scalar(node.inputs[ipos])]
l+=["local_ostr[%(ipos)s][nd_collapse-1]==1 "%locals()for ipos in range(len(node.outputs)) if not _logical_scalar(node.outputs[ipos])] l+=["local_ostr[%(ipos)s][nd_collapse-1]==1 "%locals()for ipos in xrange(len(node.outputs)) if not _logical_scalar(node.outputs[ipos])]
if len(l)>0: if len(l)>0:
print >> sio," && "," && ".join(l) print >> sio," && "," && ".join(l)
print >> sio,"""){nd_collapse=0;} """ print >> sio,"""){nd_collapse=0;} """
...@@ -679,9 +679,9 @@ nd_collapse_[i]=0; ...@@ -679,9 +679,9 @@ nd_collapse_[i]=0;
print >> sio, 'std::cerr << "\\n";' print >> sio, 'std::cerr << "\\n";'
for ipos in xrange(len(node.inputs)): for ipos in xrange(len(node.inputs)):
print >> sio, 'std::cerr << " local_str %(ipos)s: " <<'%locals()+' << " " << '.join(["local_str[%(ipos)s][%(x)s]"%locals() for x in range(nd)])+'<<"\\n";' print >> sio, 'std::cerr << " local_str %(ipos)s: " <<'%locals()+' << " " << '.join(["local_str[%(ipos)s][%(x)s]"%locals() for x in xrange(nd)])+'<<"\\n";'
for ipos in xrange(len(node.outputs)): for ipos in xrange(len(node.outputs)):
print >> sio, 'std::cerr << " local_ostr %(ipos)s: " <<'%locals()+' << " " << '.join(["local_ostr[%(ipos)s][%(x)s]"%locals() for x in range(nd)])+'<<"\\n";' print >> sio, 'std::cerr << " local_ostr %(ipos)s: " <<'%locals()+' << " " << '.join(["local_ostr[%(ipos)s][%(x)s]"%locals() for x in xrange(nd)])+'<<"\\n";'
def launch_Ccontiguous(nodename, scalar_op, sync=True): def launch_Ccontiguous(nodename, scalar_op, sync=True):
...@@ -784,7 +784,7 @@ nd_collapse_[i]=0; ...@@ -784,7 +784,7 @@ nd_collapse_[i]=0;
print >> sio, "case 0: {" print >> sio, "case 0: {"
launch_Ccontiguous(nodename, scalar_op, self.sync) launch_Ccontiguous(nodename, scalar_op, self.sync)
print >> sio, " } break;" print >> sio, " } break;"
for i in range(1, nd+1): for i in xrange(1, nd+1):
print >> sio, "case "+str(i)+": {" print >> sio, "case "+str(i)+": {"
launch_General(nodename, scalar_op, i, self.sync) launch_General(nodename, scalar_op, i, self.sync)
print >> sio, " } break;" print >> sio, " } break;"
...@@ -800,7 +800,7 @@ nd_collapse_[i]=0; ...@@ -800,7 +800,7 @@ nd_collapse_[i]=0;
def c_support_code_apply(self, node, nodename): def c_support_code_apply(self, node, nodename):
nd = node.outputs[0].type.ndim nd = node.outputs[0].type.ndim
return "".join( return "".join(
[self.c_src_kernel(node, nodename,x) for x in range(1,nd+1)]+ [self.c_src_kernel(node, nodename,x) for x in xrange(1,nd+1)]+
[ [
self.c_src_kernel_Ccontiguous(node, nodename), self.c_src_kernel_Ccontiguous(node, nodename),
self.c_src_callkernel(node, nodename), self.c_src_callkernel(node, nodename),
......
...@@ -478,7 +478,7 @@ def local_gpu_sum(node): ...@@ -478,7 +478,7 @@ def local_gpu_sum(node):
new_in_shp = [x_shape[0]] new_in_shp = [x_shape[0]]
new_mask = [reduce_mask[0]] new_mask = [reduce_mask[0]]
for i in range(1, x.type.ndim): for i in xrange(1, x.type.ndim):
if reduce_mask[i] == reduce_mask[i-1]: if reduce_mask[i] == reduce_mask[i-1]:
new_in_shp[-1] *= x_shape[i] new_in_shp[-1] *= x_shape[i]
else: else:
...@@ -993,7 +993,7 @@ def split_huge_add_or_mul(node): ...@@ -993,7 +993,7 @@ def split_huge_add_or_mul(node):
return False return False
while len(node.inputs)>max_nb_inputs: while len(node.inputs)>max_nb_inputs:
inner_op = [] inner_op = []
for i in range(0,len(node.inputs),max_nb_inputs): for i in xrange(0,len(node.inputs),max_nb_inputs):
inner_op.append(node.op(*node.inputs[i:i+max_nb_inputs])) inner_op.append(node.op(*node.inputs[i:i+max_nb_inputs]))
node = node.op(*inner_op).owner node = node.op(*inner_op).owner
return node return node
......
...@@ -76,9 +76,9 @@ class NeighbourhoodsFromImages(Op): ...@@ -76,9 +76,9 @@ class NeighbourhoodsFromImages(Op):
self.code_string, self.code = self.make_py_code() self.code_string, self.code = self.make_py_code()
def _compute_neigh_strides(self): def _compute_neigh_strides(self):
neigh_strides = [1 for i in range(len(self.strides))] neigh_strides = [1 for i in xrange(len(self.strides))]
cur_stride = 1 cur_stride = 1
for i in range(len(self.strides)-1, -1, -1): for i in xrange(len(self.strides)-1, -1, -1):
neigh_strides[i] = cur_stride neigh_strides[i] = cur_stride
cur_stride *= self.dims_neighbourhoods[i] cur_stride *= self.dims_neighbourhoods[i]
return neigh_strides return neigh_strides
...@@ -107,7 +107,7 @@ class NeighbourhoodsFromImages(Op): ...@@ -107,7 +107,7 @@ class NeighbourhoodsFromImages(Op):
def out_shape(self, input_shape): def out_shape(self, input_shape):
dims = list(input_shape[:self.n_dims_before]) dims = list(input_shape[:self.n_dims_before])
num_strides = [0 for i in range(len(self.strides))] num_strides = [0 for i in xrange(len(self.strides))]
neigh_flattened_dim = 1 neigh_flattened_dim = 1
for i, ds in enumerate(self.dims_neighbourhoods): for i, ds in enumerate(self.dims_neighbourhoods):
cur_stride = self.strides[i] cur_stride = self.strides[i]
...@@ -238,13 +238,13 @@ class NeighbourhoodsFromImages(Op): ...@@ -238,13 +238,13 @@ class NeighbourhoodsFromImages(Op):
def _py_flattened_idx(self): def _py_flattened_idx(self):
return "+".join(["neigh_strides[%d]*neigh_idx_%d" % (i,i) \ return "+".join(["neigh_strides[%d]*neigh_idx_%d" % (i,i) \
for i in range(len(self.strides))]) for i in xrange(len(self.strides))])
def _py_assignment(self): def _py_assignment(self):
input_idx = "".join(["outer_idx_%d," % (i,) \ input_idx = "".join(["outer_idx_%d," % (i,) \
for i in xrange(self.n_dims_before)]) for i in xrange(self.n_dims_before)])
input_idx += "".join(["dim_%d_offset+neigh_idx_%d," % \ input_idx += "".join(["dim_%d_offset+neigh_idx_%d," % \
(i,i) for i in range(len(self.strides))]) (i,i) for i in xrange(len(self.strides))])
out_idx = "".join(\ out_idx = "".join(\
["outer_idx_%d," % (i,) for i in \ ["outer_idx_%d," % (i,) for i in \
range(self.n_dims_before)] + \ range(self.n_dims_before)] + \
......
...@@ -1958,9 +1958,9 @@ class Composite(ScalarOp): ...@@ -1958,9 +1958,9 @@ class Composite(ScalarOp):
raise ValueError("The env to Composite must be exclusively composed of ScalarOp instances.") raise ValueError("The env to Composite must be exclusively composed of ScalarOp instances.")
subd = dict(zip(inputs, subd = dict(zip(inputs,
["%%(i%i)s"%i for i in range(len(inputs))]) + ["%%(i%i)s"%i for i in xrange(len(inputs))]) +
zip(outputs, zip(outputs,
["%%(o%i)s"%i for i in range(len(outputs))])) ["%%(o%i)s"%i for i in xrange(len(outputs))]))
for orphan in env.variables: #env.orphans: for orphan in env.variables: #env.orphans:
if orphan.owner is None and orphan not in env.inputs: if orphan.owner is None and orphan not in env.inputs:
...@@ -2048,9 +2048,9 @@ class Composite(ScalarOp): ...@@ -2048,9 +2048,9 @@ class Composite(ScalarOp):
raise NotImplementedError("grad is not implemented for Composite") raise NotImplementedError("grad is not implemented for Composite")
def c_code(self, node, name, inames, onames, sub): def c_code(self, node, name, inames, onames, sub):
d = dict(zip(["i%i"%i for i in range(len(inames))], d = dict(zip(["i%i"%i for i in xrange(len(inames))],
inames) + inames) +
zip(["o%i"%i for i in range(len(onames))], zip(["o%i"%i for i in xrange(len(onames))],
onames), onames),
**sub) **sub)
d['name'] = name d['name'] = name
......
...@@ -382,8 +382,8 @@ class ConvolutionIndices(Op): ...@@ -382,8 +382,8 @@ class ConvolutionIndices(Op):
# of the column values (order in which you write the values determines how the # of the column values (order in which you write the values determines how the
# vectorized data will get used later one) # vectorized data will get used later one)
for fmapi in range(inshp[0]): # loop over input features for fmapi in xrange(inshp[0]): # loop over input features
for n in range(nkern): # loop over number of kernels (nkern=1 for weight sharing) for n in xrange(nkern): # loop over number of kernels (nkern=1 for weight sharing)
# FOR EACH OUTPUT PIXEL... # FOR EACH OUTPUT PIXEL...
for oy in N.arange(lbound[0],ubound[0],dy): # loop over output image height for oy in N.arange(lbound[0],ubound[0],dy): # loop over output image height
......
...@@ -1815,7 +1815,7 @@ class SpecifyShape(Op): ...@@ -1815,7 +1815,7 @@ class SpecifyShape(Op):
def infer_shape(self, node, shapes): def infer_shape(self, node, shapes):
xshape, sshape = shapes xshape, sshape = shapes
new_shape=[] new_shape=[]
for dim in range(node.inputs[0].ndim): for dim in xrange(node.inputs[0].ndim):
try: try:
s=get_constant_value(node.inputs[1][dim]) s=get_constant_value(node.inputs[1][dim])
s=as_tensor_variable(s) s=as_tensor_variable(s)
...@@ -2677,7 +2677,7 @@ def var(input, axis = None): ...@@ -2677,7 +2677,7 @@ def var(input, axis = None):
#make a pattern that will undo the reduction of dimensions caused by mean #make a pattern that will undo the reduction of dimensions caused by mean
pattern = [] pattern = []
next_dim = 0 next_dim = 0
for i in range(input_ndim): for i in xrange(input_ndim):
if i in axis: if i in axis:
pattern.append('x') pattern.append('x')
else: else:
...@@ -3928,7 +3928,7 @@ class Rebroadcast(Op): ...@@ -3928,7 +3928,7 @@ class Rebroadcast(Op):
if len(self.axis) == 0: if len(self.axis) == 0:
broadcast_pattern = [] broadcast_pattern = []
else: else:
broadcast_pattern = ['?' for i in range(1+numpy.max(self.axis.keys()))] broadcast_pattern = ['?' for i in xrange(1+numpy.max(self.axis.keys()))]
for k,v in self.axis.iteritems(): for k,v in self.axis.iteritems():
broadcast_pattern[k] = str(int(v)) broadcast_pattern[k] = str(int(v))
return '%s{%s}' % (self.__class__.__name__, ','.join(broadcast_pattern)) return '%s{%s}' % (self.__class__.__name__, ','.join(broadcast_pattern))
...@@ -3955,7 +3955,7 @@ class Rebroadcast(Op): ...@@ -3955,7 +3955,7 @@ class Rebroadcast(Op):
assert len(ishapes)==1 assert len(ishapes)==1
l = [] l = []
one = constant(1) one = constant(1)
for ax in range(len(ishapes[0])): for ax in xrange(len(ishapes[0])):
if self.axis.get(ax, False): if self.axis.get(ax, False):
l.append(one) l.append(one)
else: else:
...@@ -3994,7 +3994,7 @@ def patternbroadcast(x, broadcastable): ...@@ -3994,7 +3994,7 @@ def patternbroadcast(x, broadcastable):
We apply the opt here not to pollute the graph especially during the gpu optimization We apply the opt here not to pollute the graph especially during the gpu optimization
""" """
rval = Rebroadcast(*[(i,broadcastable[i]) for i in range(len(broadcastable))])(x) rval = Rebroadcast(*[(i,broadcastable[i]) for i in xrange(len(broadcastable))])(x)
return theano.tensor.opt.apply_rebroadcast_opt(rval) return theano.tensor.opt.apply_rebroadcast_opt(rval)
class Join(Op): class Join(Op):
...@@ -4158,7 +4158,7 @@ class Join(Op): ...@@ -4158,7 +4158,7 @@ class Join(Op):
# 'axis' dimension. # 'axis' dimension.
return [gz[[slice(None)] * axis + [slice(idx[k], idx[k + 1])] + \ return [gz[[slice(None)] * axis + [slice(idx[k], idx[k + 1])] + \
[slice(None)] * (n_dims - axis - 1)] \ [slice(None)] * (n_dims - axis - 1)] \
for k in range(len(sizes_along_axis))] for k in xrange(len(sizes_along_axis))]
def vec_length(self, node): def vec_length(self, node):
"""Guess the length of a Join Variable""" """Guess the length of a Join Variable"""
...@@ -4235,7 +4235,7 @@ def shape_padleft(t, n_ones=1): ...@@ -4235,7 +4235,7 @@ def shape_padleft(t, n_ones=1):
""" """
_t = as_tensor_variable(t) _t = as_tensor_variable(t)
pattern = ['x']*n_ones + [i for i in range(_t.type.ndim)] pattern = ['x']*n_ones + [i for i in xrange(_t.type.ndim)]
return DimShuffle(_t.broadcastable, pattern)(_t) return DimShuffle(_t.broadcastable, pattern)(_t)
@constructor @constructor
...@@ -4246,7 +4246,7 @@ def shape_padright(t, n_ones=1): ...@@ -4246,7 +4246,7 @@ def shape_padright(t, n_ones=1):
""" """
_t = as_tensor_variable(t) _t = as_tensor_variable(t)
pattern = [i for i in range(_t.type.ndim)] + ['x']*n_ones pattern = [i for i in xrange(_t.type.ndim)] + ['x']*n_ones
return DimShuffle(_t.broadcastable, pattern)(_t) return DimShuffle(_t.broadcastable, pattern)(_t)
@constructor @constructor
...@@ -4382,7 +4382,7 @@ if 0: #vertical and horizontal stacking are deprecated. Better to use stack() a ...@@ -4382,7 +4382,7 @@ if 0: #vertical and horizontal stacking are deprecated. Better to use stack() a
out, = out_ out, = out_
assert x.ndim == y.ndim assert x.ndim == y.ndim
# Make sure every dimension (save the first) is the same # Make sure every dimension (save the first) is the same
for i in range(x.ndim): assert i == 0 or x.shape[i] == y.shape[i] for i in xrange(x.ndim): assert i == 0 or x.shape[i] == y.shape[i]
out[0] = numpy.vstack([x, y]) out[0] = numpy.vstack([x, y])
def grad(self, inp, grads): def grad(self, inp, grads):
""" """
...@@ -4390,7 +4390,7 @@ if 0: #vertical and horizontal stacking are deprecated. Better to use stack() a ...@@ -4390,7 +4390,7 @@ if 0: #vertical and horizontal stacking are deprecated. Better to use stack() a
that way we can do more sanity-checking:: that way we can do more sanity-checking::
assert x.ndim == y.ndim assert x.ndim == y.ndim
# Make sure every dimension (save the first) is the same # Make sure every dimension (save the first) is the same
for i in range(x.data.ndim): assert i == 0 or x.data.shape[i] == y.shape[i] for i in xrange(x.data.ndim): assert i == 0 or x.data.shape[i] == y.shape[i]
etc... etc...
""" """
x, y = inp x, y = inp
...@@ -4482,13 +4482,13 @@ class Reshape(Op): ...@@ -4482,13 +4482,13 @@ class Reshape(Op):
#return [tuple([switch(eq(node.inputs[1][i], -1), #return [tuple([switch(eq(node.inputs[1][i], -1),
# theano.tensor.opt.Shape_i(i)(node.outputs[0]), # theano.tensor.opt.Shape_i(i)(node.outputs[0]),
# node.inputs[1][i]) # node.inputs[1][i])
# for i in range(self.ndim)] # for i in xrange(self.ndim)]
# )] # )]
# Here, we only simplify if the shape (node.inputs[1]) is a constant, # Here, we only simplify if the shape (node.inputs[1]) is a constant,
# ideally it would suffice to check that it is always non-negative. # ideally it would suffice to check that it is always non-negative.
oshape = [] oshape = []
for i in range(self.ndim): for i in xrange(self.ndim):
default_os_i = theano.tensor.opt.Shape_i(i)(node.outputs[0]) default_os_i = theano.tensor.opt.Shape_i(i)(node.outputs[0])
try: try:
os_i = get_constant_value(node.inputs[1][i]).item() os_i = get_constant_value(node.inputs[1][i]).item()
...@@ -4801,15 +4801,15 @@ class PermuteRowElements(Op): ...@@ -4801,15 +4801,15 @@ class PermuteRowElements(Op):
xs0 = x.shape[0] xs0 = x.shape[0]
ys0 = y.shape[0] ys0 = y.shape[0]
if xs0 == ys0: if xs0 == ys0:
for i in range(xs0): for i in xrange(xs0):
self._rec_perform(node, x[i], y[i], inverse, out[i], curdim+1) self._rec_perform(node, x[i], y[i], inverse, out[i], curdim+1)
elif ys0 == 1 and node.inputs[1].type.broadcastable[curdim]: elif ys0 == 1 and node.inputs[1].type.broadcastable[curdim]:
# Broadcast y # Broadcast y
for i in range(xs0): for i in xrange(xs0):
self._rec_perform(node, x[i], y[0], inverse, out[i], curdim+1) self._rec_perform(node, x[i], y[0], inverse, out[i], curdim+1)
elif xs0 == 1 and node.inputs[0].type.broadcastable[curdim]: elif xs0 == 1 and node.inputs[0].type.broadcastable[curdim]:
# Broadcast x # Broadcast x
for i in range(ys0): for i in xrange(ys0):
self._rec_perform(node, x[0], y[i], inverse, out[i], curdim+1) self._rec_perform(node, x[0], y[i], inverse, out[i], curdim+1)
else: else:
raise ValueError('Dimension mismatch: %s, %s' % (xs0, ys0)) raise ValueError('Dimension mismatch: %s, %s' % (xs0, ys0))
...@@ -4850,7 +4850,7 @@ class PermuteRowElements(Op): ...@@ -4850,7 +4850,7 @@ class PermuteRowElements(Op):
# If x has been broadcasted along some axes, we need to sum # If x has been broadcasted along some axes, we need to sum
# the gradient over these axes, but keep the dimension (as # the gradient over these axes, but keep the dimension (as
# broadcastable) # broadcastable)
broadcasted_dims = [dim for dim in range(gz.type.ndim)\ broadcasted_dims = [dim for dim in xrange(gz.type.ndim)\
if x.type.broadcastable[dim] and not gz.type.broadcastable[dim]] if x.type.broadcastable[dim] and not gz.type.broadcastable[dim]]
gx = Sum(axis = broadcasted_dims)(gx) gx = Sum(axis = broadcasted_dims)(gx)
...@@ -4858,7 +4858,7 @@ class PermuteRowElements(Op): ...@@ -4858,7 +4858,7 @@ class PermuteRowElements(Op):
# so we need to put them back. # so we need to put them back.
newdims = [] newdims = []
i = 0 i = 0
for dim in range(gz.type.ndim): for dim in xrange(gz.type.ndim):
if dim in broadcasted_dims: if dim in broadcasted_dims:
newdims.append('x') newdims.append('x')
else: else:
...@@ -5348,13 +5348,13 @@ class TensorDotGrad(Op): ...@@ -5348,13 +5348,13 @@ class TensorDotGrad(Op):
_gx = numpy.tensordot(gz, y, [range(x.ndim-len(self.axes[0]),gz.ndim), sum_over_y]) _gx = numpy.tensordot(gz, y, [range(x.ndim-len(self.axes[0]),gz.ndim), sum_over_y])
idx = numpy.hstack((sum_over_x, self.axes[0])) idx = numpy.hstack((sum_over_x, self.axes[0]))
newshapex = numpy.zeros(x.ndim) newshapex = numpy.zeros(x.ndim)
newshapex[[newpos for newpos in idx]] = [i for i in range(x.ndim)] newshapex[[newpos for newpos in idx]] = [i for i in xrange(x.ndim)]
gx[0] = numpy.transpose(_gx, newshapex) gx[0] = numpy.transpose(_gx, newshapex)
_gy = numpy.tensordot(x, gz, [sum_over_x, range(x.ndim-len(self.axes[0]))]) _gy = numpy.tensordot(x, gz, [sum_over_x, range(x.ndim-len(self.axes[0]))])
idy = numpy.hstack((self.axes[1], sum_over_y)) idy = numpy.hstack((self.axes[1], sum_over_y))
newshapey = numpy.zeros(y.ndim) newshapey = numpy.zeros(y.ndim)
newshapey[[newpos for newpos in idy]] = [i for i in range(y.ndim)] newshapey[[newpos for newpos in idy]] = [i for i in xrange(y.ndim)]
gy[0] = numpy.transpose(_gy, newshapey) gy[0] = numpy.transpose(_gy, newshapey)
tensordot_grad = TensorDotGrad tensordot_grad = TensorDotGrad
......
...@@ -1603,7 +1603,7 @@ def local_dot22_to_dot22scalar(node): ...@@ -1603,7 +1603,7 @@ def local_dot22_to_dot22scalar(node):
# I'm asserting there are no such inputs here: # I'm asserting there are no such inputs here:
assert dot22_idx != mul_idx assert dot22_idx != mul_idx
assert all((i in (dot22_idx, mul_idx)) assert all((i in (dot22_idx, mul_idx))
for i in range(len(node.inputs))) for i in xrange(len(node.inputs)))
return [T.mul(m.owner.inputs[1-i],dot)] return [T.mul(m.owner.inputs[1-i],dot)]
elif m.owner and m.owner.op == T.mul: elif m.owner and m.owner.op == T.mul:
......
...@@ -315,7 +315,7 @@ def make_reordered_loop(init_loop_orders, olv_index, dtypes, inner_task, sub): ...@@ -315,7 +315,7 @@ def make_reordered_loop(init_loop_orders, olv_index, dtypes, inner_task, sub):
%(ovar)s_loops_it = %(ovar)s_loops.begin(); %(ovar)s_loops_it = %(ovar)s_loops.begin();
""" % locals() """ % locals()
for i in range(nnested): for i in xrange(nnested):
declare_totals += """ declare_totals += """
int TOTAL_%(i)i = init_totals[%(ovar)s_loops_it->second]; int TOTAL_%(i)i = init_totals[%(ovar)s_loops_it->second];
++%(ovar)s_loops_it; ++%(ovar)s_loops_it;
...@@ -357,7 +357,7 @@ def make_reordered_loop(init_loop_orders, olv_index, dtypes, inner_task, sub): ...@@ -357,7 +357,7 @@ def make_reordered_loop(init_loop_orders, olv_index, dtypes, inner_task, sub):
std::vector< std::pair<int, int> >::reverse_iterator %(ovar)s_loops_rit; std::vector< std::pair<int, int> >::reverse_iterator %(ovar)s_loops_rit;
""" % locals() """ % locals()
for i in range(nvars): for i in xrange(nvars):
var = sub["lv%i" % i] var = sub["lv%i" % i]
declare_strides_jumps += """ declare_strides_jumps += """
%(ovar)s_loops_rit = %(ovar)s_loops.rbegin();""" % locals() %(ovar)s_loops_rit = %(ovar)s_loops.rbegin();""" % locals()
...@@ -382,7 +382,7 @@ def make_reordered_loop(init_loop_orders, olv_index, dtypes, inner_task, sub): ...@@ -382,7 +382,7 @@ def make_reordered_loop(init_loop_orders, olv_index, dtypes, inner_task, sub):
iterv = 'ITER_%i' % i iterv = 'ITER_%i' % i
total = 'TOTAL_%i' % i total = 'TOTAL_%i' % i
update = '' update = ''
for j in range(nvars): for j in xrange(nvars):
var = sub["lv%i" % j] var = sub["lv%i" % j]
update += "%(var)s_iter += %(var)s_jump_l%(i)i;\n" % locals() update += "%(var)s_iter += %(var)s_jump_l%(i)i;\n" % locals()
......
...@@ -73,14 +73,14 @@ def conv2d(input, filters, image_shape=None, filter_shape=None, ...@@ -73,14 +73,14 @@ def conv2d(input, filters, image_shape=None, filter_shape=None,
#accept Constant value for image_shape and filter_shape. #accept Constant value for image_shape and filter_shape.
if image_shape is not None: if image_shape is not None:
image_shape = list(image_shape) image_shape = list(image_shape)
for i in range(len(image_shape)): for i in xrange(len(image_shape)):
if image_shape[i] is not None: if image_shape[i] is not None:
image_shape[i] = get_constant_value(as_tensor_variable(image_shape[i])) image_shape[i] = get_constant_value(as_tensor_variable(image_shape[i]))
assert str(image_shape[i].dtype).startswith('int') assert str(image_shape[i].dtype).startswith('int')
image_shape[i] = int(image_shape[i]) image_shape[i] = int(image_shape[i])
if filter_shape is not None: if filter_shape is not None:
filter_shape = list(filter_shape) filter_shape = list(filter_shape)
for i in range(len(filter_shape)): for i in xrange(len(filter_shape)):
if filter_shape[i] is not None: if filter_shape[i] is not None:
filter_shape[i] = get_constant_value(as_tensor_variable(filter_shape[i])) filter_shape[i] = get_constant_value(as_tensor_variable(filter_shape[i]))
assert str(filter_shape[i].dtype).startswith('int') assert str(filter_shape[i].dtype).startswith('int')
...@@ -450,7 +450,7 @@ class ConvOp(Op): ...@@ -450,7 +450,7 @@ class ConvOp(Op):
else: else:
time_unroll_patch = self.speed_unroll_patch_noshape[mode_idx] time_unroll_patch = self.speed_unroll_patch_noshape[mode_idx]
time_unroll_batch_kern = 9999999 time_unroll_batch_kern = 9999999
for i in range(len(self.speed_unroll_batch_kern)): for i in xrange(len(self.speed_unroll_batch_kern)):
if bsize%self.speed_unroll_batch_kern[i][0]==0 and nkern%self.speed_unroll_batch_kern[i][1]==0: if bsize%self.speed_unroll_batch_kern[i][0]==0 and nkern%self.speed_unroll_batch_kern[i][1]==0:
if self.speed_unroll_batch_kern[i][2+mode_idx]<time_unroll_batch_kern: if self.speed_unroll_batch_kern[i][2+mode_idx]<time_unroll_batch_kern:
time_unroll_batch_kern=self.speed_unroll_batch_kern[i][2+mode_idx] time_unroll_batch_kern=self.speed_unroll_batch_kern[i][2+mode_idx]
...@@ -506,9 +506,9 @@ class ConvOp(Op): ...@@ -506,9 +506,9 @@ class ConvOp(Op):
else: #full mode not implemented else: #full mode not implemented
self.flops=0 self.flops=0
for out_row in range(self.outshp[0]):#loop over output row for out_row in xrange(self.outshp[0]):#loop over output row
for out_col in range(self.outshp[0]):#loop over output col for out_col in xrange(self.outshp[0]):#loop over output col
for row in range(self.kshp[0]):#loop over kern row for row in xrange(self.kshp[0]):#loop over kern row
if (row+out_row-self.kshp[0]+1<0 or if (row+out_row-self.kshp[0]+1<0 or
row+out_row-self.kshp[0]+1>=self.imshp[1]): row+out_row-self.kshp[0]+1>=self.imshp[1]):
...@@ -664,10 +664,10 @@ class ConvOp(Op): ...@@ -664,10 +664,10 @@ class ConvOp(Op):
val = _valfrommode(self.out_mode) val = _valfrommode(self.out_mode)
bval = _bvalfromboundary('fill') bval = _bvalfromboundary('fill')
for b in range(bsize): for b in xrange(bsize):
for n in range(nkern): for n in xrange(nkern):
zz[b,n,...].fill(0) zz[b,n,...].fill(0)
for im0 in range(stacklen): for im0 in xrange(stacklen):
zz[b,n,...] += _convolve2d(\ zz[b,n,...] += _convolve2d(\
img2d[b,im0,...], filtersflipped[n,im0,...],1,val, bval, 0) img2d[b,im0,...], filtersflipped[n,im0,...],1,val, bval, 0)
...@@ -681,12 +681,12 @@ class ConvOp(Op): ...@@ -681,12 +681,12 @@ class ConvOp(Op):
img2d = img2d2 img2d = img2d2
#N_image_shape = image_data.shape #N_image_shape = image_data.shape
for b in range(bsize): for b in xrange(bsize):
for n in range(nkern): for n in xrange(nkern):
zz[b,n,...].fill(0) zz[b,n,...].fill(0)
for im0 in range(stacklen): for im0 in xrange(stacklen):
for row in range(0,zz.shape[2],self.dx): for row in xrange(0,zz.shape[2],self.dx):
for col in range(0,zz.shape[3],self.dy): for col in xrange(0,zz.shape[3],self.dy):
zz[b,n,row,col] += (img2d[b,im0,row:row+kshp[0],col:col+kshp[1]]*\ zz[b,n,row,col] += (img2d[b,im0,row:row+kshp[0],col:col+kshp[1]]*\
filtersflipped[n,im0,::-1,::-1]).sum() filtersflipped[n,im0,::-1,::-1]).sum()
...@@ -1555,16 +1555,16 @@ def gen_conv_code_unroll_batch_kern(d,unroll_bsize=1, unroll_ksize=1): ...@@ -1555,16 +1555,16 @@ def gen_conv_code_unroll_batch_kern(d,unroll_bsize=1, unroll_ksize=1):
d["unroll_ksize"]=unroll_ksize d["unroll_ksize"]=unroll_ksize
def my_dup(st,size): def my_dup(st,size):
s="" s=""
for i in range(size): for i in xrange(size):
d["unroll_iter"]=i d["unroll_iter"]=i
s+=st%d s+=st%d
return s+"\n" return s+"\n"
def my_dup2(st): def my_dup2(st):
s="" s=""
iter=0 iter=0
for i in range(unroll_bsize): for i in xrange(unroll_bsize):
d["unroll_biter"]=i d["unroll_biter"]=i
for j in range(unroll_ksize): for j in xrange(unroll_ksize):
d["unroll_kiter"]=j d["unroll_kiter"]=j
d["unroll_iter"]=iter d["unroll_iter"]=iter
iter+=1 iter+=1
......
...@@ -1043,7 +1043,7 @@ class Assert(T.Op): ...@@ -1043,7 +1043,7 @@ class Assert(T.Op):
out = onames[0] out = onames[0]
check = [] check = []
fail = sub['fail'] fail = sub['fail']
for idx in range(len(inames)-1): for idx in xrange(len(inames)-1):
i=inames[idx+1] i=inames[idx+1]
dtype=node.inputs[idx+1].dtype dtype=node.inputs[idx+1].dtype
check.append('if(!((npy_%(dtype)s*)PyArray_DATA(%(i)s))[0]){PyErr_SetString(PyExc_AssertionError,"Theano Assert failed!");%(fail)s}'%locals()) check.append('if(!((npy_%(dtype)s*)PyArray_DATA(%(i)s))[0]){PyErr_SetString(PyExc_AssertionError,"Theano Assert failed!");%(fail)s}'%locals())
...@@ -1138,7 +1138,7 @@ def local_alloc_elemwise(node): ...@@ -1138,7 +1138,7 @@ def local_alloc_elemwise(node):
assert i.type.ndim == cmp_op.ndim assert i.type.ndim == cmp_op.ndim
if theano.config.experimental.local_alloc_elemwise_assert: if theano.config.experimental.local_alloc_elemwise_assert:
assert_op = assert_(assert_op,*[T.eq(i.shape[idx],cmp_op.shape[idx])\ assert_op = assert_(assert_op,*[T.eq(i.shape[idx],cmp_op.shape[idx])\
for idx in range(i.type.ndim) \ for idx in xrange(i.type.ndim) \
if not i.type.broadcastable[idx]]) if not i.type.broadcastable[idx]])
new.append(i.owner.inputs[0]) new.append(i.owner.inputs[0])
elif i.owner and isinstance(i.owner.op, T.DimShuffle) and i.owner.inputs[0].owner \ elif i.owner and isinstance(i.owner.op, T.DimShuffle) and i.owner.inputs[0].owner \
...@@ -1146,7 +1146,7 @@ def local_alloc_elemwise(node): ...@@ -1146,7 +1146,7 @@ def local_alloc_elemwise(node):
assert i.type.ndim == cmp_op.type.ndim assert i.type.ndim == cmp_op.type.ndim
if theano.config.experimental.local_alloc_elemwise_assert: if theano.config.experimental.local_alloc_elemwise_assert:
assert_op = assert_(assert_op,*[T.eq(i.shape[idx],cmp_op.shape[idx]) for idx \ assert_op = assert_(assert_op,*[T.eq(i.shape[idx],cmp_op.shape[idx]) for idx \
in range(i.type.ndim) if not i.type.broadcastable[idx]]) in xrange(i.type.ndim) if not i.type.broadcastable[idx]])
new.append(i.owner.inputs[0].owner.inputs[0]) new.append(i.owner.inputs[0].owner.inputs[0])
else: new.append(i) else: new.append(i)
new[no_broad_idx]=assert_op new[no_broad_idx]=assert_op
...@@ -1322,7 +1322,7 @@ def local_subtensor_lift(node): ...@@ -1322,7 +1322,7 @@ def local_subtensor_lift(node):
Handles the following unary ops: Handles the following unary ops:
elemwise(x,...)[idx] -> elemwise(x[idx],...) elemwise(x,...)[idx] -> elemwise(x[idx],...)
when x,... are broadcasted scalar or not broadcasted at all when x,... are broadcasted scalar or not broadcasted at all
rebroadcast(x)[idx] => rebroadcast(x[idx]) rebroadcast(x)[idx] => rebroadcast(x[idx])
""" """
if isinstance(node.op, T.Subtensor): if isinstance(node.op, T.Subtensor):
u = node.inputs[0] u = node.inputs[0]
...@@ -1375,7 +1375,7 @@ def local_subtensor_lift(node): ...@@ -1375,7 +1375,7 @@ def local_subtensor_lift(node):
if isinstance(x, slice): if isinstance(x, slice):
new_axis += [(j, u.broadcastable[i])] new_axis += [(j, u.broadcastable[i])]
j += 1 j += 1
# now keep the broadcastable pattern of all # now keep the broadcastable pattern of all
# items not appearing in subtensor list # items not appearing in subtensor list
for i in xrange(len(node.op.idx_list), len(u.broadcastable)): for i in xrange(len(node.op.idx_list), len(u.broadcastable)):
new_axis += [(j,u.broadcastable[i])] new_axis += [(j,u.broadcastable[i])]
...@@ -1897,7 +1897,7 @@ def local_remove_switch_const_cond(node): ...@@ -1897,7 +1897,7 @@ def local_remove_switch_const_cond(node):
out = T.cast(out, node.outputs[0].dtype) out = T.cast(out, node.outputs[0].dtype)
if out.type.broadcastable != node.outputs[0].type.broadcastable: if out.type.broadcastable != node.outputs[0].type.broadcastable:
# We need to copy data to the new dimensions during execution # We need to copy data to the new dimensions during execution
out = T.alloc(out, *[node.outputs[0].shape[i] for i in range(out.ndim)]) out = T.alloc(out, *[node.outputs[0].shape[i] for i in xrange(out.ndim)])
return [out] return [out]
return False return False
...@@ -2723,11 +2723,11 @@ def local_sum_alloc(node): ...@@ -2723,11 +2723,11 @@ def local_sum_alloc(node):
val = get_constant_value(input) val = get_constant_value(input)
assert val.size == 1 assert val.size == 1
val = val.reshape(1)[0] val = val.reshape(1)[0]
to_prod = [shapes[i] for i in range(len(shapes)) if i in node.op.axis] to_prod = [shapes[i] for i in xrange(len(shapes)) if i in node.op.axis]
if to_prod: if to_prod:
val *= T.mul(*to_prod) val *= T.mul(*to_prod)
return [T.alloc(T.cast(val, dtype=node.outputs[0].dtype), return [T.alloc(T.cast(val, dtype=node.outputs[0].dtype),
*[shapes[i] for i in range(len(shapes)) if i not in node.op.axis])] *[shapes[i] for i in xrange(len(shapes)) if i not in node.op.axis])]
except TypeError, e: except TypeError, e:
pass pass
...@@ -2905,7 +2905,7 @@ def local_pow_specialize_device(node): ...@@ -2905,7 +2905,7 @@ def local_pow_specialize_device(node):
pow2 = [xsym] pow2 = [xsym]
pow2_scal = [theano.scalar.Scalar(xsym.dtype)()] pow2_scal = [theano.scalar.Scalar(xsym.dtype)()]
y_to_do = abs(y) y_to_do = abs(y)
for i in range(int(numpy.log2(y_to_do))): for i in xrange(int(numpy.log2(y_to_do))):
pow2.append(T.sqr(pow2[i])) pow2.append(T.sqr(pow2[i]))
pow2_scal.append(theano.scalar.sqr(pow2_scal[i])) pow2_scal.append(theano.scalar.sqr(pow2_scal[i]))
rval1 = None rval1 = None
...@@ -3546,7 +3546,7 @@ def local_grad_log_erfc_neg(node): ...@@ -3546,7 +3546,7 @@ def local_grad_log_erfc_neg(node):
mul_inputs = check_input(mul_neg.owner.inputs) mul_inputs = check_input(mul_neg.owner.inputs)
#put the constant first #put the constant first
for i in range(len(mul_inputs)): for i in xrange(len(mul_inputs)):
if isinstance(i, Constant): if isinstance(i, Constant):
if i==0: if i==0:
break break
...@@ -3636,7 +3636,7 @@ a64=[x*((1-1/(2*x**2)+3/(4*x**4)-15/(8*x**6))**(-1))*numpy.sqrt(numpy.pi) for x ...@@ -3636,7 +3636,7 @@ a64=[x*((1-1/(2*x**2)+3/(4*x**4)-15/(8*x**6))**(-1))*numpy.sqrt(numpy.pi) for x
a32=[x*((1-1/(2*x**2)+3/(4*x**4)-15/(8*x**6))**(-1))*numpy.float32(numpy.sqrt(numpy.pi)) for x in numpy.asarray(r,dtype='float32')] a32=[x*((1-1/(2*x**2)+3/(4*x**4)-15/(8*x**6))**(-1))*numpy.float32(numpy.sqrt(numpy.pi)) for x in numpy.asarray(r,dtype='float32')]
for idx,(a,b,c,d,e) in enumerate(zip(r,p64,p32,a64,a32)):print a,b,c,d,e,c-b,e-b,numpy.absolute(c-b)<numpy.absolute(e-b) for idx,(a,b,c,d,e) in enumerate(zip(r,p64,p32,a64,a32)):print a,b,c,d,e,c-b,e-b,numpy.absolute(c-b)<numpy.absolute(e-b)
for i in range(1,len(p32)): print r[i], p32[i]-p32[i-1]#, show that the value don't look stable at some point before inf. for i in xrange(1,len(p32)): print r[i], p32[i]-p32[i-1]#, show that the value don't look stable at some point before inf.
#float64 threshold is 26.63 the approx seam more precise at that point. #float64 threshold is 26.63 the approx seam more precise at that point.
r = numpy.arange(26.2,26.7,.001) r = numpy.arange(26.2,26.7,.001)
...@@ -3649,7 +3649,7 @@ a128=[x*((1-1/(2*x**2)+3/(4*x**4)-15/(8*x**6))**(-1))*numpy.float128(numpy.sqrt( ...@@ -3649,7 +3649,7 @@ a128=[x*((1-1/(2*x**2)+3/(4*x**4)-15/(8*x**6))**(-1))*numpy.float128(numpy.sqrt(
a64=[x*((1-1/(2*x**2)+3/(4*x**4)-15/(8*x**6)+63/(7*x**8))**(-1))*numpy.sqrt(numpy.pi) for x in r] a64=[x*((1-1/(2*x**2)+3/(4*x**4)-15/(8*x**6)+63/(7*x**8))**(-1))*numpy.sqrt(numpy.pi) for x in r]
for a,b,c,d in zip(r,p128,p64,a64):print a,b,c,d,c-b,d-b for a,b,c,d in zip(r,p128,p64,a64):print a,b,c,d,c-b,d-b
for i in range(1,len(p64)): print i, 64[i]-p64[i-1] for i in xrange(1,len(p64)): print i, 64[i]-p64[i-1]
""" """
# ############### # ###############
......
...@@ -357,7 +357,7 @@ def _generate_broadcasting_indices(out_shape, *shapes): ...@@ -357,7 +357,7 @@ def _generate_broadcasting_indices(out_shape, *shapes):
# Will contain the return value: a list of indices for each argument # Will contain the return value: a list of indices for each argument
ret_indices = [ [()] for shape in all_shapes ] ret_indices = [ [()] for shape in all_shapes ]
for dim in range(len(out_shape)): for dim in xrange(len(out_shape)):
# Temporary list to generate the indices # Temporary list to generate the indices
_ret_indices = [ [] for shape in all_shapes ] _ret_indices = [ [] for shape in all_shapes ]
...@@ -377,7 +377,7 @@ def _generate_broadcasting_indices(out_shape, *shapes): ...@@ -377,7 +377,7 @@ def _generate_broadcasting_indices(out_shape, *shapes):
for prev_index in zip(*ret_indices): for prev_index in zip(*ret_indices):
for dim_index in zip(*ranges): for dim_index in zip(*ranges):
for i in range(len(all_shapes)): for i in xrange(len(all_shapes)):
_ret_indices[i].append(prev_index[i] + (dim_index[i],)) _ret_indices[i].append(prev_index[i] + (dim_index[i],))
ret_indices = _ret_indices ret_indices = _ret_indices
...@@ -488,7 +488,7 @@ def random_integers_helper(random_state, low, high, size): ...@@ -488,7 +488,7 @@ def random_integers_helper(random_state, low, high, size):
out_size = tuple(size) out_size = tuple(size)
else: else:
out_size = () out_size = ()
for dim in range(out_ndim): for dim in xrange(out_ndim):
dim_len = max(low.shape[dim], high.shape[dim]) dim_len = max(low.shape[dim], high.shape[dim])
out_size = out_size + (dim_len,) out_size = out_size + (dim_len,)
...@@ -602,7 +602,7 @@ def multinomial_helper(random_state, n, pvals, size): ...@@ -602,7 +602,7 @@ def multinomial_helper(random_state, n, pvals, size):
size = tuple(size) size = tuple(size)
else: else:
size = () size = ()
for dim in range(ndim): for dim in xrange(ndim):
dim_len = max(n.shape[dim], pvals.shape[dim]) dim_len = max(n.shape[dim], pvals.shape[dim])
size = size + (dim_len,) size = size + (dim_len,)
out_size = size+(pvals.shape[-1],) out_size = size+(pvals.shape[-1],)
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论