trieste.utils.misc
#
Module Contents#
- jit(apply: bool = True, **optimize_kwargs: Any) Callable[[C], C] [source]#
A decorator that conditionally wraps a function with tf.function.
- Parameters:
apply – If True, the decorator is equivalent to tf.function. If False, the decorator does nothing.
optimize_kwargs – Additional arguments to tf.function.
- Returns:
The decorator.
- shapes_equal(this: trieste.types.TensorType, that: trieste.types.TensorType) trieste.types.TensorType [source]#
Return a scalar tensor containing: True if
this
andthat
have equal runtime shapes, else False.
- to_numpy(t: trieste.types.TensorType) numpy.ndarray[Any, Any] [source]#
- Parameters:
t – An array-like object.
- Returns:
t
as a NumPy array.
- class Result[source]#
Bases:
Generic
[ResultType
],abc.ABC
Represents the result of an operation that can fail with an exception. It contains either the operation return value (in an
Ok
), or the exception raised (in anErr
).To check whether instances such as
>>> res = Ok(1) >>> other_res = Err(ValueError("whoops"))
contain a value, use
is_ok
(oris_err
)>>> res.is_ok True >>> other_res.is_ok False
We can access the value if it
is_ok
usingunwrap()
.>>> res.unwrap() 1
Trying to access the value of a failed
Result
, orErr
, will raise the wrapped exception>>> other_res.unwrap() Traceback (most recent call last): ... ValueError: whoops
Note: This class is not intended to be subclassed other than by
Ok
andErr
.
- class Ok(value: ResultType)[source]#
Bases:
Result
[ResultType
]Wraps the result of a successful evaluation.
- Parameters:
value – The result of a successful evaluation.
- class Err(exc: Exception)[source]#
Bases:
Result
[NoReturn
]Wraps the exception that occurred during a failed evaluation.
- Parameters:
exc – The exception that occurred.
- map_values(f: Callable[[U], V], mapping: Mapping[K, U]) Mapping[K, V] [source]#
Apply
f
to each value inmapping
and return the result. Iff
does not modify its argument,map_values()
does not modifymapping
. For example:>>> import math >>> squares = {'a': 1, 'b': 4, 'c': 9} >>> map_values(math.sqrt, squares)['b'] 2.0 >>> squares {'a': 1, 'b': 4, 'c': 9}
- Parameters:
f – The function to apply to the values in
mapping
.mapping – A mapping.
- Returns:
A new mapping, whose keys are the same as
mapping
, and values are the result of applyingf
to each value inmapping
.
- class Timer[source]#
Functionality for timing chunks of code. For example: >>> from time import sleep >>> with Timer() as timer: sleep(2.0) >>> timer.time # doctest: +SKIP 2.0
- flatten_leading_dims(x: trieste.types.TensorType, output_dims: int = 2) Tuple[trieste.types.TensorType, Callable[[trieste.types.TensorType], trieste.types.TensorType]] [source]#
Flattens the leading dimensions of x (all but the last output_dims dimensions), and returns a function that can be used to restore them (typically after first manipulating the flattened tensor).
- get_variables(object: Any) tuple[tensorflow.Variable, Ellipsis] [source]#
Return the sequence of variables in an object.
This is essentially a reimplementation of the variables property of tf.Module but doesn’t require that we, or any of our substructures, inherit from that.
- Returns:
A sequence of variables of the object (sorted by attribute name) followed by variables from all submodules recursively (breadth first).
- _flatten(model, recursive=True, predicate=None, attribute_traversal_key=None, with_path=False, expand_composites=False)[source]#
Flattened attribute values in sorted order by attribute name.
This is taken verbatim from tensorflow core but uses a modified _flatten_module.
- _flatten_module(module, recursive, predicate, attribute_traversal_key, attributes_to_ignore, with_path, expand_composites, module_path=(), seen=None)[source]#
Implementation of flatten.
This is a reimplementation of the equivalent function in tf.Module so that we can extract the list of variables from a Trieste model wrapper without the need to inherit from it.