提交 61493b72 authored 作者: Olivier Delalleau's avatar Olivier Delalleau

Merged

......@@ -246,13 +246,13 @@ def neibs2images(neibs, neib_shape, original_shape, mode='valid'):
neib_shape = T.as_tensor_variable(neib_shape)
original_shape = T.as_tensor_variable(original_shape)
new_neib_shape = T.stack( original_shape[-1]/neib_shape[1], neib_shape[1] )
new_neib_shape = T.stack(original_shape[-1] // neib_shape[1], neib_shape[1])
output_2d = images2neibs(neibs.dimshuffle('x','x',0,1), new_neib_shape, mode=mode)
if mode == 'ignore_borders':
valid_shape = list(original_shape)
valid_shape[2] = valid_shape[2] / neib_shape[0] * neib_shape[0]
valid_shape[3] = valid_shape[3] / neib_shape[1] * neib_shape[1]
valid_shape[2] = (valid_shape[2] // neib_shape[0]) * neib_shape[0]
valid_shape[3] = (valid_shape[3] // neib_shape[1]) * neib_shape[1]
output_4d = output_2d.reshape(valid_shape)
#padding the borders with zeros
for d in [2,3]:
......
......@@ -263,7 +263,7 @@ class mrg_uniform(mrg_uniform_base):
if (%(size)s->dimensions[0] != %(ndim)s)
{
PyErr_Format(PyExc_ValueError, "size must have length %%i (not %%i)",
%(ndim)s, %(size)s->dimensions[0]);
%(ndim)s, int(%(size)s->dimensions[0]));
%(fail)s
}
if (%(size)s->descr->type_num != PyArray_INT32)
......@@ -789,11 +789,11 @@ class MRG_RandomStreams(object):
flattened = self.uniform(size=(n_samples,), dtype=dtype)
if constant:
U1 = flattened[:n_samples/2]
U2 = flattened[n_samples/2:]
U1 = flattened[:n_samples // 2]
U2 = flattened[n_samples // 2:]
else:
U1 = flattened[:prod(flattened.shape)/2]
U2 = flattened[prod(flattened.shape)/2:]
U1 = flattened[:prod(flattened.shape) // 2]
U2 = flattened[prod(flattened.shape) // 2:]
#normal_samples = zeros_like(flattened)
sqrt_ln_U1 = sqrt(-2.0*log(U1))
......
......@@ -27,6 +27,11 @@ builtin_int = int
builtin_float = float
class IntegerDivisionError(Exception):
"""Raised if someone tries to divide integers with '/' instead of '//'."""
pass
def upcast(dtype, *dtypes):
# Should we try to keep float32 instead of float64? This is used so that
# for instance mixing int64 with float32 yields float32 instead of float64.
......@@ -1028,7 +1033,7 @@ def div_proxy(x, y):
# Following discussion on theano-dev ("Inconsistent behavior in integer
# division"), we will change the semantics of "/" on integer types in
# Theano 0.4. Until then, it is forbidden to use "/" on integers.
raise NotImplementedError(
raise IntegerDivisionError(
"Dividing two integers with '/' is forbidden until Theano v0.4"
" is released (where the result will be a floating point "
"number). In the meantime, please either use '//' for integer "
......
......@@ -182,9 +182,9 @@ class test_div(unittest.TestCase):
d = float64()
f = float32()
print (a/b).owner.op
assert isinstance((a/b).owner.op, IntDiv)
assert isinstance((b/a).owner.op, IntDiv)
print (a//b).owner.op
assert isinstance((a//b).owner.op, IntDiv)
assert isinstance((b//a).owner.op, IntDiv)
assert isinstance((b/d).owner.op, TrueDiv)
assert isinstance((b/f).owner.op, TrueDiv)
assert isinstance((f/a).owner.op, TrueDiv)
......
......@@ -7,6 +7,7 @@ import sys # for sys.maxint
from theano.configparser import config, AddConfigVar, BoolParam
import traceback #for overriding Op.__call__
import warnings
from itertools import izip
import numpy, theano
#from copy import copy as python_copy
......@@ -23,6 +24,9 @@ from theano.gof.python25 import partial, any, all
from theano import compile, printing
from theano.printing import pprint
# We use this exception as well.
from theano.scalar import IntegerDivisionError
### set up the external interface
from elemwise import Elemwise, DimShuffle, CAReduce, Sum
......@@ -1138,7 +1142,7 @@ class _tensor_py_operators:
def __div__(self,other):
try:
return div_proxy(self,other)
except NotImplementedError:
except IntegerDivisionError:
# This is to raise the exception that occurs when trying to divide
# two integer arrays (currently forbidden).
raise
......@@ -2579,7 +2583,7 @@ def div_proxy(x, y):
if (as_tensor_variable(x).dtype in discrete_dtypes and
as_tensor_variable(y).dtype in discrete_dtypes):
# See the same in scalar/basic.py
raise NotImplementedError(
raise IntegerDivisionError(
"Dividing two integer arrays with '/' is forbidden until "
"Theano v0.4 is released (where the result will be a floating "
"point number). In the meantime, please either use '//' for "
......@@ -2921,7 +2925,7 @@ class Subtensor(Op):
padded = ( actual_idx_list +
[slice(None, None, None)]*(len(xshp)-len(self.idx_list)))
i = 0
for idx, xl in zip(padded, xshp):
for idx, xl in izip(padded, xshp):
if isinstance(idx, slice):
# If it is the default (None, None, None) slice, or a variant,
# the shape will be xl
......@@ -2931,7 +2935,7 @@ class Subtensor(Op):
outshp.append(xl)
else:
cnf = get_canonical_form_slice(idx, xl)
length = (cnf[0].stop - cnf[0].start -1)/cnf[0].step + 1
length = (cnf[0].stop - cnf[0].start -1) // cnf[0].step + 1
length = switch(lt(length,0), 0, length)
outshp.append(length)
i += 1
......
......@@ -135,9 +135,9 @@ class Conv3D(theano.Op):
vidDur = V_shape[3]
filterDur = W_shape[3]
output_height = T.floor( (vidHeight - filterHeight) / dr )+1
output_width = T.floor( (vidWidth - filterWidth) / dc )+1
output_dur = T.floor( (vidDur - filterDur) / dt ) +1
output_height = T.floor((vidHeight - filterHeight) // dr) + 1
output_width = T.floor((vidWidth - filterWidth) // dc) + 1
output_dur = T.floor((vidDur - filterDur) // dt) + 1
rval = (batch_size, output_height, output_width, output_dur, output_channels )
......
......@@ -32,6 +32,12 @@ from basic import get_constant_value
# Utilities
class ShapeError(Exception):
"""Raised when the shape cannot be computed."""
pass
def out2in(*local_opts):
"""WRITEME """
return opt.TopoOptimizer(opt.LocalOptGroup(*local_opts),
......@@ -528,7 +534,7 @@ class ShapeFeature(object):
the cost of many Ops accurately, and generate c-code that is specific [e.g. unrolled] to
particular sizes.
If you can determine the shape only in some case, return NotImplementedError when you can't
In cases where you cannot figure out the shape, raise a ShapeError.
.. note::
......@@ -714,13 +720,22 @@ class ShapeFeature(object):
try:
o_shapes = shape_infer(node, [self.shape_of[r] for r in node.inputs])
except NotImplementedError:
except ShapeError:
o_shapes = self.default_infer_shape(node, [self.shape_of[r] for r in node.inputs])
except NotImplementedError, e:
raise NotImplementedError(
'Code called by infer_shape failed raising a '
'NotImplementedError. Raising NotImplementedError to '
'indicate that a shape cannot be computed is no longer '
'supported, and one should now use tensor.opt.ShapeError '
'instead. The original exception message is: %s' % e)
except Exception, e:
_logger.error('Failed to infer_shape from Op %s (i_shapes=%s): %s %s'% (node.op,
[self.shape_of[r] for r in node.inputs],
type(e), str(e)))
o_shapes = self.default_infer_shape(node, [self.shape_of[r] for r in node.inputs])
# We raise the exception to make sure the user knows something bad
# is going on.
raise
# this is packed information
# an element of o_shapes is either None or a tuple
......
......@@ -2734,9 +2734,9 @@ class T_divimpl(unittest.TestCase):
(5.0/11.0))
assert numpy.allclose(function([i, ii, d, f, c], f/i)(5, 3, 7.0, 11.0, numpy.complex(5,3)),
(11.0/5.0))
assert numpy.allclose(function([i, ii, d, f, c], i/ii)(5, 3, 7.0, 11.0, numpy.complex(5,3)),
assert numpy.allclose(function([i, ii, d, f, c], i//ii)(5, 3, 7.0, 11.0, numpy.complex(5,3)),
(5/3))
assert numpy.allclose(function([i, ii, d, f, c], ii/i)(5, 3, 7.0, 11.0, numpy.complex(5,3)),
assert numpy.allclose(function([i, ii, d, f, c], ii//i)(5, 3, 7.0, 11.0, numpy.complex(5,3)),
(3/5))
assert numpy.allclose(function([i, ii, d, f, c], true_div(i,ii))(5, 3, 7.0, 11.0, numpy.complex(5,3)),
(5./3.))
......
......@@ -647,10 +647,13 @@ def test_local_merge_abs():
def test_mixeddiv():
"""Test that int division is preserved"""
"""Test that int division raises an exception."""
i = iscalar()
d = dscalar()
assert 0 == function([i,d], d*(i/(i+1)))(3, 1.0)
try:
0 == function([i,d], d*(i/(i+1)))(3, 1.0)
except theano.scalar.IntegerDivisionError:
pass
def test_const_type_in_mul_canonizer():
input = dmatrix()
......
""" test code snippet in the Theano tutorials.
"""
import unittest
import os, unittest
import theano
import theano.tensor as T
from theano import function
......@@ -722,6 +722,15 @@ class T_loading_and_saving(unittest.TestCase):
mode_instance = theano.compile.mode.get_mode(None)
if not isinstance(mode_instance, theano.compile.debugmode.DebugMode):
if os.path.exists('obj.save') or os.path.exists('objects.save'):
# We do not want to delete these files silently, in case for
# some reason they would be something else than test-generated
# files.
# Ideally we would save those files in a temporary directory...
raise AssertionError(
'Please get rid of files obj.save and '
'objects.save in directory %s' % os.getcwd())
f = file('obj.save', 'wb')
cPickle.dump(my_obj, f, protocol=cPickle.HIGHEST_PROTOCOL)
f.close()
......@@ -746,6 +755,9 @@ class T_loading_and_saving(unittest.TestCase):
loaded_objects.append(cPickle.load(f))
f.close()
# Cleanup created files.
os.remove('obj.save')
os.remove('objects.save')
class T_modes(unittest.TestCase):
## All tests here belog to
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论