trieste.utils
#
This package contains library utilities.
Package Contents#
- class Err(exc: Exception)[source]#
Bases:
Result
[NoReturn
]Wraps the exception that occurred during a failed evaluation.
- Parameters:
exc – The exception that occurred.
- class Ok(value: ResultType)[source]#
Bases:
Result
[ResultType
]Wraps the result of a successful evaluation.
- Parameters:
value – The result of a successful evaluation.
- 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 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).
- 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.
- 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
.