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.

concat(other)[source]

Concatenate with another space of the same type.

Parameters:other (Space) – A space to be concatenated with this space.
Returns:A concatenated space.
Return type:Space
flat_dim

Return the length of the flattened vector of the space.

flatten(x)[source]

Return a flattened observation x.

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

Return flattened observations xs.

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

Create a tensor placeholder from the Space object.

Parameters:
  • name (str) – name of the variable
  • batch_dims (list) – batch dimensions to add to the shape of the object.
Returns:

Tensor object with the same properties as

the Dict where the shape is modified by batch_dims.

Return type:

tf.Tensor

to_theano_tensor(name, batch_dims)[source]

Create a theano tensor from the Space object.

Parameters:
  • name (str) – name of the variable
  • batch_dims (list) – batch dimensions to add to the shape of the object.
Returns:

Tensor object with the

same properties as the Dict where the shape is modified by batch_dims.

Return type:

theano.tensor.TensorVariable

unflatten(x)[source]

Return an unflattened observation x.

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

Return unflattened observations xs.

Parameters:xs (Iterable) – The object to reshape and unflatten
Returns:
An array of xs in a shape inferred by the size of
its first element and self.shape.
Return type:np.ndarray
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.

concat(other)[source]

Concatenate with another Box space.

Note that the dimension of both boxes will be flatten.

Parameters:other (Box) – A space to be concatenated with this space.
Returns:A concatenated space.
Return type:Box
flat_dim

Return the length of the flattened vector of the space.

flatten(x)[source]

Return a flattened observation x.

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

Return flattened observations obs.

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

Create a tensor placeholder from the Space object.

Parameters:
  • name (str) – name of the variable
  • batch_dims (list) – batch dimensions to add to the shape of the object.
Returns:

Tensor object with the same properties as

the Box where the shape is modified by batch_dims.

Return type:

tf.Tensor

to_theano_tensor(name, batch_dims)[source]

Create a theano tensor from the Space object.

Parameters:
  • name (str) – name of the variable
  • batch_dims (list) – batch dimensions to add to the shape of the object.
Returns:

Tensor object with the

same properties as the Box where the shape is modified by batch_dims.

Return type:

theano.tensor.TensorVariable

unflatten(x)[source]

Return an unflattened observation x.

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

Return unflattened observation of obs.

Parameters:obs (Iterable) – The object to reshape and unflatten
Returns:
An array of obs in a shape inferred by the size of
its first element and self.shape.
Return type:np.ndarray
class akro.Dict(spaces=None, **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)})
concat(other)[source]

Concatenate with another Dict space.

If a key exists in both Dict, the two associated spaces will be concat. If a key exists in only one Dict, the associated space will be copied to the new Dict.

Parameters:other (Dict) – A space to be concatenated with this space.
Returns:A concatenated space.
Return type:Dict
flat_dim

Return the length of the flattened vector of the space.

flat_dim_with_keys(keys)[source]

Return a flat dimension of the spaces specified by the keys.

Returns:sum (int)
flatten(x)[source]

Return an observation of x with collapsed values.

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

Return flattened observations xs.

Parameters:xs (Iterable) – The object to reshape and flatten
Returns:
An array of xs in a shape inferred by the size of
its first element.
Return type:np.ndarray
flatten_with_keys(x, keys)[source]

Return flattened obs of spaces specified by the keys using x.

Returns:list
to_tf_placeholder(name, batch_dims)[source]

Create a tensor placeholder from the Space object.

Parameters:
  • name (str) – name of the variable
  • batch_dims (list) – batch dimensions to add to the shape of the object.
Returns:

Tensor object with the same properties as

the Dict where the shape is modified by batch_dims.

Return type:

tf.Tensor

to_theano_tensor(name, batch_dims)[source]

Create a theano tensor from the Space object.

Parameters:
  • name (str) – name of the variable
  • batch_dims (list) – batch dimensions to add to the shape of the object.
Returns:

Tensor object with the

same properties as the Dict where the shape is modified by batch_dims.

Return type:

theano.tensor.TensorVariable

unflatten(x)[source]

Return an unflattened observation x.

Parameters:x (Iterable) – The object to unflatten.
Returns:collections.OrderedDict
unflatten_n(xs)[source]

Return unflattened observations xs.

Parameters:xs (Iterable) – The object to reshape and unflatten
Returns:List[OrderedDict]
unflatten_with_keys(x, keys)[source]

Return an unflattened observation.

This is the inverse of flatten_with_keys.

Returns:collections.OrderedDict
class akro.Discrete(n)[source]

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

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

concat(other)[source]

Concatenate with another space of the same type.

Parameters:other (Space) – A space to be concatenated with this space.
Returns:A concatenated space.
Return type:Space
flat_dim

Return the length of the flattened vector of the space.

flatten(x)[source]

Return a flattened observation x.

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

Return flattened observations xs.

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

