akro package

A library containing types of Spaces.

class akro.Space(shape=None, dtype=None)[source]

Bases: abc.ABC, gym.spaces.space.Space

Provides a classification state spaces and action spaces.

Allows you to write generic code that applies to any Environment. E.g. to choose a random action.

flat_dim

Return the length of the flattened vector of the space.

flatten(x)[source]

Return a flattened observation x.

Args:
x (Iterable): The object to flatten.
Returns:
np.ndarray: An array of x collapsed into one dimension.
flatten_n(xs)[source]

Return flattened observations xs.

Args:
xs (Iterable): The object to reshape and flatten
Returns:
np.ndarray: An array of xs in a shape inferred by the size of
its first element.
to_tf_placeholder(name, batch_dims)[source]

Create a tensor placeholder from the Space object.

Args:

name (str): name of the variable batch_dims (list): batch dimensions to add to the

shape of the object.
Returns:
tf.Tensor: Tensor object with the same properties as
the Dict where the shape is modified by batch_dims.
to_theano_tensor(name, batch_dims)[source]

Create a theano tensor from the Space object.

Args:

name (str): name of the variable batch_dims (list): batch dimensions to add to the

shape of the object.
Returns:
theano.tensor.TensorVariable: Tensor object with the
same properties as the Dict where the shape is modified by batch_dims.
unflatten(x)[source]

Return an unflattened observation x.

Args:
x (Iterable): The object to unflatten.
Returns:
np.ndarray: An array of x in the shape of self.shape.
unflatten_n(xs)[source]

Return unflattened observations xs.

Args:
xs (Iterable): The object to reshape and unflatten
Returns:
np.ndarray: An array of xs in a shape inferred by the size of
its first element and self.shape.
class akro.Box(low, high, shape=None, dtype=<class 'numpy.float32'>)[source]

Bases: gym.spaces.box.Box, akro.space.Space

A box in R^n.

Each coordinate is bounded above and below.

bounds

Return a 2-tuple containing the lower and upper bounds.

flat_dim

Return the length of the flattened vector of the space.

flatten(x)[source]

Return a flattened observation x.

