提交 f1ef401b authored 作者: Olivier Delalleau's avatar Olivier Delalleau

Merge pull request #479 from nouiz/except

Better exception handling
......@@ -443,7 +443,7 @@ class Env(utils.object2):
"""
try:
self._features.remove(feature)
except:
except Exception:
return
detach = getattr(feature, 'on_detach', None)
if detach is not None:
......
from theano.gof.optdb import *
from unittest import TestCase
from theano.gof.optdb import opt, DB
class Test_DB(TestCase):
def test_0(self):
class Opt(opt.Optimizer): #inheritance buys __hash__
class Opt(opt.Optimizer): # inheritance buys __hash__
name = 'blah'
db = DB()
......@@ -16,36 +18,34 @@ class Test_DB(TestCase):
db.register('c', Opt(), 'z', 'asdf')
try:
db.register('c', Opt()) #name taken
db.register('c', Opt()) # name taken
self.fail()
except ValueError, e:
if e[0].startswith("The name"):
pass
else:
raise
except:
except Exception:
self.fail()
try:
db.register('z', Opt()) #name collides with tag
db.register('z', Opt()) # name collides with tag
self.fail()
except ValueError, e:
if e[0].startswith("The name"):
pass
else:
raise
except:
except Exception:
self.fail()
try:
db.register('u', Opt(), 'b') #name new but tag collides with name
db.register('u', Opt(), 'b') # name new but tag collides with name
self.fail()
except ValueError, e:
if e[0].startswith("The tag"):
pass
else:
raise
except:
except Exception:
self.fail()
......@@ -285,20 +285,11 @@ def comm_guard(type1, type2):
and (type2 is ANY_TYPE or isinstance(arg1, type2)):
arg1, arg2 = arg2, arg1
else:
try:
return old_f(arg1, arg2, *rest)
except:
raise
try:
variable = f(arg1, arg2, *rest)
except:
raise
return old_f(arg1, arg2, *rest)
variable = f(arg1, arg2, *rest)
if variable is FALL_THROUGH:
try:
return old_f(arg1, arg2, *rest)
except:
raise
return old_f(arg1, arg2, *rest)
else:
return variable
......
......@@ -299,13 +299,13 @@ class TestScan(unittest.TestCase):
for th_out, num_out in zip(theano_outs, numpy_outs):
try:
assert numpy.allclose(th_out, num_out)
except:
except Exception:
#import ipdb; ipdb.set_trace()
raise
for th_out, num_out in zip(shared_vars, numpy_shared):
try:
assert numpy.allclose(th_out.get_value(), num_out)
except:
except Exception:
#import ipdb; ipdb.set_trace()
raise
# Scenario 2 : Loose fit (sequences longer then required)
......
......@@ -310,25 +310,12 @@ class TestConv2D(unittest.TestCase):
"""
Make sure errors are raised when image and kernel are not 4D tensors
"""
try:
self.validate((3,2,8,8), (4,2,5,5), 'valid', input = T.dmatrix())
# should never reach here
self.fail()
except:
pass
try:
self.validate((3,2,8,8), (4,2,5,5), 'valid', filters = T.dvector())
# should never reach here
self.fail()
except:
pass
try:
self.validate((3,2,8,8), (4,2,5,5), 'valid', input = T.dtensor3())
# should never reach here
self.fail()
except:
pass
self.assertRaises(Exception, self.validate, (3, 2, 8, 8), (4, 2, 5, 5),
'valid', input=T.dmatrix())
self.assertRaises(Exception, self.validate, (3, 2, 8, 8), (4, 2, 5, 5),
'valid', filters=T.dvector())
self.assertRaises(Exception, self.validate, (3, 2, 8, 8), (4, 2, 5, 5),
'valid', input=T.dtensor3())
def test_gcc_crash(self):
"""
......
......@@ -496,7 +496,7 @@ class T_CrossentropyCategorical1Hot(unittest.TestCase):
try:
assert len(f.maker.env.toposort()) == 4
f(x_val, y_val)
except:
except Exception:
theano.printing.debugprint(f)
raise
......@@ -507,7 +507,7 @@ class T_CrossentropyCategorical1Hot(unittest.TestCase):
try:
assert len(g.maker.env.toposort()) == 4
g(x_val, y_val)
except:
except Exception:
theano.printing.debugprint(g)
raise
......@@ -525,7 +525,7 @@ class T_CrossentropyCategorical1Hot(unittest.TestCase):
try:
assert len(f.maker.env.toposort()) == 2 # [big_op, sum]
f(x_val, b_val, y_val)
except:
except Exception:
theano.printing.debugprint(f)
raise
......@@ -535,7 +535,7 @@ class T_CrossentropyCategorical1Hot(unittest.TestCase):
try:
assert len(g.maker.env.toposort()) == 4
g(x_val, b_val, y_val)
except:
except Exception:
theano.printing.debugprint(g)
raise
......@@ -553,7 +553,7 @@ class T_CrossentropyCategorical1Hot(unittest.TestCase):
try:
assert len(f.maker.env.toposort()) == 6
f(x_val, y_val)
except:
except Exception:
theano.printing.debugprint(f)
raise
......@@ -564,7 +564,7 @@ class T_CrossentropyCategorical1Hot(unittest.TestCase):
assert len(g.maker.env.toposort()) in (6,7) #there's an extra dimshuffle in there
# but I can't think of a good rule to get rid of it
g(x_val, y_val)
except:
except Exception:
theano.printing.debugprint(g)
raise
......@@ -580,7 +580,7 @@ class T_CrossentropyCategorical1Hot(unittest.TestCase):
theano.printing.debugprint(f)
try:
assert len(f.maker.env.toposort()) == 4
except:
except Exception:
theano.printing.debugprint(f)
raise
......@@ -590,7 +590,7 @@ class T_CrossentropyCategorical1Hot(unittest.TestCase):
try:
assert len(g.maker.env.toposort()) in (6,7)
g(x_val, b_val, y_val)
except:
except Exception:
theano.printing.debugprint(g)
raise
......@@ -624,7 +624,7 @@ class T_CrossentropyCategorical1Hot(unittest.TestCase):
try:
assert len(f.maker.env.toposort()) == 5
f(x_val, y_val)
except:
except Exception:
theano.printing.debugprint(f)
raise
......@@ -635,7 +635,7 @@ class T_CrossentropyCategorical1Hot(unittest.TestCase):
try:
assert len(g.maker.env.toposort()) == 5
g(x_val, y_val)
except:
except Exception:
theano.printing.debugprint(g)
raise
......@@ -671,7 +671,7 @@ class T_CrossentropyCategorical1Hot(unittest.TestCase):
prev, last = f.maker.env.toposort()[-2:]
assert len(f.maker.env.toposort()) == 5
f(x_val, y_val)
except:
except Exception:
theano.printing.debugprint(f)
raise
......@@ -684,7 +684,7 @@ class T_CrossentropyCategorical1Hot(unittest.TestCase):
assert softmax in ops
assert softmax_grad not in ops
g(x_val, y_val)
except:
except Exception:
theano.printing.debugprint(g)
raise
......@@ -723,7 +723,7 @@ class T_CrossentropyCategorical1Hot(unittest.TestCase):
prev, last = f.maker.env.toposort()[-2:]
assert len(f.maker.env.toposort()) == 3 # [big_op, sum, dim_shuffle]
f(x_val, b_val, y_val)
except:
except Exception:
theano.printing.debugprint(f)
raise
......@@ -742,7 +742,7 @@ class T_CrossentropyCategorical1Hot(unittest.TestCase):
assert softmax_with_bias in ops
assert softmax_grad not in ops
g(x_val, b_val, y_val)
except:
except Exception:
theano.printing.debugprint(g)
raise
......@@ -831,7 +831,7 @@ class T_CrossentropyCategorical1Hot(unittest.TestCase):
assert 5 <= len(f.maker.env.toposort()) <= 10
validate_fn_graph(f)
f(x_val, y_val, 0.1)
except:
except Exception:
theano.printing.debugprint(f)
raise
......@@ -841,7 +841,7 @@ class T_CrossentropyCategorical1Hot(unittest.TestCase):
assert 5 <= len(g.maker.env.toposort()) <= 12
validate_grad_graph(g)
g(x_val, y_val, 0.1)
except:
except Exception:
theano.printing.debugprint(g)
raise
......@@ -851,7 +851,7 @@ class T_CrossentropyCategorical1Hot(unittest.TestCase):
assert 8 <= len(h.maker.env.toposort()) <= 17
validate_grad_graph(h)
h(x_val, y_val, 0.1)
except:
except Exception:
theano.printing.debugprint(h)
raise
......
import traceback
import numpy
import theano.tensor.basic
from basic import TensorType, _tensor_py_operators, autocast_int, autocast_float
from basic import TensorType, _tensor_py_operators
from theano.compile import shared_constructor, SharedVariable
from theano import config
def load_shared_variable(val):
"""This function is only here to keep some pickles loading
......@@ -11,35 +13,40 @@ def load_shared_variable(val):
It can be removed after sufficient time has passed."""
return tensor_constructor(val)
# _tensor_py_operators is first to have its version of __{gt,ge,lt,le}__
class TensorSharedVariable(_tensor_py_operators, SharedVariable):
pass
@shared_constructor
def tensor_constructor(value, name=None, strict=False, allow_downcast=None, borrow=False, broadcastable=None):
def tensor_constructor(value, name=None, strict=False, allow_downcast=None,
borrow=False, broadcastable=None):
"""SharedVariable Constructor for TensorType
:note: Regarding the inference of the broadcastable pattern...
The default is to assume that the value might be resized in any dimension, so the default
broadcastable is ``(False,)*len(value.shape)``. The optional `broadcastable` argument will
override this default.
The default is to assume that the value might be resized in any
dimension, so the default broadcastable is
``(False,)*len(value.shape)``. The optional `broadcastable`
argument will override this default.
"""
if not isinstance(value, numpy.ndarray):
raise TypeError()
# if no broadcastable is given, then the default is to assume that the value might be
# resized in any dimension in the future.
# if no broadcastable is given, then the default is to assume that
# the value might be resized in any dimension in the future.
#
if broadcastable is None:
broadcastable = (False,)*len(value.shape)
broadcastable = (False,) * len(value.shape)
type = TensorType(value.dtype, broadcastable=broadcastable)
return TensorSharedVariable(type=type,
value=numpy.array(value,copy=(not borrow)),
value=numpy.array(value, copy=(not borrow)),
name=name,
strict=strict,
allow_downcast=allow_downcast)
# TensorSharedVariable brings in the tensor operators, is not ideal, but works
# as long as we dont do purely scalar-scalar operations
# _tensor_py_operators is first to have its version of __{gt,ge,lt,le}__
......@@ -50,6 +57,7 @@ def tensor_constructor(value, name=None, strict=False, allow_downcast=None, borr
class ScalarSharedVariable(_tensor_py_operators, SharedVariable):
pass
@shared_constructor
def scalar_constructor(value, name=None, strict=False, allow_downcast=None):
"""SharedVariable constructor for scalar values. Default: int64 or float64.
......@@ -57,14 +65,14 @@ def scalar_constructor(value, name=None, strict=False, allow_downcast=None):
:note: We implement this using 0-d tensors for now.
"""
if not isinstance (value, (numpy.number, float, int, complex)):
if not isinstance(value, (numpy.number, float, int, complex)):
raise TypeError()
try:
dtype=value.dtype
except:
dtype=numpy.asarray(value).dtype
dtype = value.dtype
except Exception:
dtype = numpy.asarray(value).dtype
dtype=str(dtype)
dtype = str(dtype)
value = theano._asarray(value, dtype=dtype)
tensor_type = TensorType(dtype=str(value.dtype), broadcastable=[])
......@@ -75,6 +83,6 @@ def scalar_constructor(value, name=None, strict=False, allow_downcast=None):
value=numpy.array(value, copy=True),
name=name, strict=strict, allow_downcast=allow_downcast)
return rval
except:
except Exception:
traceback.print_exc()
raise
import sys, time, unittest
import unittest
import numpy
import theano
import theano.tensor as T
from theano import function, Mode
from theano.tests import unittest_tools as utt
from theano.tensor.signal import conv
from theano.tensor.basic import _allclose
class TestSignalConv2D(unittest.TestCase):
def setUp(self):
......@@ -19,13 +20,15 @@ class TestSignalConv2D(unittest.TestCase):
image_dim = len(image_shape)
filter_dim = len(filter_shape)
input = T.TensorType('float64', [False]*image_dim)()
filters = T.TensorType('float64', [False]*filter_dim)()
input = T.TensorType('float64', [False] * image_dim)()
filters = T.TensorType('float64', [False] * filter_dim)()
bsize = image_shape[0]
if image_dim!=3: bsize = 1
if image_dim != 3:
bsize = 1
nkern = filter_shape[0]
if filter_dim!=3: nkern = 1
if filter_dim != 3:
nkern = 1
############# THEANO IMPLEMENTATION ############
# we create a symbolic function so that verify_grad can work
......@@ -35,7 +38,7 @@ class TestSignalConv2D(unittest.TestCase):
theano_conv = theano.function([input, filters], output)
# initialize input and compute result
image_data = numpy.random.random(image_shape)
image_data = numpy.random.random(image_shape)
filter_data = numpy.random.random(filter_shape)
theano_output = theano_conv(image_data, filter_data)
......@@ -45,10 +48,11 @@ class TestSignalConv2D(unittest.TestCase):
ref_output = numpy.zeros(tuple(out_shape2d))
# reshape as 3D input tensors to make life easier
image_data3d = image_data.reshape((bsize,)+image_shape[-2:])
filter_data3d = filter_data.reshape((nkern,)+filter_shape[-2:])
image_data3d = image_data.reshape((bsize,) + image_shape[-2:])
filter_data3d = filter_data.reshape((nkern,) + filter_shape[-2:])
# reshape theano output as 4D to make life easier
theano_output4d = theano_output.reshape((bsize,nkern,)+theano_output.shape[-2:])
theano_output4d = theano_output.reshape((bsize, nkern,) +
theano_output.shape[-2:])
# loop over mini-batches (if required)
for b in range(bsize):
......@@ -56,17 +60,19 @@ class TestSignalConv2D(unittest.TestCase):
# loop over filters (if required)
for k in range(nkern):
image2d = image_data3d[b,:,:]
filter2d = filter_data3d[k,:,:]
image2d = image_data3d[b, :, :]
filter2d = filter_data3d[k, :, :]
output2d = numpy.zeros(ref_output.shape)
for row in range(ref_output.shape[0]):
for col in range(ref_output.shape[1]):
output2d[row,col] += (image2d[row:row+filter2d.shape[0],
col:col+filter2d.shape[1]]*filter2d[::-1,::-1]
).sum()
output2d[row, col] += (
image2d[row:row + filter2d.shape[0],
col:col + filter2d.shape[1]] *
filter2d[::-1, ::-1]
).sum()
self.assertTrue(_allclose(theano_output4d[b,k,:,:], output2d))
self.assertTrue(_allclose(theano_output4d[b, k, :, :],
output2d))
############# TEST GRADIENT ############
if verify_grad:
......@@ -74,29 +80,22 @@ class TestSignalConv2D(unittest.TestCase):
def test_basic(self):
"""
Basic functionality of nnet.conv.ConvOp is already tested by its own test suite. We
just have to test whether or not signal.conv.conv2d can support inputs and filters of
type matrix or tensor3.
Basic functionality of nnet.conv.ConvOp is already tested by
its own test suite. We just have to test whether or not
signal.conv.conv2d can support inputs and filters of type
matrix or tensor3.
"""
self.validate((1,4,5), (2,2,3), verify_grad=True)
self.validate((7,5), (5,2,3), verify_grad=False)
self.validate((3,7,5), (2,3), verify_grad=False)
self.validate((7,5), (2,3), verify_grad=False)
self.validate((1, 4, 5), (2, 2, 3), verify_grad=True)
self.validate((7, 5), (5, 2, 3), verify_grad=False)
self.validate((3, 7, 5), (2, 3), verify_grad=False)
self.validate((7, 5), (2, 3), verify_grad=False)
def test_fail(self):
"""
Test that conv2d fails for dimensions other than 2 or 3.
"""
try:
conv.conv2d(T.dtensor4(), T.dtensor3())
self.fail()
except:
pass
try:
conv.conv2d(T.dtensor3(), T.dvector())
self.fail()
except:
pass
self.assertRaises(Exception, conv.conv2d, T.dtensor4(), T.dtensor3())
self.assertRaises(Exception, conv.conv2d, T.dtensor3(), T.dvector())
def test_bug_josh_reported(self):
"""
......@@ -106,5 +105,4 @@ class TestSignalConv2D(unittest.TestCase):
"""
m1 = theano.tensor.matrix()
m2 = theano.tensor.matrix()
rval = conv.conv2d(m1, m2)
conv.conv2d(m1, m2)
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论