提交 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,6 +557,13 @@ class Function(object): ...@@ -549,6 +557,13 @@ class Function(object):
t0 = time.time() t0 = time.time()
# Reinitialize each container's 'provided' counter # Reinitialize each container's 'provided' counter
if self.trust_input:
i = 0
for arg in args:
s = self.input_storage[i]
s.storage[0] = arg
i += 1
else:
for c in self.input_storage: for c in self.input_storage:
c.provided = 0 c.provided = 0
...@@ -589,7 +604,8 @@ class Function(object): ...@@ -589,7 +604,8 @@ class Function(object):
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,7 +645,8 @@ class Function(object): ...@@ -629,7 +645,8 @@ 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
if not self.trust_input:
for c in self.input_storage: for c in self.input_storage:
if c.required and not c.provided: if c.required and not c.provided:
raise TypeError("Missing required input: %s" % raise TypeError("Missing required input: %s" %
...@@ -640,7 +657,8 @@ class Function(object): ...@@ -640,7 +657,8 @@ class Function(object):
getattr(self.inv_finder[c], 'variable', getattr(self.inv_finder[c], 'variable',
self.inv_finder[c])) self.inv_finder[c]))
if c.implicit and c.provided > 0: if c.implicit and c.provided > 0:
raise TypeError('Tried to provide value for implicit input: %s' 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]))
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论