提交 64c46aa1 authored 作者: Olivier Delalleau's avatar Olivier Delalleau

Merged

......@@ -6,7 +6,7 @@ from theano.gof.cc import get_module_cache
if len(sys.argv) == 1:
print config.compiledir
elif sys.argv[1] in ('clear'):
get_module_cache().clear()
get_module_cache().clear(unversioned_min_age=-1)
else:
print 'command "%s" not recognized' % sys.argv[1]
print 'Type "theano-cache" to print the cache location'
......
......@@ -144,7 +144,7 @@ import theano and print the config variable, as in:
.. attribute:: floatX
String value: either 'float64' or 'float32'.
String value: either 'float64' or 'float32'
Default: 'float64'
......@@ -152,6 +152,47 @@ import theano and print the config variable, as in:
and similar functions. It also sets the default theano bit width for
arguments passed as Python floating-point numbers.
.. attribute:: cast_policy
String value: either 'numpy+floatX', 'numpy' or 'custom'
Default: 'custom'
This specifies how data types are implicitly figured out in Theano, e.g. for
constants or in the results of arithmetic operations. The current default
value ('custom') corresponds to a set of custom rules originally used in
Theano (which can be partially customized, see e.g. the in-code help of
``tensor.NumpyAutocaster``). However the 'custom' option will be
deprecated in a future release of Theano. The 'numpy' setting attempts to
mimic the numpy casting rules. 'numpy+floatX' does the same, except that
it prefers to use float32 numbers instead of float64 when ``config.floatX``
is set to 'float32' (this will become the default value in a future
release of Theano). Note that both 'numpy' and 'numpy+floatX'
behave differently from numpy on purpose in the following situations:
* Depending on the value of ``config.int_division``, the resulting type
of a division of integer types with the ``/`` operator may not match
that of numpy.
* On mixed scalar / array operations, numpy tries to prevent the scalar
from upcasting the array's type unless it is of a fundamentally
different type. However it is not practical to implement in Theano
a behavior similar to the one currently found in numpy, so Theano
does not attempt to do the same.
.. attribute:: int_division
String value: either 'int', 'floatX' or 'raise'
Default: 'int'
Specifies what to do when one tries to compute ``x / y``, where both ``x`` and
``y`` are of integer types (possibly unsigned). 'int' means an integer is
returned (as in Python 2.X), but this behavior is deprecated. 'floatX'
returns a number of type given by ``config.floatX``. 'raise' is the safest
choice (and will become default in a future release of Theano) and raises
an error when one tries to do such an operation, enforcing the use of the
integer division operator (``//``) (if a float result is intended, either
cast one of the arguments to a float, or use ``x.__truediv__(y)``).
.. attribute:: mode
String value: 'Mode', 'ProfileMode', 'DebugMode', 'FAST_RUN', 'FAST_COMPILE'
......
......@@ -15,11 +15,16 @@ AddConfigVar('floatX',
EnumStr('float64', 'float32'),
)
# TODO Work-in-progress
#AddConfigVar('casting_policy',
# "Rules for implicit casts of constants in arithmetic operations",
# EnumStr('theano_0.3', 'numpy'),
# )
AddConfigVar('cast_policy',
"Rules for implicit type casting (until further notice, do not modify within a script, and clear your Theano cache whenever it is modified)",
EnumStr('custom', 'numpy+floatX', 'numpy'),
)
AddConfigVar('int_division',
"What to do when one computes x / y, where both x and y are of "
"integer types",
EnumStr('int', 'raise', 'floatX'),
)
#gpu mean let the driver select the gpu. Needed in case of gpu in exclusive mode.
#gpuX mean use the gpu number X.
......
......@@ -7,6 +7,8 @@ import ConfigParser
import logging
import warnings
import theano
_logger = logging.getLogger('theano.config')
class TheanoConfigWarning(Warning):
......@@ -103,6 +105,17 @@ def _config_print(thing, buf):
print >> buf, " Value: ", cv.val
print >> buf, ""
def get_config_md5():
"""
Return a string md5 of the current config options. It should be such that
we can safely assume that two different config setups will lead to two
different strings.
"""
all_opts = sorted(_config_var_list, key=lambda cv: cv.fullname)
return theano.gof.cc.hash_from_code('\n'.join(['%s = %s' % (cv.fullname, cv.val) for cv in all_opts]))
class TheanoConfigParser(object):
#properties are installed by AddConfigVar
_i_am_a_config_class = True
......@@ -110,6 +123,7 @@ class TheanoConfigParser(object):
sio = StringIO.StringIO()
_config_print(self.__class__, sio)
return sio.getvalue()
# N.B. all instances of TheanoConfigParser give access to the same properties.
config = TheanoConfigParser()
......
......@@ -4,6 +4,7 @@ This is not used currently very used. It appear in some case, but I'm not sure i
It could help the current system to make it detect problem earlier when contructing the graph instead of during optimization.
"""
import sys
import theano
from theano import gof
def ishape(v):
......@@ -35,7 +36,7 @@ class Apply(gof.Apply):
try:
oshapes = infer_shape(self, ishapes)
except NotImplementedError:
except theano.tensor.ShapeError:
return
for o, oshp in zip(outputs, oshapes):
......
......@@ -7,6 +7,7 @@ from copy import copy
import re #for set_compiledir
import os, sys, StringIO
if sys.version_info[:2] >= (2,5):
import hashlib
def hash_from_code(msg):
......@@ -16,6 +17,13 @@ else:
def hash_from_code(msg):
return md5.new(msg).hexdigest()
def hash_from_file(file_path):
"""Return the MD5 hash of a file."""
return hash_from_code(open(file_path, 'rb').read())
import theano
from theano.gof.python25 import all
from theano import config
......@@ -43,6 +51,7 @@ import cmodule
import logging
_logger=logging.getLogger("theano.gof.cc")
_logger.setLevel(logging.WARN)
def info(*args):
_logger.info(' '.join(str(a) for a in args))
def debug(*args):
......@@ -791,7 +800,7 @@ class CLinker(link.Linker):
The key returned by this function is of the form (version, signature)
The signature has the following form:
{{{
'CLinker.cmodule_key', compilation args, libraries,
'CLinker.cmodule_key', compilation args, libraries, config md5,
(op0, input_signature0, output_signature0),
(op1, input_signature1, output_signature1),
...
......@@ -858,10 +867,16 @@ class CLinker(link.Linker):
constant_ids = dict()
op_pos = {} # Apply -> topological position
# first we put the header, compile_args, library names into the signature
# First we put the header, compile_args, library names and config md5
# into the signature.
sig = ['CLinker.cmodule_key'] # will be cast to tuple on return
if compile_args is not None: sig.append(tuple(compile_args))
if libraries is not None: sig.append(tuple(libraries))
# IMPORTANT: The 'md5' prefix is used to isolate the compilation
# parameters from the rest of the key. If you want to add more key
# elements, they should be before this md5 hash if and only if they
# can lead to a different compiled file with the same source code.
sig.append('md5:' + theano.configparser.get_config_md5())
# technically this should only be appended for gcc-compiled Ops
# and the flags of other compilers should be inserted here... but it's not clear how to
......
差异被折叠。
......@@ -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)
......@@ -589,6 +589,35 @@ class GPU_mrg_uniform(mrg_uniform_base):
def c_code_cache_version(self):
return (5,)
def guess_n_streams(size, warn=True):
"""
Return a guess at a good number of streams.
:param warn: If True, warn when a guess cannot be made (in which case
we return 30 * 256).
"""
# TODO: a smart way of choosing the number of streams, see #612.
# Note that this code was moved out of `MRG_RandomStreams` so that it can
# be easily accessed from tests, where we want to disable the warning.
if (isinstance(size, (tuple, list)) and
all([isinstance(i, int) for i in size])):
# We can make a guess.
r = 1
for s in size:
r *= s
if r > 6:
r = r/6 # chosen as fastest for rbm_benchmark
return r
else:
if warn:
assert False
print >> sys.stderr, (
"MRG_RandomStreams Can't determine #streams from "
"size (%s), guessing 30*256") % str(size)
return 30 * 256
class MRG_RandomStreams(object):
"""Module component with similar interface to numpy.random (numpy.random.RandomState)"""
......@@ -654,18 +683,7 @@ class MRG_RandomStreams(object):
return rval
def n_streams(self, size):
# TODO: a smart way of choosing the number of streams, see #612.
if isinstance(size, (tuple, list)) and all([isinstance(i,int) for i in size]):
r = 1
for s in size:
r *= s
if r > 6:
r = r/6 # chosen as fastest for rbm_benchmark
return r
print >> sys.stderr, ("MRG_RandomStreams Can't determine #streams from "
"size (%s), guessing 30*256")%str(size)
return 30*256
return guess_n_streams(size, warn=True)
def pretty_return(self, node_rstate, new_rstate, sample):
sample.rstate = node_rstate
......@@ -674,7 +692,8 @@ class MRG_RandomStreams(object):
node_rstate.default_update = new_rstate
return sample
def uniform(self, size=None, low=0.0, high=1.0, ndim=None, dtype=config.floatX, nstreams=None):
def uniform(self, size, low=0.0, high=1.0, ndim=None, dtype='floatX',
nstreams=None):
"""
Sample a tensor of given size whose element from a uniform
distribution between low and high.
......@@ -683,10 +702,14 @@ class MRG_RandomStreams(object):
ndim may be a plain integer to supplement the missing
information.
:param: size: Can be a list of integer or Theano variable
:param size: Can be a list of integer or Theano variable
(ex: the shape of other Theano Variable)
TODO: can size be None?
:param dtype: The output data type.
"""
if dtype == 'floatX':
dtype = config.floatX
if isinstance(size, tuple):
msg = "size must be a tuple of int or a Theano variable"
assert all([isinstance(i,int) or isinstance(i,Variable)
......@@ -728,16 +751,19 @@ class MRG_RandomStreams(object):
raise NotImplementedError( 'Increase the size to match the broadcasting pattern of `low` and `high` arguments')
return r
def binomial(self, size=None, n=1, p=0.5, ndim=None, dtype='int64'):
def binomial(self, size=None, n=1, p=0.5, ndim=None, dtype='int64',
nstreams=None):
if n == 1:
if dtype=='float32' and self.use_cuda:
return cast(self.uniform(size=size, dtype=dtype) < p, dtype)
if dtype == 'float32' and self.use_cuda:
x = self.uniform(size=size, dtype=dtype, nstreams=nstreams)
else:
return cast(self.uniform(size=size) < p, dtype)
x = self.uniform(size=size, nstreams=nstreams)
return cast(x < p, dtype)
else:
raise NotImplementedError("MRG_RandomStreams.binomial with n > 1")
def multinomial(self, size=None, n=1, pvals=None, ndim=None, dtype='int64'):
def multinomial(self, size=None, n=1, pvals=None, ndim=None, dtype='int64',
nstreams=None):
"""
Sample `n` (currently `n` needs to be 1) times from a multinomial
distribution defined by probabilities pvals.
......@@ -758,22 +784,31 @@ class MRG_RandomStreams(object):
ndim, size, pvals[:,0])
assert ndim==1
bcast = bcast+(pvals.type.broadcastable[-1],)
unis = self.uniform(size=size, ndim=1)
unis = self.uniform(size=size, ndim=1, nstreams=nstreams)
op = multinomial.MultinomialFromUniform(dtype)
return op(pvals, unis)
else:
raise NotImplementedError(("MRG_RandomStreams.multinomial only"
" implemented with n == 1 and pvals.ndim = 2"))
def normal(self, size=None, avg=0.0, std=1.0, ndim=None, dtype=config.floatX):
def normal(self, size=None, avg=0.0, std=1.0, ndim=None,
dtype='floatX', nstreams=None):
"""
:param: size: Can be a list of integer or Theano variable(ex: the shape of other Theano Variable)
:param size: Can be a list of integers or Theano variables (ex: the
shape of another Theano Variable)
:param dtype: The output data type.
:param nstreams: Number of streams.
"""
# We need an even number of ]0,1[ samples. Then we split them
# in two halves. First half becomes our U1's for Box-Muller,
# second half our U2's. See Wikipedia page:
# http://en.wikipedia.org/wiki/Box%E2%80%93Muller_transform
if dtype == 'floatX':
dtype = config.floatX
evened = False
constant = False
if isinstance(size, tuple) and all([isinstance(i,int) for i in size]):
......@@ -786,14 +821,15 @@ class MRG_RandomStreams(object):
else:
#if even, don't change, if odd, +1
n_samples = prod(size)+(prod(size)%2)
flattened = self.uniform(size=(n_samples,), dtype=dtype)
flattened = self.uniform(size=(n_samples,), dtype=dtype,
nstreams=nstreams)
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))
......
......@@ -350,7 +350,9 @@ def test_uniform():
print 'ON CPU with size=(%s):'%str(size)
x = tensor.matrix()
R = MRG_RandomStreams(234, use_cuda=False)
u = R.uniform(size=size)
# Note: we specify `nstreams` to avoid a warning.
u = R.uniform(size=size,
nstreams=rng_mrg.guess_n_streams(size, warn=False))
f = theano.function(var_input, u, mode=mode)
assert any([isinstance(node.op,theano.sandbox.rng_mrg.mrg_uniform)
for node in f.maker.env.toposort()])
......@@ -366,7 +368,8 @@ def test_uniform():
print ''
print 'ON GPU with size=(%s):'%str(size)
R = MRG_RandomStreams(234, use_cuda=True)
u = R.uniform(size=size, dtype='float32')
u = R.uniform(size=size, dtype='float32',
nstreams=rng_mrg.guess_n_streams(size, warn=False))
assert u.dtype == 'float32' #well, it's really that this test w GPU doesn't make sense otw
f = theano.function(var_input, theano.Out(
theano.sandbox.cuda.basic_ops.gpu_from_host(u),
......@@ -421,7 +424,9 @@ def test_binomial():
print ''
print 'ON CPU with size=(%s) and mean(%d):'%(str(size),mean)
R = MRG_RandomStreams(234, use_cuda=False)
u = R.binomial(size=size, p=mean)
# Note: we specify `nstreams` to avoid a warning.
u = R.binomial(size=size, p=mean,
nstreams=rng_mrg.guess_n_streams(size, warn=False))
f = theano.function(var_input, u, mode=mode)
theano.printing.debugprint(f)
out = f(*input)
......@@ -433,7 +438,9 @@ def test_binomial():
print ''
print 'ON GPU with size=(%s) and mean(%d):'%(str(size),mean)
R = MRG_RandomStreams(234, use_cuda=True)
u = R.binomial(size=size, p=mean, dtype='float32')
u = R.binomial(size=size, p=mean, dtype='float32',
nstreams=rng_mrg.guess_n_streams(size,
warn=False))
assert u.dtype == 'float32' #well, it's really that this test w GPU doesn't make sense otw
f = theano.function(var_input, theano.Out(
theano.sandbox.cuda.basic_ops.gpu_from_host(u),
......@@ -478,7 +485,9 @@ def test_normal0():
print 'ON CPU:'
R = MRG_RandomStreams(234, use_cuda=False)
n = R.normal(size=size, avg=avg, std=std)
# Note: we specify `nstreams` to avoid a warning.
n = R.normal(size=size, avg=avg, std=std,
nstreams=rng_mrg.guess_n_streams(size, warn=False))
f = theano.function(var_input, n, mode=mode)
theano.printing.debugprint(f)
out = f(*input)
......@@ -491,7 +500,8 @@ def test_normal0():
print ''
print 'ON GPU:'
R = MRG_RandomStreams(234, use_cuda=True)
n = R.normal(size=size, avg=avg, std=std, dtype='float32')
n = R.normal(size=size, avg=avg, std=std, dtype='float32',
nstreams=rng_mrg.guess_n_streams(size, warn=False))
assert n.dtype == 'float32' #well, it's really that this test w GPU doesn't make sense otw
f = theano.function(var_input, theano.Out(
theano.sandbox.cuda.basic_ops.gpu_from_host(n),
......@@ -557,7 +567,8 @@ def test_multinomial():
pvals = numpy.asarray(numpy.random.uniform(size=sample_size))
pvals = numpy.apply_along_axis(lambda row : row/numpy.sum(row), 1, pvals)
R = MRG_RandomStreams(234, use_cuda=False)
m = R.multinomial(pvals=pvals, dtype=config.floatX)
# Note: we specify `nstreams` to avoid a warning.
m = R.multinomial(pvals=pvals, dtype=config.floatX, nstreams=30 * 256)
f = theano.function([], m, mode=mode_)
theano.printing.debugprint(f)
out = f()
......
......@@ -12,8 +12,9 @@ If you want to use a scalar variable in a Theano graph,
you probably want to use theano.tensor.[c,z,f,d,b,w,i,l,]scalar!
"""
import math
import math, warnings
from copy import copy
from itertools import imap
import numpy, theano
......@@ -26,11 +27,37 @@ builtin_complex = complex
builtin_int = int
builtin_float = float
class ComplexError(Exception):
"""Raised if complex numbers are used in an unsupported operation."""
pass
class IntegerDivisionError(Exception):
"""Raised if someone tries to divide integers with '/' instead of '//'."""
pass
def upcast(dtype, *dtypes):
z = numpy.zeros((), dtype = dtype)
for dtype in dtypes:
z = z + numpy.zeros((), dtype = dtype)
return str(z.dtype)
# 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.
# Note that we store this boolean as a one-element list so that it can be
# modified within `make_array`.
keep_float32 = [(config.cast_policy == 'numpy+floatX' and
config.floatX == 'float32')]
def make_array(dt):
if dt == 'float64':
# There is an explicit float64 dtype: we cannot keep float32.
keep_float32[0] = False
return numpy.zeros((), dtype=dt)
z = make_array(dtype)
for dt in dtypes:
z = z + make_array(dt=dt)
rval = str(z.dtype)
if rval == 'float64' and keep_float32[0]:
return 'float32'
else:
return rval
def as_scalar(x, name = None):
if isinstance(x, gof.Apply):
......@@ -47,6 +74,7 @@ def as_scalar(x, name = None):
except TypeError:
raise TypeError("Cannot convert %s to Scalar" % x, type(x))
def constant(x):
# pass through numpy scalars, since they are already typed on purpose typically.
if hasattr(x,'dtype'):
......@@ -383,8 +411,9 @@ uint_types = uint8, uint16, uint32, uint64
float_types = float32, float64
complex_types = complex64, complex128
discrete_types = int_types + uint_types
continuous_types = float_types + complex_types
class _scalar_py_operators:
#UNARY
......@@ -416,7 +445,8 @@ class _scalar_py_operators:
def __sub__(self,other): return sub(self,other)
def __mul__(self,other): return mul(self,other)
def __div__(self,other): return div_proxy(self,other)
def __mod__(self,other): return mod(self,other)
def __floordiv__(self, other): return int_div(self, other)
def __mod__(self, other): return mod_check(self, other)
def __pow__(self,other): return pow(self,other)
#ARITHMETIC - RIGHT-OPERAND
......@@ -994,32 +1024,74 @@ class Sub(BinaryScalarOp):
return first_part, second_part
sub = Sub(upcast_out, name = 'sub')
def div_proxy(x, y):
"""Proxy for either true_div or int_div, depending on types of x, y.
def int_or_true_div(x_discrete, y_discrete):
"""
Return 'int' or 'true' depending on the type of division used for x / y.
:param x_discrete: True if `x` is discrete ([unsigned] integer).
:param y_discrete: True if `x` is discrete ([unsigned] integer).
:returns: 'int' if `x / y` should be an integer division, or `true` if it
should be a true division.
Raises an IntegerDivisionError if both `x_discrete` and `y_discrete` are
True and `config.int_division` is set to 'raise'.
This function is used by both scalar/basic.py and tensor.basic/py.
"""
if as_scalar(x).type.dtype.startswith('int') and as_scalar(y).type.dtype.startswith('int'):
return int_div(x, y)
if (x_discrete and y_discrete):
if config.int_division == 'raise':
raise IntegerDivisionError(
"With `config.int_division` set to 'raise', dividing two "
"integer types with '/' is forbidden to avoid confusion "
"between integer and floating point divisions. Please "
"use // for integer division, or if you want a float result "
"either cast one of the arguments to a float or directly call "
"`x.__truediv__(y)`.")
elif config.int_division == 'int':
warnings.warn(
"Division of two integer types with x / y is deprecated, "
"please use x // y for an integer division "
"(set `config.int_division = raise` to track the origin "
"of this warning)",
DeprecationWarning)
return 'int'
elif config.int_division == 'floatX':
return 'true'
else:
raise NotImplementedError(config.int_division)
else:
return true_div(x, y)
return 'true'
def div_proxy(x, y):
"""Proxy for either true_div or int_div, depending on types of x, y."""
f = eval('%s_div' % int_or_true_div(as_scalar(x).type in discrete_types,
as_scalar(y).type in discrete_types))
return f(x, y)
class TrueDiv(BinaryScalarOp):
def output_types(self, types):
if all(t not in continuous_types for t in types):
return [float64]
if all(t in discrete_types for t in types):
return [Scalar(config.floatX)]
else:
return super(TrueDiv, self).output_types(types)
def impl(self, x, y):
x = numpy.asarray(x)
y = numpy.asarray(y)
if str(x.dtype).startswith('int') and str(y.dtype).startswith('int'):
return float(x) / y
if all(a.dtype in discrete_types for a in (x, y)):
return numpy.array(float(x) / y, dtype=config.floatX)
else:
return x / y
def c_code(self, node, name, (x, y), (z, ), sub):
#we generate good c code only when both are complex!
if sum([node.inputs[0].type in complex_types, node.inputs[1].type in complex_types])==1:
raise NotImplementedError('type not supported', type)
if node.inputs[0].type in int_types and node.inputs[1].type in int_types:
if (node.inputs[0].type in discrete_types and
node.inputs[1].type in discrete_types):
return "%(z)s = ((double)%(x)s) / %(y)s;" % locals()
return "%(z)s = %(x)s / %(y)s;" % locals()
def grad(self, (x, y), (gz, )):
......@@ -1028,11 +1100,15 @@ class TrueDiv(BinaryScalarOp):
if x.type in float_types:
first_part = cast(gz / y, x.type.dtype)
else:
assert x.type in discrete_types
first_part = None
if y.type in complex_types:
raise NotImplementedError()
if y.type in float_types:
second_part = cast(-(gz * x) / (y * y), y.type.dtype)
else:
assert y.type in discrete_types
second_part = None
return first_part, second_part
true_div = TrueDiv(upcast_out, name = 'true_div')
......@@ -1048,9 +1124,29 @@ int_div = IntDiv(upcast_out, name = 'int_div')
floor_div = int_div
def raise_complex_error():
raise ComplexError(
"Theano does not support the mod operator (%) on "
"complex numbers, since numpy deprecated it.")
def mod_check(x, y):
if (as_scalar(x).type in complex_types or
as_scalar(y).type in complex_types):
# Currently forbidden.
raise_complex_error()
else:
return mod(x, y)
class Mod(BinaryScalarOp):
def impl(self, x, y):
if isinstance(x, numpy.complex) or isinstance(y, numpy.complex):
raise_complex_error()
return x % y
def c_code_cache_version(self):
return (5,)
......@@ -1060,20 +1156,34 @@ class Mod(BinaryScalarOp):
def c_code(self, node, name, (x, y), (z, ), sub):
"""
We want the result to have the same sign as python, not the other implementaiton of mod.
We want the result to have the same sign as python, not the other implementation of mod.
"""
#raise NotImplementedError("Unlike Python, C's modulo returns negative modulo on negative dividend (to implement)")
t = node.inputs[0].type.upcast(*[ i.type for i in node.inputs[1:]])
if t in int_types or t in ['uint8','int8','uint16','int16','uint32','int32','uint64','int64']:
if (str(t) in imap(str, discrete_types) or
t in ['uint8','int8','uint16','int16','uint32','int32','uint64','int64'] or
t in discrete_types):
# The above or's should not be needed anymore. However, for now we
# keep them out of safety, and verify they are useless with an
# assert.
assert str(t) in imap(str, discrete_types)
x_mod_y = "THEANO_MACRO_MOD(%(x)s, %(y)s)"%locals()
x_mod_ymm = "THEANO_MACRO_MOD(-%(x)s, -%(y)s)"%locals()
x_mod_ypm = "THEANO_MACRO_MOD(%(x)s, -%(y)s)"%locals()
x_mod_ymp = "THEANO_MACRO_MOD(-%(x)s, %(y)s)"%locals()
elif t in float_types or t in ['float32','float64']:
elif (str(t) in imap(str, float_types) or
t in ['float32','float64'] or
t in float_types):
# The above or's should not be needed anymore. However, for now we
# keep them out of safety, and verify they are useless with an
# assert.
assert str(t) in imap(str, float_types)
x_mod_y = "fmod(%(x)s,%(y)s)"%locals()
x_mod_ymm = "fmod(-%(x)s,-%(y)s)"%locals()
x_mod_ypm = "fmod(%(x)s,-%(y)s)"%locals()
x_mod_ymp = "fmod(-%(x)s,%(y)s)"%locals()
elif str(t) in imap(str, complex_types):
raise_complex_error()
else:
raise NotImplementedError('type not supported', type)
......
......@@ -37,6 +37,7 @@ class test_ScalarOps(unittest.TestCase):
#As we use theano.scalar normally, but we use theano.tensor.scalar
#that is not important. Also this make the theano fct fail at call time
#so this is not a silent bug.
# --> This is why it is purposedly named 'tes_mod' instead of 'test_mod'.
def tes_mod(self):
"""
We add this test as not all language and C implementation give the same
......@@ -174,6 +175,19 @@ class test_logical(unittest.TestCase):
self.assertTrue(fn(a,b) == ~a, (a,))
class test_complex_mod(unittest.TestCase):
"""Make sure % fails on complex numbers."""
def test_fail(self):
x = complex64()
y = int32()
try:
x % y
assert False
except ComplexError:
pass
class test_div(unittest.TestCase):
def test_0(self):
a = int8()
......@@ -182,9 +196,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)
......
差异被折叠。
......@@ -454,7 +454,7 @@ class Elemwise(Op):
"""
inputs = map(as_tensor_variable, inputs)
shadow = self.scalar_op.make_node(*[Scalar(dtype = t.type.dtype)() for t in inputs])
shadow = self.scalar_op.make_node(*[Scalar(dtype=i.type.dtype)() for i in inputs])
target_length = max([input.type.ndim for input in inputs])
......
......@@ -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 )
......
......@@ -575,14 +575,15 @@ class ConvOp(Op):
try:
fmshp = ConvOp.getOutputShape(imshp[1:], kshp, (self.dx,self.dy), self.out_mode)
except TypeError:
raise NotImplementedError()
raise theano.tensor.ShapeError()
outshp = (batch_size,fmo) + tuple(fmshp)
return [outshp]
else:
# Haven't implemented this case. imshp and kshp may be symbollic
# and ConvOp.getOutputShape doesn't handle this. In this case
# we simply let the default function do its work.
raise NotImplementedError()
raise theano.tensor.ShapeError()
def perform(self,node, inp, out):
"""
......
......@@ -879,6 +879,7 @@ def test_argmax_pushdown():
[x],
[out])
config.warn.argmax_pushdown_bug = False
theano.compile.mode.optdb.query(
theano.compile.mode.OPT_FAST_RUN).optimize(env)
......@@ -922,6 +923,7 @@ def test_argmax_pushdown_bias():
[x,b],
[out])
config.warn.argmax_pushdown_bug = False
theano.compile.mode.optdb.query(
theano.compile.mode.OPT_FAST_RUN).optimize(env)
......
......@@ -28,11 +28,12 @@ from theano import compile #to register the optimizer built by this file
from theano.gof.python25 import any, all
from theano.gof.opt import Optimizer, pre_constant_merge, pre_greedy_local_optimizer
from theano.gof import toolbox, DestroyHandler
from basic import get_constant_value
from basic import get_constant_value, ShapeError
# Utilities
def out2in(*local_opts):
"""WRITEME """
return opt.TopoOptimizer(opt.LocalOptGroup(*local_opts),
......@@ -529,7 +530,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::
......@@ -728,8 +729,15 @@ 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.ShapeError '
'instead. The original exception message is: %s' % e)
except Exception, e:
_logger.error('Failed to infer_shape from Op %s.\nInput shapes:%s\nException encountered during infer_shape: %s\nException message: %s\nTraceback: %s'% (node.op,
[self.shape_of[r] for r in node.inputs],
......@@ -3431,11 +3439,12 @@ def local_elemwise_fusion_op(OP, max_input_fct=lambda node: 1024):
"""
def local_fuse(node):
"""
As part of specialisation, we fuse two consecutive elemwise op of the same shape.
For mixed dtype, we let the Compise op do the cast. It let the C compile do the cast.
The number of dimension is validated at call time by theano itself.
As part of specialization, we fuse two consecutive elemwise Ops of the
same shape.
For mixed dtype, we let the Composite op do the cast. It lets the C
compiler do the cast.
The number of dimensions is validated at call time by theano itself.
"""
# META TODO: PUT THESE THINGS IN TRAC, NOT TODO NOTES!!
# TODO: use broadcast flag?
......@@ -3551,7 +3560,7 @@ def local_elemwise_fusion_op(OP, max_input_fct=lambda node: 1024):
if new_nb_input != len(inputs) or len(s_inputs) != len(inputs):
raise Exception("""Something has gone wrong with the elemwise
fusion optimization. We skip this optimization. You can ignore this message,
your code will run correctly, but maybe slower.""")
your code will run correctly, but may be slower.""")
otype = node.outputs[0].type
s_new_out=node.op.scalar_op(*s_g)
......
......@@ -30,9 +30,11 @@ class Test_incsubtensor(unittest.TestCase):
for do_set in [False,True]:
if do_set:
resut = T.setsubtensor(a, increment, [sl1, sl2])
resut = T.setsubtensor(a, increment, [sl1, sl2],
show_warning=False)
else:
resut = T.incsubtensor(a, increment, [sl1, sl2])
resut = T.incsubtensor(a, increment, [sl1, sl2],
show_warning=False)
f = theano.function([a, increment, sl2_end], resut)
......@@ -59,7 +61,7 @@ class Test_incsubtensor(unittest.TestCase):
def inc_slice(*s):
def just_numeric_args(a,b):
return T.incsubtensor(a, b, s)
return T.incsubtensor(a, b, s, show_warning=False)
return just_numeric_args
# vector
......
......@@ -647,10 +647,14 @@ 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)
assert False
except theano.scalar.IntegerDivisionError:
pass
def test_const_type_in_mul_canonizer():
input = dmatrix()
......@@ -2487,6 +2491,7 @@ class T_local_sum(unittest.TestCase):
assert numpy.allclose(f(input),input.sum())
config.warn.sum_sum_bug = False
f = theano.function([a],a.sum(0).sum(0).sum(0),mode=self.mode)
assert len(f.maker.env.nodes)==1
assert numpy.allclose(f(input),input.sum())
......@@ -2496,6 +2501,7 @@ class T_local_sum(unittest.TestCase):
input=numpy.arange(3*3*3, dtype=config.floatX).reshape(3,3,3)
dims=[(0,0),(1,0),(2,0),(0,1),(1,1),(2,1)]
config.warn.sum_sum_bug = False
for d,dd in dims:
f = theano.function([a],a.sum(d).sum(dd),mode=self.mode)
assert numpy.allclose(f(input),input.sum(d).sum(dd))
......@@ -2541,6 +2547,7 @@ class T_local_sum(unittest.TestCase):
assert len(f.maker.env.nodes)==nb_nodes[2]
assert f.maker.env.toposort()[-1].op==T.alloc
config.warn.sum_sum_bug = False
for d, dd in [(0,0),(1,0),(2,0),(0,1),(1,1),(2,1)]:
f = theano.function([a],t_like(a).sum(d).sum(dd),mode=mode)
print f.maker.env.toposort()
......@@ -2600,6 +2607,8 @@ class T_local_sum_dimshuffle(unittest.TestCase):
c_val = rng.randn(2,2,2).astype(config.floatX)
d_val = numpy.asarray(rng.randn(), config.floatX)
config.warn.sum_sum_bug = False
config.warn.sum_div_dimshuffle_bug = False
for i,s in enumerate(sums):
print i
f = theano.function([a,b,c,d], s, mode=self.mode)
......
""" 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 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论