Args:
x (:obj:’Iterable`): The object to flatten.
Returns:
np.ndarray: An array of x collapsed into one dimension.
flatten_n(obs)[source]

Return flattened observations obs.

Args:
obs (Iterable): The object to reshape and flatten
Returns:
np.ndarray: An array of obs in a shape inferred by the size of
its first element.
to_tf_placeholder(name, batch_dims)[source]

Create a tensor placeholder from the Space object.

Args:

name (str): name of the variable batch_dims (list): batch dimensions to add to the

shape of the object.
Returns:
tf.Tensor: Tensor object with the same properties as
the Box where the shape is modified by batch_dims.
to_theano_tensor(name, batch_dims)[source]

Create a theano tensor from the Space object.

Args:

name (str): name of the variable batch_dims (list): batch dimensions to add to the

shape of the object.
Returns:
theano.tensor.TensorVariable: Tensor object with the
same properties as the Box where the shape is modified by batch_dims.
unflatten(x)[source]

Return an unflattened observation x.

Args:
x (Iterable): The object to unflatten.
Returns:
np.ndarray: An array of x in the shape of self.shape.
unflatten_n(obs)[source]

Return unflattened observation of obs.

Args:
obs (Iterable): The object to reshape and unflatten
Returns:
np.ndarray: An array of obs in a shape inferred by the size of
its first element and self.shape.
class akro.Dict(spaces=None, **spaces_kwargs)[source]

Bases: gym.spaces.dict.Dict, akro.space.Space

A dictionary of simpler spaces, e.g. Discrete, Box.

Example usage:
self.observation_space = spaces.Dict({“position”: spaces.Discrete(2),
“velocity”: spaces.Discrete(3)})
flat_dim

Return the length of the flattened vector of the space.

flatten(x)[source]

Return an observation of x with collapsed values.

Args:
x (Iterable): The object to flatten.
Returns:
Dict: A Dict where each value is collapsed into a single dimension.
Keys are unchanged.
flatten_n(xs)[source]

Return flattened observations xs.

Args:
xs (Iterable): The object to reshape and flatten
Returns:
np.ndarray: An array of xs in a shape inferred by the size of
its first element.
to_tf_placeholder(name, batch_dims)[source]

Create a tensor placeholder from the Space object.

Args:

name (str): name of the variable batch_dims (list): batch dimensions to add to the

shape of the object.
Returns:
tf.Tensor: Tensor object with the same properties as
the Dict where the shape is modified by batch_dims.
to_theano_tensor(name, batch_dims)[source]

Create a theano tensor from the Space object.

Args:

name (str): name of the variable batch_dims (list): batch dimensions to add to the

shape of the object.
Returns:
theano.tensor.TensorVariable: Tensor object with the
same properties as the Dict where the shape is modified by batch_dims.
unflatten(x)[source]

Return an unflattened observation x.

Args:
x (Iterable): The object to unflatten.
Returns:
np.ndarray: An array of x in the shape of self.shape.
unflatten_n(xs)[source]

Return unflattened observations xs.

Args:
xs (Iterable): The object to reshape and unflatten
Returns:
np.ndarray: An array of xs in a shape inferred by the size of
its first element and self.shape.
class akro.Discrete(n)[source]

Bases: gym.spaces.discrete.Discrete, akro.space.Space

{0,1,…,n-1}.

flat_dim

Return the length of the flattened vector of the space.

flatten(x)[source]

Return a flattened observation x.

Args:
x (Iterable): The object to flatten.
Returns:
np.ndarray: An array of x collapsed into one dimension.
flatten_n(xs)[source]

Return flattened observations xs.

Args:
xs (Iterable): The object to reshape and flatten
Returns:
np.ndarray: An array of xs in a shape inferred by the size of
its first element.
to_tf_placeholder(name, batch_dims)[source]

Create a tensor placeholder from the Space object.

Args:

name (str): name of the variable batch_dims (list): batch dimensions to add to the

shape of the object.
Returns:
tf.Tensor: Tensor object with the same properties as
the Discrete obj where the shape is modified by batch_dims.
to_theano_tensor(name, batch_dims)[source]

Create a theano tensor from the Space object.

Args:

name (str): name of the variable batch_dims (list): batch dimensions to add to the

shape of the object.
Returns:
theano.tensor.TensorVariable: Tensor object with the
same properties as the Discrete obj where the shape is modified by batch_dims..
unflatten(x)[source]

Return an unflattened observation x.

Args:
x (Iterable): The object to unflatten.
Returns:
np.ndarray: An array of x in the shape of self.shape.
unflatten_n(xs)[source]

Return unflattened observations xs.

Args:
xs (Iterable): The object to reshape and unflatten
Returns:
np.ndarray: An array of xs in a shape inferred by the size of
its first element and self.shape.
weighted_sample(weights)[source]

Compute a weighted sample of the elements in the Discrete Space.

Args:
weights (list): Values to use in the sample.
Returns:
int or np.ndarray: A random sample of n based on
probabilities in weights.
class akro.Tuple(spaces)[source]

Bases: gym.spaces.tuple.Tuple, akro.space.Space

A Tuple of Spaces which produces samples which are Tuples of samples.

flat_dim

Return the length of the flattened vector of the space.

flatten(x)[source]

Return a flattened observation x.

Args:
x (Iterable): The object to flatten.
Returns:
np.ndarray: An array of x collapsed into one dimension.
flatten_n(obs)[source]

Return flattened observations obs.

Args:
obs (Iterable): The object to reshape and flatten
Returns:
np.ndarray: An array of obs in a shape inferred by the size of
its first element.
to_tf_placeholder(name, batch_dims)[source]

Create a tensor placeholder from the Space object.

Args:
name (str): name to append to the akro type when naming
the tensor. e.g. When name is ‘tmp’ - ‘Box-tmp’, ‘Discrete-tmp’.
batch_dims (list): batch dimensions to add to the
shape of each object in self.spaces.
Returns:
tuple(tf.Tensor): A tuple of Tensor objects converted
from each Space in self.spaces. Each Tensor’s shape is modified by batch_dims.
to_theano_tensor(name, batch_dims)[source]

Create a theano tensor from the Space object.

Args:
name (str): name to append to the akro type when naming
the tensor. e.g. When name is ‘tmp’ - ‘Box-tmp’, ‘Discrete-tmp’.
batch_dims (list): batch dimensions to add to the
shape of each object in self.spaces.
Returns:
theano.tensor.TensorVariable: A tuple of Tensor objects converted
from each Space in self.spaces. Each Tensor’s shape is modified by batch_dims.
unflatten(x)[source]

Return an unflattened observation x.

Args:
x (Iterable): The object to unflatten.
Returns:
tuple: A tuple of x in the shape of self.shape.
unflatten_n(obs)[source]

Return unflattened observations obs.

Args:
obs (Iterable): The object to reshape and unflatten
Returns:
np.ndarray: An array of obs in a shape inferred by the size of
its first element and self.shape.
akro.from_gym(space)[source]

Convert a gym.space to an akro.space.

Args:
space(gym.Space): The Space object to convert.
Returns:
akro.Space: The gym.Space object converted to an
akro.Space object.

Submodules

akro.box module

A Space representing a rectangular region of space.

class akro.box.Box(low, high, shape=None, dtype=<class 'numpy.float32'>)[source]

Bases: gym.spaces.box.Box, akro.space.Space

A box in R^n.

Each coordinate is bounded above and below.

bounds

Return a 2-tuple containing the lower and upper bounds.

flat_dim

Return the length of the flattened vector of the space.

flatten(x)[source]

Return a flattened observation x.

Args:
x (:obj:’Iterable`): The object to flatten.
Returns:
np.ndarray: An array of x collapsed into one dimension.
flatten_n(obs)[source]

