提交 2f498b43 authored 作者: lamblin's avatar lamblin

Merge pull request #645 from nouiz/on_unused_input

New Theano flag on_unused_input that default the default value.
......@@ -42,6 +42,8 @@ Interface changes
instance, function([x, y], [y]). You can use the kwarg
``on_unused_input={'raise', 'warn', 'ignore'}`` to control this.
(Pascal L.)
* New Theano flag "on_unused_input" that define the default value of the
previous point. (Frederic B.)
* tensor.alloc() now raises an error during graph build time
when we try to create less dimensions than the number of dimensions
the provided value have. In the past, the error was at run time.
......
......@@ -2065,7 +2065,7 @@ class _Maker(FunctionMaker): # inheritance buys a few helper functions
accept_inplace = False,
function_builder = Function,
profile=None,
on_unused_input='raise'):
on_unused_input=None):
"""
:type inputs: a list of SymbolicInput instances
......
......@@ -14,7 +14,7 @@ from numpy import any #for to work in python 2.4
def function(inputs, outputs=None, mode=None, updates=None, givens=None,
no_default_updates=False, accept_inplace=False, name=None,
rebuild_strict=True, allow_input_downcast=None, profile=None,
on_unused_input='raise'):
on_unused_input=None):
"""
Return a callable object that will calculate `outputs` from `inputs`.
......@@ -70,7 +70,7 @@ def function(inputs, outputs=None, mode=None, updates=None, givens=None,
used. This profiling object will be available via self.profile.
:param on_unused_input: What to do if a variable in the 'inputs' list is
not used in the graph. Possible values are 'raise', 'warn', and 'ignore'.
not used in the graph. Possible values are 'raise', 'warn', 'ignore' and None.
:note: Regarding givens: Be careful to make sure that these substitutions are
independent--behaviour when Var1 of one pair appears in the graph leading to Var2 in
......
......@@ -965,7 +965,7 @@ class FunctionMaker(object):
def __init__(self, inputs, outputs,
mode = None, accept_inplace = False, function_builder = Function,
profile=None, on_unused_input='raise'):
profile=None, on_unused_input=None):
"""
:type inputs: a list of SymbolicInput instances
......@@ -982,9 +982,10 @@ class FunctionMaker(object):
:param on_unused_input: What to do if a variable in the 'inputs' list
is not used in the graph. Possible values are:
- 'raise' (default): raise an error
- 'raise': raise an error
- 'warn': log a warning
- 'ignore': do not do anything
- None: Use the value in the Theano flags on_unused_input
"""
mode = mode_module.get_mode(mode)
......@@ -1090,6 +1091,9 @@ class FunctionMaker(object):
for i in self.inputs]
def _check_unused_inputs(self, inputs, outputs, on_unused_input):
if on_unused_input is None:
on_unused_input = theano.config.on_unused_input
if on_unused_input == 'ignore':
return
......@@ -1256,7 +1260,7 @@ def register_checker(checker):
def orig_function(inputs, outputs, mode=None, accept_inplace=False,
name=None, profile=None, on_unused_input='raise'):
name=None, profile=None, on_unused_input=None):
"""
Return a Function that will calculate the outputs from the inputs.
......@@ -1290,7 +1294,8 @@ def orig_function(inputs, outputs, mode=None, accept_inplace=False,
:param profile: None or ProfileStats instance
:param on_unused_input: What to do if a variable in the 'inputs' list is
not used in the graph. Possible values are 'raise', 'warn', and 'ignore'.
not used in the graph. Possible values are 'raise', 'warn', 'ignore'
and None
"""
# Every element of the input list will be upgraded to an `In` instance if
......
......@@ -325,7 +325,7 @@ class Param(object):
def pfunc(params, outputs=None, mode=None, updates=None, givens=None,
no_default_updates=False, accept_inplace=False, name=None,
rebuild_strict=True, allow_input_downcast=None,
profile=None, on_unused_input='raise'):
profile=None, on_unused_input=None):
"""Function-constructor for graphs with shared variables.
:type params: list of either Variable or Param instances.
......@@ -375,8 +375,8 @@ def pfunc(params, outputs=None, mode=None, updates=None, givens=None,
:type on_unused_input: str
:param on_unused_input: What to do if a variable in the 'inputs' list
is not used in the graph. Possible values are 'raise', 'warn', and
'ignore.
is not used in the graph. Possible values are 'raise', 'warn',
'ignore' and None.
:rtype: theano.compile.Function
......
......@@ -159,6 +159,11 @@ AddConfigVar('nocleanup',
BoolParam(False),
in_c_key=False)
AddConfigVar('on_unused_input',
"What to do if a variable in the 'inputs' list of "
" theano.function() is not used in the graph.",
EnumStr('raise' 'warn', 'ignore'),
in_c_key=False)
# This flag is used when we import Theano to initialize global variables.
# So changing it after import will not modify these global variables.
......
......@@ -2650,8 +2650,6 @@ def ones_like(model, dtype=None):
"""equivalent of numpy.ones_like"""
if dtype is None:
dtype = model.type.dtype
if model.ndim == 0 and isinstance(model.type, TensorType):
return constant(1.0, dtype=dtype)
ret = fill(model, constant(1.0, dtype=dtype))
return ret
......@@ -2661,8 +2659,6 @@ def zeros_like(model, dtype=None):
"""equivalent of numpy.zeros_like"""
if dtype is None:
dtype = model.type.dtype
if model.ndim == 0 and isinstance(model.type, TensorType):
return constant(0.0, dtype=dtype)
return fill(model, constant(0.0, dtype=dtype))
......
......@@ -1078,6 +1078,25 @@ def local_fill_to_alloc(node):
return rval
@gof.local_optimizer([T.fill])
def local_useless_fill(node):
"""fill(s,v) -> v
This optimization is only needed in FAST_COMPILE to make the code
more readable. Normally, it is done by the local_fill_to_alloc
opt.
"""
if node.op == T.fill:
r, v = node.inputs
if v.type == node.outputs[0].type:
# this is a useless fill, erase it.
return [v]
compile.optdb['canonicalize'].register('local_useless_fill',
in2out(local_useless_fill),
1.1, 'fast_compile')
@register_specialize
@register_stabilize
@register_canonicalize
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论