提交 fe38ef37 authored 作者: Frederic's avatar Frederic

Add the theano_function.trust_input member that allow to skip check of the input…

Add the theano_function.trust_input member that allow to skip check of the input on all functions call.
上级 aeedaa73
...@@ -280,6 +280,13 @@ class Function(object): ...@@ -280,6 +280,13 @@ class Function(object):
A Function instance may be serialized using the `pickle` or `cPickle` modules. A Function instance may be serialized using the `pickle` or `cPickle` modules.
This will save all default inputs, the graph, and *** to the pickle file (WRITEME). This will save all default inputs, the graph, and *** to the pickle file (WRITEME).
A Function instance have a ``trust_input`` field that default to
False. When True, we don't do extra check of the input to give
better error message. In some case, python code will still return
the good results if you pass a python or numpy scalar instead of a
numpy tensor. C code should raise an error if you pass an object
of the wrong type.
""" """
pickle_aliased_memory_strategy = 'warn' pickle_aliased_memory_strategy = 'warn'
...@@ -367,6 +374,7 @@ class Function(object): ...@@ -367,6 +374,7 @@ class Function(object):
self.return_none = return_none self.return_none = return_none
self.maker = maker self.maker = maker
self.profile = None # reassigned in FunctionMaker.create self.profile = None # reassigned in FunctionMaker.create
self.trust_input = False # If True, we don't check the input parameter
# We will be popping stuff off this `containers` object. It is a copy. # We will be popping stuff off this `containers` object. It is a copy.
containers = list(self.input_storage) containers = list(self.input_storage)
...@@ -549,47 +557,55 @@ class Function(object): ...@@ -549,47 +557,55 @@ class Function(object):
t0 = time.time() t0 = time.time()
# Reinitialize each container's 'provided' counter # Reinitialize each container's 'provided' counter
for c in self.input_storage: if self.trust_input:
c.provided = 0 i = 0
for arg in args:
if len(args) + len(kwargs) > len(self.input_storage): s = self.input_storage[i]
raise TypeError("Too many parameter passed to theano function")
# Set positional arguments
i = 0
for arg in args:
#TODO: provide a Param option for skipping the filter if we
# really want speed.
s = self.input_storage[i]
# see this emails for a discuation about None as input
# https://groups.google.com/group/theano-dev/browse_thread/thread/920a5e904e8a8525/4f1b311a28fc27e5
if arg is None:
s.storage[0] = arg s.storage[0] = arg
else: i += 1
try: else:
s.storage[0] = s.type.filter(arg, strict=s.strict, for c in self.input_storage:
allow_downcast=s.allow_downcast) c.provided = 0
except Exception, e: if len(args) + len(kwargs) > len(self.input_storage):
function_name = "theano function" raise TypeError("Too many parameter passed to theano function")
if self.name:
function_name += 'with name "' + self.name + '" ' # Set positional arguments
#end if i = 0
e.args = tuple(["Bad input argument to " + function_name + for arg in args:
" at index %d(0-based)" % i] + #TODO: provide a Param option for skipping the filter if we
list(e.args)) # really want speed.
raise s = self.input_storage[i]
#end except # see this emails for a discuation about None as input
#end if # https://groups.google.com/group/theano-dev/browse_thread/thread/920a5e904e8a8525/4f1b311a28fc27e5
s.provided += 1 if arg is None:
i += 1 s.storage[0] = arg
else:
try:
s.storage[0] = s.type.filter(arg, strict=s.strict,
allow_downcast=s.allow_downcast)
except Exception, e:
function_name = "theano function"
if self.name:
function_name += 'with name "' + self.name + '" '
#end if
e.args = tuple(["Bad input argument to " + function_name +
" at index %d(0-based)" % i] +
list(e.args))
raise
#end except
#end if
s.provided += 1
i += 1
# Set keyword arguments # Set keyword arguments
if kwargs: # for speed, skip the iteritems for empty kwargs if kwargs: # for speed, skip the iteritems for empty kwargs
for k, arg in kwargs.iteritems(): for k, arg in kwargs.iteritems():
self[k] = arg self[k] = arg
if (not hasattr(self, '_check_for_aliased_inputs') or if not self.trust_input and (
not hasattr(self, '_check_for_aliased_inputs') or
self._check_for_aliased_inputs): self._check_for_aliased_inputs):
## Collect aliased inputs among the storage space ## Collect aliased inputs among the storage space
args_share_memory = [] args_share_memory = []
...@@ -629,20 +645,22 @@ class Function(object): ...@@ -629,20 +645,22 @@ class Function(object):
self.input_storage[i].storage[0]) self.input_storage[i].storage[0])
# Check if inputs are missing, or if inputs were set more than once, or # Check if inputs are missing, or if inputs were set more than once, or
# if we tried to provide inputs that are supposed to be implicit. # if we tried to provide inputs that are supposed to be
for c in self.input_storage: if not self.trust_input:
if c.required and not c.provided: for c in self.input_storage:
raise TypeError("Missing required input: %s" % if c.required and not c.provided:
getattr(self.inv_finder[c], 'variable', raise TypeError("Missing required input: %s" %
self.inv_finder[c])) getattr(self.inv_finder[c], 'variable',
if c.provided > 1: self.inv_finder[c]))
raise TypeError("Multiple values for input: %s" % if c.provided > 1:
getattr(self.inv_finder[c], 'variable', raise TypeError("Multiple values for input: %s" %
self.inv_finder[c])) getattr(self.inv_finder[c], 'variable',
if c.implicit and c.provided > 0: self.inv_finder[c]))
raise TypeError('Tried to provide value for implicit input: %s' if c.implicit and c.provided > 0:
raise TypeError(
'Tried to provide value for implicit input: %s'
% getattr(self.inv_finder[c], 'variable', % getattr(self.inv_finder[c], 'variable',
self.inv_finder[c])) self.inv_finder[c]))
# Do the actual work # Do the actual work
t0_fn = time.time() t0_fn = time.time()
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论