allow params_as_tensors_for() to take several objects as varargs
Allow params_as_tensors_for() to take several objects as varargs
@@ -102,12 +102,12 @@ class defer_build(contextlib.ContextDecorator):
"""
def __init__(self, defer=True):
- self.disable_build = not defer
+ self.defer = defer
self.prev_autobuild_status = None
def __enter__(self):
self.prev_autobuild_status = AutoBuildStatus.__autobuild_enabled_global__
- AutoBuildStatus.__autobuild_enabled_global__ = self.disable_build
+ AutoBuildStatus.__autobuild_enabled_global__ = not self.defer
def __exit__(self, *exc):
AutoBuildStatus.__autobuild_enabled_global__ = self.prev_autobuild_status
@@ -115,12 +115,13 @@ class defer_build(contextlib.ContextDecorator):
@contextlib.contextmanager
-def params_as_tensors_for(obj, convert=True):
+def params_as_tensors_for(*objs, convert=True):
"""
- Context manager which changes respresentation of parameters and data holders
- for specific parameterized object.
+ Context manager which changes the representation of parameters and data holders
+ for the specific parameterized object(s).
- User can turn off tensor conversion inside `params_as_tensors` wrapped function.
+ This can also be used to turn off tensor conversion functions wrapped with
+ `params_as_tensors`:
```
@gpflow.params_as_tensors
def compute_something(self): # self is parameterized object.
@@ -130,14 +131,16 @@ def params_as_tensors_for(obj, convert=True):
return s + b
```
+ :param objs: one or more instances of classes deriving from Parameterized
:param convert: Flag which is used for turning tensor convertion
feature on, `True`, or turning it off, `False`.
"""
- prev_value = _params_as_tensors_enter(obj, convert)
+ prev_values = [_params_as_tensors_enter(o, convert) for o in objs]
try:
yield
finally:
- _params_as_tensors_exit(obj, prev_value)
+ for o, pv in zip(objs, prev_values):
+ _params_as_tensors_exit(o, pv)
def autoflow(*af_args, **af_kwargs):
