提交 6469c740 authored 作者: Frédéric Bastien's avatar Frédéric Bastien

Merge pull request #1427 from nouiz/py3.3

Fix python code in python 3.3 and fix tests.
...@@ -3511,12 +3511,9 @@ class T_Scan(unittest.TestCase): ...@@ -3511,12 +3511,9 @@ class T_Scan(unittest.TestCase):
#y7, _ = theano.scan(lambda i, W, _, _2: W[i], sequences=v, #y7, _ = theano.scan(lambda i, W, _, _2: W[i], sequences=v,
# outputs_info=None, non_sequences=[v, W[0], W]) # outputs_info=None, non_sequences=[v, W[0], W])
for out in [y1, y2, y3, y4, y5, y6]: for out in [y1, y2, y3, y4, y5, y6]:
print
print "Begin test"
print
#This used to raise an exception #This used to raise an exception
f = theano.function([W, v], out) f = theano.function([W, v], out, mode=mode_with_opt)
f(numpy.zeros((3, 3)), [1, 2]) f(numpy.zeros((3, 3), dtype=theano.config.floatX), [1, 2])
scan_node = f.maker.fgraph.toposort()[-1] scan_node = f.maker.fgraph.toposort()[-1]
# TODO: Why this assert always fail? # TODO: Why this assert always fail?
......
...@@ -5,16 +5,20 @@ DownsampleFactorMax, DownsampleAvg, DownsampleSoftmax. ...@@ -5,16 +5,20 @@ DownsampleFactorMax, DownsampleAvg, DownsampleSoftmax.
""" """
#This file should move along with conv.py #This file should move along with conv.py
import __builtin__
import numpy
import theano
from theano import gof, Op, tensor, Variable, Apply from theano import gof, Op, tensor, Variable, Apply
import numpy, theano
import __builtin__
def max_pool2D(*args, **kwargs): def max_pool2D(*args, **kwargs):
import sys import sys
print >> sys.stderr, "DEPRECATION: max_pool2D renamed to max_pool_2d" print >> sys.stderr, "DEPRECATION: max_pool2D renamed to max_pool_2d"
return max_pool_2d(*args, **kwargs) return max_pool_2d(*args, **kwargs)
def max_pool_2d(input, ds, ignore_border=False): def max_pool_2d(input, ds, ignore_border=False):
""" """
Takes as input a N-D tensor, where N >= 2. It downscales the input image by Takes as input a N-D tensor, where N >= 2. It downscales the input image by
...@@ -36,11 +40,12 @@ def max_pool_2d(input, ds, ignore_border=False): ...@@ -36,11 +40,12 @@ def max_pool_2d(input, ds, ignore_border=False):
# count the number of "leading" dimensions, store as dmatrix # count the number of "leading" dimensions, store as dmatrix
batch_size = tensor.prod(input.shape[:-2]) batch_size = tensor.prod(input.shape[:-2])
batch_size = tensor.shape_padright(batch_size,1) batch_size = tensor.shape_padright(batch_size, 1)
# store as 4D tensor with shape: (batch_size,1,height,width) # store as 4D tensor with shape: (batch_size,1,height,width)
new_shape = tensor.cast(tensor.join(0, batch_size, new_shape = tensor.cast(tensor.join(0, batch_size,
tensor.as_tensor([1,]), img_shape), 'int64') tensor.as_tensor([1,]),
img_shape), 'int64')
input_4D = tensor.reshape(input, new_shape, ndim=4) input_4D = tensor.reshape(input, new_shape, ndim=4)
# downsample mini-batch of images # downsample mini-batch of images
...@@ -53,10 +58,11 @@ def max_pool_2d(input, ds, ignore_border=False): ...@@ -53,10 +58,11 @@ def max_pool_2d(input, ds, ignore_border=False):
class DownsampleFactorMax(Op): class DownsampleFactorMax(Op):
""" """For N-dimensional tensors, consider that the last two
For N-dimensional tensors, consider that the last two dimensions span images. dimensions span images. This Op downsamples these images by a
This Op downsamples these images by a factor ds, by taking the max over non- factor ds, by taking the max over non- overlapping rectangular
overlapping rectangular regions. regions.
""" """
@staticmethod @staticmethod
...@@ -83,7 +89,7 @@ class DownsampleFactorMax(Op): ...@@ -83,7 +89,7 @@ class DownsampleFactorMax(Op):
if len(imgshape) < 2: if len(imgshape) < 2:
raise TypeError('imgshape must have at least two elements (rows, cols)') raise TypeError('imgshape must have at least two elements (rows, cols)')
r, c = imgshape[-2:] r, c = imgshape[-2:]
rval = list(imgshape[:-2]) + [ r // ds[0], c // ds[1] ] rval = list(imgshape[:-2]) + [r // ds[0], c // ds[1]]
if not ignore_border: if not ignore_border:
if isinstance(r, theano.Variable): if isinstance(r, theano.Variable):
...@@ -111,13 +117,16 @@ class DownsampleFactorMax(Op): ...@@ -111,13 +117,16 @@ class DownsampleFactorMax(Op):
self.ignore_border = ignore_border self.ignore_border = ignore_border
def __eq__(self, other): def __eq__(self, other):
return type(self) == type(other) and self.ds == other.ds and self.ignore_border == other.ignore_border return (type(self) == type(other) and
self.ds == other.ds and
self.ignore_border == other.ignore_border)
def __hash__(self): def __hash__(self):
return hash(type(self)) ^ hash(self.ds) ^ hash(self.ignore_border) return hash(type(self)) ^ hash(self.ds) ^ hash(self.ignore_border)
def __str__(self): def __str__(self):
return '%s{%s,%s}' % (self.__class__.__name__, self.ds, self.ignore_border) return '%s{%s,%s}' % (self.__class__.__name__,
self.ds, self.ignore_border)
def make_node(self, x): def make_node(self, x):
if x.type.ndim != 4: if x.type.ndim != 4:
...@@ -130,30 +139,35 @@ class DownsampleFactorMax(Op): ...@@ -130,30 +139,35 @@ class DownsampleFactorMax(Op):
""" """
x, = inp x, = inp
z, = out z, = out
if len(x.shape)!=4: if len(x.shape) != 4:
raise NotImplementedError('DownsampleFactorMax requires 4D input for now') raise NotImplementedError(
'DownsampleFactorMax requires 4D input for now')
z_shape = self.out_shape(x.shape, self.ds, self.ignore_border) z_shape = self.out_shape(x.shape, self.ds, self.ignore_border)
if (z[0] is None) or (z[0].shape != z_shape): if (z[0] is None) or (z[0].shape != z_shape):
z[0] = numpy.zeros(self.out_shape(x.shape, self.ds, self.ignore_border)) z[0] = numpy.zeros(self.out_shape(x.shape, self.ds,
self.ignore_border))
z[0] = theano._asarray(z[0], dtype=x.dtype) z[0] = theano._asarray(z[0], dtype=x.dtype)
zz=z[0] zz = z[0]
## zz needs to be initialized with -inf for the following to work ## zz needs to be initialized with -inf for the following to work
zz -= numpy.inf zz -= numpy.inf
ds0, ds1 = self.ds ds0, ds1 = self.ds
if self.ignore_border: if self.ignore_border:
x_usable2 = (x.shape[2] / ds0 * ds0) x_usable2 = (x.shape[2] // ds0 * ds0)
else: x_usable2 = x.shape[2] else:
x_usable2 = x.shape[2]
if self.ignore_border: if self.ignore_border:
x_usable3 = (x.shape[3] / ds1 * ds1) x_usable3 = (x.shape[3] // ds1 * ds1)
else: x_usable3 = x.shape[3] else:
x_usable3 = x.shape[3]
for n in xrange(x.shape[0]): for n in xrange(x.shape[0]):
for k in xrange(x.shape[1]): for k in xrange(x.shape[1]):
for i in xrange(x_usable2): for i in xrange(x_usable2):
zi = i / ds0 zi = i / ds0
for j in xrange(x_usable3): for j in xrange(x_usable3):
zj = j / ds1 zj = j / ds1
zz[n,k,zi,zj] = __builtin__.max(zz[n,k,zi,zj], x[n,k,i,j]) zz[n, k, zi, zj] = __builtin__.max(zz[n, k, zi, zj],
x[n, k, i, j])
def infer_shape(self, node, in_shapes): def infer_shape(self, node, in_shapes):
shp = self.out_shape(in_shapes[0], self.ds, self.ignore_border) shp = self.out_shape(in_shapes[0], self.ds, self.ignore_border)
...@@ -163,12 +177,14 @@ class DownsampleFactorMax(Op): ...@@ -163,12 +177,14 @@ class DownsampleFactorMax(Op):
x, = inp x, = inp
gz, = grads gz, = grads
maxout = self(x) maxout = self(x)
return [DownsampleFactorMaxGrad(self.ds, ignore_border=self.ignore_border)(x, maxout, gz)] return [DownsampleFactorMaxGrad(self.ds,
ignore_border=self.ignore_border)(
x, maxout, gz)]
def c_code(self, node, name, inp, out, sub): def c_code(self, node, name, inp, out, sub):
x, = inp x, = inp
z, = out z, = out
fail=sub['fail'] fail = sub['fail']
ignore_border = int(self.ignore_border) ignore_border = int(self.ignore_border)
ds0, ds1 = self.ds ds0, ds1 = self.ds
return """ return """
...@@ -209,7 +225,8 @@ class DownsampleFactorMax(Op): ...@@ -209,7 +225,8 @@ class DownsampleFactorMax(Op):
dims[1]=PyArray_DIMS(%(x)s)[1]; dims[1]=PyArray_DIMS(%(x)s)[1];
dims[2]=z_shp0; dims[2]=z_shp0;
dims[3]=z_shp1; dims[3]=z_shp1;
%(z)s = (PyArrayObject*) PyArray_ZEROS(4, dims, typenum,0); //TODO: zeros not necessary //TODO: zeros not necessary
%(z)s = (PyArrayObject*) PyArray_ZEROS(4, dims, typenum,0);
} }
if (z_shp0 && z_shp1) if (z_shp0 && z_shp1)
...@@ -237,7 +254,7 @@ class DownsampleFactorMax(Op): ...@@ -237,7 +254,7 @@ class DownsampleFactorMax(Op):
""" % locals() """ % locals()
def c_code_cache_version(self): def c_code_cache_version(self):
return (0,1) return (0, 1)
class DownsampleFactorMaxGrad(Op): class DownsampleFactorMaxGrad(Op):
...@@ -247,20 +264,23 @@ class DownsampleFactorMaxGrad(Op): ...@@ -247,20 +264,23 @@ class DownsampleFactorMaxGrad(Op):
self.ignore_border = ignore_border self.ignore_border = ignore_border
def __eq__(self, other): def __eq__(self, other):
return type(self) == type(other) and self.ds == other.ds and self.ignore_border == other.ignore_border return (type(self) == type(other) and
self.ds == other.ds and
self.ignore_border == other.ignore_border)
def __hash__(self): def __hash__(self):
return hash(type(self)) ^ hash(self.ds) ^ hash(self.ignore_border) return hash(type(self)) ^ hash(self.ds) ^ hash(self.ignore_border)
def __str__(self): def __str__(self):
return '%s{%s,%s}' % (self.__class__.__name__, self.ds, self.ignore_border) return '%s{%s,%s}' % (self.__class__.__name__,
self.ds, self.ignore_border)
def make_node(self, x, maxout, gz): def make_node(self, x, maxout, gz):
# make_node should only be called by the grad function of DownsampleFactorMax, # make_node should only be called by the grad function of
# so these asserts should not fail. # DownsampleFactorMax, so these asserts should not fail.
assert isinstance(x, Variable) and x.ndim==4 assert isinstance(x, Variable) and x.ndim == 4
assert isinstance(maxout, Variable) and maxout.ndim==4 assert isinstance(maxout, Variable) and maxout.ndim == 4
assert isinstance(gz, Variable) and gz.ndim==4 assert isinstance(gz, Variable) and gz.ndim == 4
return Apply(self, [x, maxout, gz], [x.type()]) return Apply(self, [x, maxout, gz], [x.type()])
...@@ -270,10 +290,12 @@ class DownsampleFactorMaxGrad(Op): ...@@ -270,10 +290,12 @@ class DownsampleFactorMaxGrad(Op):
gx = numpy.zeros_like(x) gx = numpy.zeros_like(x)
ds0, ds1 = self.ds ds0, ds1 = self.ds
shape2 = (x.shape[2] / ds0 * ds0) shape2 = (x.shape[2] // ds0 * ds0)
if not self.ignore_border: shape2 = x.shape[2] if not self.ignore_border:
shape3 = (x.shape[3] / ds1 * ds1) shape2 = x.shape[2]
if not self.ignore_border: shape3 = x.shape[3] shape3 = (x.shape[3] // ds1 * ds1)
if not self.ignore_border:
shape3 = x.shape[3]
for n in xrange(x.shape[0]): for n in xrange(x.shape[0]):
for k in xrange(x.shape[1]): for k in xrange(x.shape[1]):
for i in xrange(shape2): for i in xrange(shape2):
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论