Create a tensor placeholder from the Space object.

Parameters:
  • name (str) – name of the variable
  • batch_dims (list) – batch dimensions to add to the shape of the object.
Returns:

Tensor object with the same properties as

the Discrete obj where the shape is modified by batch_dims.

Return type:

tf.Tensor

to_theano_tensor(name, batch_dims)[source]

Create a theano tensor from the Space object.

Parameters:
  • name (str) – name of the variable
  • batch_dims (list) – batch dimensions to add to the shape of the object.
Returns:

Tensor object with the

same properties as the Discrete obj where the shape is modified by batch_dims..

Return type:

theano.tensor.TensorVariable

unflatten(x)[source]

Return an unflattened observation x.

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

Return unflattened observations xs.

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

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

Parameters:weights (list) – Values to use in the sample.
Returns:
A random sample of n based on
probabilities in weights.
Return type:int or np.ndarray
class akro.Image(shape)[source]

Bases: akro.box.Box

An Image, represented by a Box of at most three dimensions.

This class allows us to type check the observation input and decide whether to normalize. Each dimension must have pixel values between [0, 255].

Parameters:shape (tuple) – Shape of the observation. The shape cannot have more than 3 dimensions.
concat(other)[source]

Concatenate with another Image space.

Parameters:other (Image) – A space to be concatenated with this space.
Returns:A concatenated space.
Return type:Image
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.

concat(other)[source]

Concatenate with another Tuple space.

Parameters:other (Tuple) – A space to be concatenated with this space.
Returns:A concatenated space.
Return type:Tuple
flat_dim

Return the length of the flattened vector of the space.

flatten(x)[source]

Return a flattened observation x.

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

Return flattened observations obs.

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

Create a tensor placeholder from the Space object.

Parameters:
  • 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:

A tuple of Tensor objects converted

from each Space in self.spaces. Each Tensor’s shape is modified by batch_dims.

Return type:

tuple(tf.Tensor)

to_theano_tensor(name, batch_dims)[source]

Create a theano tensor from the Space object.

Parameters:
  • 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:

A tuple of Tensor objects converted

from each Space in self.spaces. Each Tensor’s shape is modified by batch_dims.

Return type:

theano.tensor.TensorVariable

unflatten(x)[source]

Return an unflattened observation x.

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

Return unflattened observations obs.

Parameters:obs (Iterable) – The object to reshape and unflatten
Returns:
An array of obs in a shape inferred by the size of
its first element and self.shape.
Return type:np.ndarray
akro.from_gym(space, is_image=False)[source]

Convert a gym.space to an akro.space.

Parameters:
  • space (gym.Space) – The Space object to convert.
  • is_image (boolean) – True if input is an image, False otherwise. This is False by default.
Returns:

The gym.Space object converted to an

akro.Space object.

Return type:

akro.Space

akro.concat(first, second)[source]

Concatenate two spaces of the same type.

Parameters:
  • first (Space) – The first space.
  • second (Space) – The second space.
Returns:

A concatenated space.

Return type:

Space

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.

concat(other)[source]

Concatenate with another Box space.

Note that the dimension of both boxes will be flatten.

Parameters:other (Box) – A space to be concatenated with this space.
Returns:A concatenated space.
Return type:Box
flat_dim

Return the length of the flattened vector of the space.

flatten(x)[source]

Return a flattened observation x.

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

Return flattened observations obs.

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

Create a tensor placeholder from the Space object.

Parameters:
  • name (str) – name of the variable
  • batch_dims (list) – batch dimensions to add to the shape of the object.
Returns:

Tensor object with the same properties as

the Box where the shape is modified by batch_dims.

Return type:

tf.Tensor

to_theano_tensor(name, batch_dims)[source]

Create a theano tensor from the Space object.

Parameters:
  • name (str) – name of the variable
  • batch_dims (list) – batch dimensions to add to the shape of the object.
Returns:

Tensor object with the

same properties as the Box where the shape is modified by batch_dims.

Return type:

theano.tensor.TensorVariable

unflatten(x)[source]

Return an unflattened observation x.

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

Return unflattened observation of obs.

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

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, **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)})
concat(other)[source]

Concatenate with another Dict space.

If a key exists in both Dict, the two associated spaces will be concat. If a key exists in only one Dict, the associated space will be copied to the new Dict.

Parameters:other (Dict) – A space to be concatenated with this space.
Returns:A concatenated space.
Return type:Dict
flat_dim

Return the length of the flattened vector of the space.

flat_dim_with_keys(keys)[source]

Return a flat dimension of the spaces specified by the keys.

Returns:sum (int)
flatten(x)[source]

Return an observation of x with collapsed values.

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

Return flattened observations xs.

Parameters:xs (Iterable) – The object to reshape and flatten
Returns:
An array of xs in a shape inferred by the size of
its first element.
Return type:np.ndarray
flatten_with_keys(x, keys)[source]

