Encoders

Encoders are used to translate the properties of the environment as seen through an Agent’s context view into that environment into an observation that the agent is able to train on:

../_images/encoder.svg

Encoders are a fully optional feature of Phantom. There is no functional difference between defining an encode_observation() method on an Agent and defining an Encoder (whose encode() method performs the same actions) and attaching it to the Agent.

Encoders are useful when many different Agents want to encode the same properties and code re-use is desirable. It is also possible to compose multiple encoders (see ChainedEncoder), allowing the construction of complex encoders from many smaller parts.

Base Encoder

class phantom.encoders.Encoder(*args, **kwds)[source]

A trait for types that encodes the context of an agent into an observation.

chain(others)[source]

Chains this encoder together with adjoint set of encoders.

This method returns a ChainedEncoder instance where the output space reduces to a tuple with each element given by the output space specified in each of the encoders provided.

Return type:

ChainedEncoder

abstract encode(ctx)[source]

Encode the data in a given network context into an observation.

Parameters:

ctx (Context) – The local network context.

Return type:

TypeVar(Observation)

Returns:

An observation encoding properties of the provided context.

abstract property observation_space: Space

The output space of the encoder type.

reset()[source]

Resets the encoder.

Provided Implementations

class phantom.encoders.EmptyEncoder(*args, **kwds)[source]

Generates an empty observation.

chain(others)

Chains this encoder together with adjoint set of encoders.

This method returns a ChainedEncoder instance where the output space reduces to a tuple with each element given by the output space specified in each of the encoders provided.

Return type:

ChainedEncoder

encode(_)[source]

Encode the data in a given network context into an observation.

Parameters:

ctx – The local network context.

Return type:

ndarray

Returns:

An observation encoding properties of the provided context.

property observation_space: Box

The output space of the encoder type.

reset()

Resets the encoder.

class phantom.encoders.ChainedEncoder(encoders)[source]

Combines n encoders into a single encoder with a tuple action space.

encoders

An iterable collection of encoders which is flattened into a list.

chain(others)[source]

Chains this encoder together with adjoint set of encoders.

This method returns a ChainedEncoder instance where the output space reduces to a tuple with each element given by the output space specified in each of the encoders provided.

Return type:

ChainedEncoder

encode(ctx)[source]

Encode the data in a given network context into an observation.

Parameters:

ctx (Context) – The local network context.

Return type:

Tuple

Returns:

An observation encoding properties of the provided context.

property observation_space: Space

The output space of the encoder type.

reset()[source]

Resets the encoder.

class phantom.encoders.Constant(shape, value=0.0)[source]

Encoder that always returns a constant valued Box Space.

Parameters:
  • shape (Tuple[int]) – Shape of the returned box.

  • value (float) – Value that the box is filled with.

chain(others)

Chains this encoder together with adjoint set of encoders.

This method returns a ChainedEncoder instance where the output space reduces to a tuple with each element given by the output space specified in each of the encoders provided.

Return type:

ChainedEncoder

encode(_)[source]

Encode the data in a given network context into an observation.

Parameters:

ctx – The local network context.

Return type:

ndarray

Returns:

An observation encoding properties of the provided context.

property observation_space: Box

The output space of the encoder type.

reset()

Resets the encoder.