Return flattened observations obs.

Args:
obs (Iterable): The object to reshape and flatten
Returns:
np.ndarray: An array of obs in a shape inferred by the size of
its first element.
to_tf_placeholder(name, batch_dims)[source]

Create a tensor placeholder from the Space object.

Args:

name (str): name of the variable batch_dims (list): batch dimensions to add to the

shape of the object.
Returns:
tf.Tensor: Tensor object with the same properties as
the Box where the shape is modified by batch_dims.
to_theano_tensor(name, batch_dims)[source]

Create a theano tensor from the Space object.

Args:

name (str): name of the variable batch_dims (list): batch dimensions to add to the

shape of the object.
Returns:
theano.tensor.TensorVariable: Tensor object with the
same properties as the Box where the shape is modified by batch_dims.
unflatten(x)[source]

Return an unflattened observation x.

Args:
x (Iterable): The object to unflatten.
Returns:
np.ndarray: An array of x in the shape of self.shape.
unflatten_n(obs)[source]

Return unflattened observation of obs.

Args:
obs (Iterable): The object to reshape and unflatten
Returns:
np.ndarray: An array of obs in a shape inferred by the size of
its first element and self.shape.

akro.dict module

Cartesian product of multiple named Spaces (also known as a dict of Spaces).

This Space produces samples which are dicts, where the values of those dicts are drawn from the values of this Space.

class akro.dict.Dict(spaces=None, **spaces_kwargs)[source]

Bases: gym.spaces.dict.Dict, akro.space.Space

A dictionary of simpler spaces, e.g. Discrete, Box.

Example usage:
self.observation_space = spaces.Dict({“position”: spaces.Discrete(2),
“velocity”: spaces.Discrete(3)})
flat_dim

Return the length of the flattened vector of the space.

flatten(x)[source]

Return an observation of x with collapsed values.

Args:
x (Iterable): The object to flatten.
Returns:
Dict: A Dict where each value is collapsed into a single dimension.
Keys are unchanged.
flatten_n(xs)[source]

Return flattened observations xs.

Args:
xs (Iterable): The object to reshape and flatten
Returns:
np.ndarray: An array of xs in a shape inferred by the size of
its first element.
to_tf_placeholder(name, batch_dims)[source]

Create a tensor placeholder from the Space object.

Args:

name (str): name of the variable batch_dims (list): batch dimensions to add to the

shape of the object.
Returns:
tf.Tensor: Tensor object with the same properties as
the Dict where the shape is modified by batch_dims.
to_theano_tensor(name, batch_dims)[source]

Create a theano tensor from the Space object.

Args:

name (str): name of the variable batch_dims (list): batch dimensions to add to the

shape of the object.
Returns:
theano.tensor.TensorVariable: Tensor object with the
same properties as the Dict where the shape is modified by batch_dims.
unflatten(x)[source]

Return an unflattened observation x.

Args:
x (Iterable): The object to unflatten.
Returns:
np.ndarray: An array of x in the shape of self.shape.
unflatten_n(xs)[source]

Return unflattened observations xs.

Args:
xs (Iterable): The object to reshape and unflatten
Returns:
np.ndarray: An array of xs in a shape inferred by the size of
its first element and self.shape.

akro.discrete module

A space representing a selection between a finite number of items.

class akro.discrete.Discrete(n)[source]

Bases: gym.spaces.discrete.Discrete, akro.space.Space

{0,1,…,n-1}.

flat_dim

Return the length of the flattened vector of the space.

flatten(x)[source]

Return a flattened observation x.

Args:
x (Iterable): The object to flatten.
Returns:
np.ndarray: An array of x collapsed into one dimension.
flatten_n(xs)[source]

Return flattened observations xs.

Args:
xs (Iterable): The object to reshape and flatten
Returns:
np.ndarray: An array of xs in a shape inferred by the size of
its first element.
to_tf_placeholder(name, batch_dims)[source]

Create a tensor placeholder from the Space object.

Args:

name (str): name of the variable batch_dims (list): batch dimensions to add to the

shape of the object.
Returns:
tf.Tensor: Tensor object with the same properties as
the Discrete obj where the shape is modified by batch_dims.
to_theano_tensor(name, batch_dims)[source]

Create a theano tensor from the Space object.

Args:

name (str): name of the variable batch_dims (list): batch dimensions to add to the

shape of the object.
Returns:
theano.tensor.TensorVariable: Tensor object with the
same properties as the Discrete obj where the shape is modified by batch_dims..
unflatten(x)[source]