Return flattened obs of spaces specified by the keys using x.

Returns:list
to_tf_placeholder(name, batch_dims)[source]

Create a tensor placeholder from the Space object.

Parameters:
  • name (str) – name of the variable
  • batch_dims (list) – batch dimensions to add to the shape of the object.
Returns:

Tensor object with the same properties as

the Dict where the shape is modified by batch_dims.

Return type:

tf.Tensor

to_theano_tensor(name, batch_dims)[source]

Create a theano tensor from the Space object.

Parameters:
  • name (str) – name of the variable
  • batch_dims (list) – batch dimensions to add to the shape of the object.
Returns:

Tensor object with the

same properties as the Dict where the shape is modified by batch_dims.

Return type:

theano.tensor.TensorVariable

unflatten(x)[source]

Return an unflattened observation x.

Parameters:x (Iterable) – The object to unflatten.
Returns:collections.OrderedDict
unflatten_n(xs)[source]

Return unflattened observations xs.

Parameters:xs (Iterable) – The object to reshape and unflatten
Returns:List[OrderedDict]
unflatten_with_keys(x, keys)[source]

Return an unflattened observation.

This is the inverse of flatten_with_keys.

Returns:collections.OrderedDict

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}.

concat(other)[source]

Concatenate with another space of the same type.

Parameters:other (Space) – A space to be concatenated with this space.
Returns:A concatenated space.
Return type:Space
flat_dim

Return the length of the flattened vector of the space.

flatten(x)[source]

Return a flattened observation x.

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

Return flattened observations xs.

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

Create a tensor placeholder from the Space object.

Parameters:
  • name (str) – name of the variable
  • batch_dims (list) – batch dimensions to add to the shape of the object.
Returns:

Tensor object with the same properties as

the Discrete obj where the shape is modified by batch_dims.

Return type:

tf.Tensor

to_theano_tensor(name, batch_dims)[source]

Create a theano tensor from the Space object.

Parameters:
  • name (str) – name of the variable
  • batch_dims (list) – batch dimensions to add to the shape of the object.
Returns:

Tensor object with the

same properties as the Discrete obj where the shape is modified by batch_dims..

Return type:

theano.tensor.TensorVariable

unflatten(x)[source]

Return an unflattened observation x.

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

Return unflattened observations xs.

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

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

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

akro.image module

A Space representing an RGB Image.

class akro.image.Image(shape)[source]

Bases: akro.box.Box

An Image, represented by a Box of at most three dimensions.

This class allows us to type check the observation input and decide whether to normalize. Each dimension must have pixel values between [0, 255].

Parameters:shape (tuple) – Shape of the observation. The shape cannot have more than 3 dimensions.
concat(other)[source]

Concatenate with another Image space.

Parameters:other (Image) – A space to be concatenated with this space.
Returns:A concatenated space.
Return type:Image

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.

concat(other)[source]

Concatenate with another space of the same type.

Parameters:other (Space) – A space to be concatenated with this space.
Returns:A concatenated space.
Return type:Space
flat_dim

Return the length of the flattened vector of the space.

flatten(x)[source]

Return a flattened observation x.

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

Return flattened observations xs.

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

Create a tensor placeholder from the Space object.

Parameters:
  • name (str) – name of the variable
  • batch_dims (list) – batch dimensions to add to the shape of the object.
Returns:

Tensor object with the same properties as

the Dict where the shape is modified by batch_dims.

Return type:

tf.Tensor

to_theano_tensor(name, batch_dims)[source]

Create a theano tensor from the Space object.

Parameters:
  • name (str) – name of the variable
  • batch_dims (list) – batch dimensions to add to the shape of the object.
Returns:

Tensor object with the

same properties as the Dict where the shape is modified by batch_dims.

Return type:

theano.tensor.TensorVariable

unflatten(x)[source]

Return an unflattened observation x.

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

Return unflattened observations xs.

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

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.

concat(other)[source]

Concatenate with another Tuple space.

Parameters:other (Tuple) – A space to be concatenated with this space.
Returns:A concatenated space.
Return type:Tuple
flat_dim

Return the length of the flattened vector of the space.

flatten(x)[source]

Return a flattened observation x.

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

Return flattened observations obs.

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

Create a tensor placeholder from the Space object.

Parameters:
  • 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:

A tuple of Tensor objects converted

from each Space in self.spaces. Each Tensor’s shape is modified by batch_dims.

Return type:

tuple(tf.Tensor)

to_theano_tensor(name, batch_dims)[source]

Create a theano tensor from the Space object.

Parameters:
  • 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:

A tuple of Tensor objects converted

from each Space in self.spaces. Each Tensor’s shape is modified by batch_dims.

Return type:

theano.tensor.TensorVariable

unflatten(x)[source]

Return an unflattened observation x.

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

Return unflattened observations obs.

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