benchmarl.models.SequenceModelConfig

class SequenceModelConfig(model_configs: Sequence[ModelConfig], intermediate_sizes: Sequence[int])[source]

Bases: ModelConfig

Dataclass for a SequenceModel.

Examples

import torch_geometric
from torch import nn
from benchmarl.algorithms import IppoConfig
from benchmarl.environments import VmasTask
from benchmarl.experiment import Experiment, ExperimentConfig
from benchmarl.models import SequenceModelConfig, GnnConfig, MlpConfig

experiment = Experiment(
    algorithm_config=IppoConfig.get_from_yaml(),
    model_config=SequenceModelConfig(
        model_configs=[
            MlpConfig(num_cells=[8], activation_class=nn.Tanh, layer_class=nn.Linear),
            GnnConfig(
                topology="full",
                self_loops=False,
                gnn_class=torch_geometric.nn.conv.GraphConv,
            ),
            MlpConfig(num_cells=[6], activation_class=nn.Tanh, layer_class=nn.Linear),
        ],
        intermediate_sizes=[5, 3],
    ),
    seed=0,
    config=ExperimentConfig.get_from_yaml(),
    task=VmasTask.NAVIGATION.get_from_yaml(),
)
experiment.run()
get_model(input_spec: Composite, output_spec: Composite, agent_group: str, input_has_agent_dim: bool, n_agents: int, centralised: bool, share_params: bool, device: device | str | int, action_spec: Composite, model_index: int = 0) Model[source]

Creates the model from the config.

Parameters:
  • input_spec (Composite) – the input spec of the model

  • output_spec (Composite) – the output spec of the model

  • agent_group (str) – the name of the agent group the model is for

  • n_agents (int) – the number of agents this module is for

  • device (str) – the mdoel’s device

  • input_has_agent_dim (bool) – This tells the model if the input will have a multi-agent dimension or not. For example, the input of policies will always have this set to true, but critics that use a global state have this set to false as the state is shared by all agents

  • centralised (bool) – This tells the model if it has full observability. This will always be true when self.input_has_agent_dim==False, but in cases where the input has the agent dimension, this parameter is used to distinguish between a decentralised model (where each agent’s data is processed separately) and a centralized model, where the model pools all data together

  • share_params (bool) – This tells the model if it should have only one set of parameters or a different set of parameters for each agent. This is independent of the other options as it is possible to have different parameters for centralized critics with global input.

  • action_spec (Composite) – The action spec of the environment

  • model_index (int) – the index of the model in a sequence. Defaults to 0.

Returns: the Model

static associated_class()[source]

The associated Model class

property is_critic

Whether the model is a critic

get_model_state_spec(model_index: int = 0) Composite[source]

Get additional specs needed by the model as input.

This method is useful for adding recurrent states.

The returned value should be key: spec with the desired ending shape.

The batch and agent dimensions will automatically be added to the spec.

Parameters:

model_index (int, optional) – the index of the model. Defaults to 0.

property is_rnn: bool

Whether the model is an RNN

classmethod get_from_yaml(path: str | None = None)[source]

Load the model configuration from yaml

Parameters:

path (str, optional) – The full path of the yaml file to load from. If None, it will default to benchmarl/conf/model/layers/self.associated_class().__name__

Returns: the loaded AlgorithmConfig