提交 6978e564 authored 作者: James Bergstra's avatar James Bergstra

compile.sharedvar - added get_value and set_value methods

The get_value and set_value have a borrow parameter that controls whether data is copied when moving it between user-managed values and theano-managed values. The .value property is uses these new methods with borrow=False.
上级 1a06538d
...@@ -64,11 +64,37 @@ class SharedVariable(Variable): ...@@ -64,11 +64,37 @@ class SharedVariable(Variable):
readonly=False, readonly=False,
strict=strict) strict=strict)
def __set(self,new_value): def get_value(self, borrow=False):
self.container.value = new_value """Get the non-symbolic value associated with this SharedVariable.
:param borrow:
True to return the internal value directly, potentially creating problems related
to aliased memory.
If the return value is mutable, and you have used borrow=True to get at the internal
value, then you should be careful about changing it. If you modify it, call
set_value(rval, borrow=True) to tell Theano that you modified it. (Theano may have
cached computations based on the old value.)
"""
if borrow:
return self.container.value
else:
return copy.deepcopy(self.container.value)
def __get(self): def set_value(self,new_value, borrow=False):
return self.container.value """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.
"""
if borrow:
self.container.value = new_value
else:
self.container.value = copy.deepcopy(new_value)
def clone(self): def clone(self):
cp = self.__class__( cp = self.__class__(
...@@ -80,16 +106,9 @@ class SharedVariable(Variable): ...@@ -80,16 +106,9 @@ class SharedVariable(Variable):
cp.tag = copy.copy(self.tag) cp.tag = copy.copy(self.tag)
return cp return cp
value = property(__get, __set) value = property(get_value, set_value,
#value = self.container.value #GD- would've thought mapping one property to another would work doc="shortcut for self.get_value() and self.set_value() which COPIES data")
"""Read/write the non-symbolic value associated with this SharedVariable.
If the SharedVariable is shared, changes to this value will be visible to all functions using
this SharedVariable. If this SharedVariable is not shared, a change will not be visible to
functions that were created before the change.
"""
def filter_update(self, update): def filter_update(self, update):
"""When this shared variable is updated by a pfunc, the update value will be run through this function. """When this shared variable is updated by a pfunc, the update value will be run through this function.
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论