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

Merge pull request #498 from nouiz/add_err

Add err
......@@ -9,6 +9,8 @@ Documentation
(Frédéric B.)
Interface changes
* In 0.5, we removed the deprecated sharedvar.value property.
Now we raise an error if you access it.
* theano.function does not accept duplicate inputs, so function([x, x], ...)
does not work anymore. (Pascal L.)
* theano.function now raises an error if some of the provided inputs are
......
......@@ -4,16 +4,11 @@ __docformat__ = 'restructuredtext en'
# Standard imports
import copy
import logging
import traceback
import warnings
# Third-party imports
import numpy
# Theano imports
from theano import config
from theano.configparser import (TheanoConfigParser, AddConfigVar, EnumStr,
StrParam, IntParam, FloatParam, BoolParam)
from theano.gof import Container, Variable, generic
_logger = logging.getLogger('theano.compile.sharedvalue')
......@@ -21,14 +16,16 @@ _logger = logging.getLogger('theano.compile.sharedvalue')
class SharedVariable(Variable):
"""
Variable that is (defaults to being) shared between functions that it appears in.
Variable that is (defaults to being) shared between functions that
it appears in.
"""
#Container object
container = None
"""
A container to use for this SharedVariable when it is an implicit function parameter.
A container to use for this SharedVariable when it is an implicit
function parameter.
:type: `Container`
"""
......@@ -38,39 +35,44 @@ class SharedVariable(Variable):
# this Variable, unless another update value has been passed to "function",
# or the "no_default_updates" list passed to "function" contains it.
def __init__(self, name, type, value, strict, allow_downcast=None, container=None):
def __init__(self, name, type, value, strict,
allow_downcast=None, container=None):
"""
:param name: The name for this variable (see `Variable`).
:param type: The type for this variable (see `Variable`).
:param value: A value to associate with this variable (a new container will be created).
:param value: A value to associate with this variable (a new
container will be created).
:param strict: True -> assignments to .value will not be cast or copied, so they must
have the correct type.
:param strict: True -> assignments to .value will not be cast
or copied, so they must have the correct type.
:param allow_downcast: Only applies if `strict` is False.
True -> allow assigned value to lose precision when cast during assignment.
False -> never allow precision loss.
None -> only allow downcasting of a Python float to a scalar floatX.
:param container: The container to use for this variable. Illegal to pass this as well
as a value.
:param container: The container to use for this
variable. Illegal to pass this as well as a value.
:note: For more user-friendly constructor, see `shared`
"""
super(SharedVariable, self).__init__(type=type, name=name, owner=None, index=None)
super(SharedVariable, self).__init__(type=type, name=name,
owner=None, index=None)
if container is not None:
self.container = container
if (value is not None) or (strict is not None):
raise TypeError('value and strict are ignored if you pass a container here')
raise TypeError(
'value and strict are ignored if you pass a container here')
else:
if container is not None:
raise TypeError('Error to specify both value and container')
self.container = Container(self,
storage=[type.filter(value, strict=strict, allow_downcast=allow_downcast)],
storage=[type.filter(value, strict=strict,
allow_downcast=allow_downcast)],
readonly=False,
strict=strict,
allow_downcast=allow_downcast)
......@@ -78,15 +80,16 @@ class SharedVariable(Variable):
def get_value(self, borrow=False, return_internal_type=False):
"""Get the non-symbolic value associated with this SharedVariable.
:param borrow:
True to permit returning of an object aliased to internal memory.
:param return_internal_type:
True to permit the returning of an arbitrary type object used internally to store
the shared variable.
:param borrow: True to permit returning of an object aliased
to internal memory.
:param return_internal_type: True to permit the returning of
an arbitrary type object used internally to store the
shared variable.
Only with borrow=False and return_internal_type=True does this function guarantee that
you actually get the internal object. But in that case, you may get different return
types when using different compute devices.
Only with borrow=False and return_internal_type=True does this
function guarantee that you actually get the internal object.
But in that case, you may get different return types when
using different compute devices.
"""
if borrow:
......@@ -94,14 +97,15 @@ class SharedVariable(Variable):
else:
return copy.deepcopy(self.container.value)
def set_value(self,new_value, borrow=False):
def set_value(self, new_value, borrow=False):
"""Set the non-symbolic value associated with this SharedVariable.
:param borrow:
True to use the new_value directly, potentially creating problems
related to aliased memory.
Changes to this value will be visible to all functions using this SharedVariable.
Changes to this value will be visible to all functions using
this SharedVariable.
"""
if borrow:
self.container.value = new_value
......@@ -139,20 +143,38 @@ class SharedVariable(Variable):
"The generic 'SharedVariable' object is not subscriptable. "
"This shared variable contains %s" % msg)
def _value_get(self):
raise Exception("sharedvar.value does not exist anymore. Use "
"sharedvar.get_value() or sharedvar.get_value()"
" instead.")
def _value_set(self, new_value):
raise Exception("sharedvar.value does not exist anymore. Use "
"sharedvar.get_value() or sharedvar.get_value()"
" instead.")
# We keep this just to raise an error
value = property(_value_get, _value_set),
def shared_constructor(ctor):
shared.constructors.append(ctor)
def shared_constructor(ctor, remove=False):
if remove:
shared.constructors.remove(ctor)
else:
shared.constructors.append(ctor)
return ctor
def shared(value, name=None, strict=False, allow_downcast=None, **kwargs):
"""Return a SharedVariable Variable, initialized with a copy or reference of `value`.
"""Return a SharedVariable Variable, initialized with a copy or
reference of `value`.
This function iterates over constructor functions (see
`shared_constructor`) to find a suitable SharedVariable subclass.
This function iterates over constructor functions (see `shared_constructor`) to find a
suitable SharedVariable subclass.
:note: By passing kwargs, you effectively limit the set of
potential constructors to those that can accept those kwargs.
:note:
By passing kwargs, you effectively limit the set of potential constructors to those that
can accept those kwargs.
:note: Some shared variable have 'borrow' as extra kwargs.
`See <http://deeplearning.net/software/theano/tutorial/aliasing.html#borrowing-when-creating-shared-variables>`_ for detail.
......@@ -166,11 +188,13 @@ def shared(value, name=None, strict=False, allow_downcast=None, **kwargs):
# This may happen when kwargs were supplied
# if kwargs were given, the generic_constructor won't be callable.
#
# This was done on purpose, the rationale being that if kwargs were supplied,
# the user didn't want them to be ignored.
raise TypeError('No suitable SharedVariable constructor could be found', (value, kwargs))
# This was done on purpose, the rationale being that if kwargs
# were supplied, the user didn't want them to be ignored.
raise TypeError('No suitable SharedVariable constructor could be found',
(value, kwargs))
shared.constructors = []
@shared_constructor
def generic_constructor(value, name=None, strict=False, allow_downcast=None):
"""SharedVariable Constructor"""
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论