trieste.utils.misc#

Module Contents#

C[source]#

A type variable bound to typing.Callable.

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 and that 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.

ResultType[source]#

An unbounded covariant type variable.

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 an Err).

To check whether instances such as

>>> res = Ok(1)
>>> other_res = Err(ValueError("whoops"))

contain a value, use is_ok (or is_err)

>>> res.is_ok
True
>>> other_res.is_ok
False

We can access the value if it is_ok using unwrap().

>>> res.unwrap()
1

Trying to access the value of a failed Result, or Err, 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 and Err.

abstract property is_ok: bool[source]#

True if this Result contains a value, else False.

property is_err: bool[source]#

True if this Result contains an error, else False. The opposite of is_ok.

abstract unwrap() ResultType[source]#
Returns:

The contained value, if it exists.

Raises:

Exception – If there is no contained value.

class Ok(value: ResultType)[source]#

Bases: Result[ResultType]

Wraps the result of a successful evaluation.

Parameters:

value – The result of a successful evaluation.

property is_ok: bool[source]#

True always.

unwrap() ResultType[source]#
Returns:

The wrapped value.

class Err(exc: Exception)[source]#

Bases: Result[NoReturn]

Wraps the exception that occurred during a failed evaluation.

Parameters:

exc – The exception that occurred.

property is_ok: bool[source]#

False always.

unwrap() NoReturn[source]#
Raises:

Exception – Always. Raises the wrapped exception.

class DEFAULTS[source]#

Default constants used in Trieste.

JITTER: typing_extensions.Final[float] = 1e-06[source]#

The default jitter, typically used to stabilise computations near singular points, such as in Cholesky decomposition.

K[source]#

An unbound type variable.

U[source]#

An unbound type variable.

V[source]#

An unbound type variable.

map_values(f: Callable[[U], V], mapping: Mapping[K, U]) Mapping[K, V][source]#

Apply f to each value in mapping and return the result. If f does not modify its argument, map_values() does not modify mapping. 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 applying f to each value in mapping.

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.