Return an unflattened observation x.

Args:
x (Iterable): The object to unflatten.
Returns:
np.ndarray: An array of x in the shape of self.shape.
unflatten_n(xs)[source]

Return unflattened observations xs.

Args:
xs (Iterable): The object to reshape and unflatten
Returns:
np.ndarray: An array of xs in a shape inferred by the size of
its first element and self.shape.
weighted_sample(weights)[source]

Compute a weighted sample of the elements in the Discrete Space.

Args:
weights (list): Values to use in the sample.
Returns:
int or np.ndarray: A random sample of n based on
probabilities in weights.

akro.requires module

Decorators used for calling tensorflow and theano functions safely.

akro.requires.requires_tf(func)[source]

Check tf is installed before calling a function.

akro.requires.requires_theano(func)[source]

Check theano is installed before calling a function.

akro.space module

The abstract base class for all Space types.

class akro.space.Space(shape=None, dtype=None)[source]

Bases: abc.ABC, gym.spaces.space.Space

Provides a classification state spaces and action spaces.

Allows you to write generic code that applies to any Environment. E.g. to choose a random action.

flat_dim

Return the length of the flattened vector of the space.

flatten(x)[source]

Return a flattened observation x.

Args:
x (Iterable): The object to flatten.
Returns:
np.ndarray: An array of x collapsed into one dimension.
flatten_n(xs)[source]

Return flattened observations xs.

Args:
xs (Iterable): The object to reshape and flatten
Returns:
np.ndarray: An array of xs in a shape inferred by the size of
its first element.
to_tf_placeholder(name, batch_dims)[source]

Create a tensor placeholder from the Space object.

Args:

name (str): name of the variable batch_dims (list): batch dimensions to add to the

shape of the object.
Returns:
tf.Tensor: Tensor object with the same properties as
the Dict where the shape is modified by batch_dims.
to_theano_tensor(name, batch_dims)[source]

Create a theano tensor from the Space object.

Args:

name (str): name of the variable batch_dims (list): batch dimensions to add to the

shape of the object.
Returns:
theano.tensor.TensorVariable: Tensor object with the
same properties as the Dict where the shape is modified by batch_dims.
unflatten(x)[source]

Return an unflattened observation x.

Args:
x (Iterable): The object to unflatten.
Returns:
np.ndarray: An array of x in the shape of self.shape.
unflatten_n(xs)[source]

Return unflattened observations xs.

Args:
xs (Iterable): The object to reshape and unflatten
Returns:
np.ndarray: An array of xs in a shape inferred by the size of
its first element and self.shape.

akro.tuple module

Cartesian product of multiple Spaces (also known as a tuple of Spaces).

This Space produces samples which are Tuples, where the elments of those Tuples are drawn from the components of this Space.

class akro.tuple.Tuple(spaces)[source]

Bases: gym.spaces.tuple.Tuple, akro.space.Space

A Tuple of Spaces which produces samples which are Tuples of samples.

flat_dim

Return the length of the flattened vector of the space.

flatten(x)[source]

Return a flattened observation x.

Args:
x (Iterable): The object to flatten.
Returns:
np.ndarray: An array of x collapsed into one dimension.
flatten_n(obs)[source]

Return flattened observations obs.

Args:
obs (Iterable): The object to reshape and flatten
Returns:
np.ndarray: An array of obs in a shape inferred by the size of
its first element.
to_tf_placeholder(name, batch_dims)[source]

Create a tensor placeholder from the Space object.

Args:
name (str): name to append to the akro type when naming
the tensor. e.g. When name is ‘tmp’ - ‘Box-tmp’, ‘Discrete-tmp’.
batch_dims (list): batch dimensions to add to the
shape of each object in self.spaces.
Returns:
tuple(tf.Tensor): A tuple of Tensor objects converted
from each Space in self.spaces. Each Tensor’s shape is modified by batch_dims.
to_theano_tensor(name, batch_dims)[source]

Create a theano tensor from the Space object.

Args:
name (str): name to append to the akro type when naming
the tensor. e.g. When name is ‘tmp’ - ‘Box-tmp’, ‘Discrete-tmp’.
batch_dims (list): batch dimensions to add to the
shape of each object in self.spaces.
Returns:
theano.tensor.TensorVariable: A tuple of Tensor objects converted
from each Space in self.spaces. Each Tensor’s shape is modified by batch_dims.
unflatten(x)[source]

Return an unflattened observation x.

Args:
x (Iterable): The object to unflatten.
Returns:
tuple: A tuple of x in the shape of self.shape.
unflatten_n(obs)[source]

Return unflattened observations obs.

Args:
obs (Iterable): The object to reshape and unflatten
Returns:
np.ndarray: An array of obs in a shape inferred by the size of
its first element and self.shape.