Unity-Technologies/ml-agents · error · TrainerConfigError

When using a recurrent network, memory size must be greater

Error message

When using a recurrent network, memory size must be greater than 0.

What it means

TrainerConfigError raised by the attrs validator _check_valid_memory_size on NetworkSettings when use_recurrent is enabled but memory_size is not a positive even integer. Recurrent policies require an LSTM hidden state whose size must be > 0 and divisible by 2 (multiplied internally for bidirectional layer computation).

Source

Thrown at ml-agents/mlagents/trainers/settings.py:127

    # LESSON = "lesson"


class ConditioningType(Enum):
    HYPER = "hyper"
    NONE = "none"


@attr.s(auto_attribs=True)
class NetworkSettings:
    @attr.s
    class MemorySettings:
        sequence_length: int = attr.ib(default=64)
        memory_size: int = attr.ib(default=128)

        @memory_size.validator
        def _check_valid_memory_size(self, attribute, value):
            if value <= 0:
                raise TrainerConfigError(
                    "When using a recurrent network, memory size must be greater than 0."
                )
            elif value % 2 != 0:
                raise TrainerConfigError(
                    "When using a recurrent network, memory size must be divisible by 2."
                )

    normalize: bool = False
    hidden_units: int = 128
    num_layers: int = 2
    vis_encode_type: EncoderType = EncoderType.SIMPLE
    memory: Optional[MemorySettings] = None
    goal_conditioning_type: ConditioningType = ConditioningType.HYPER
    deterministic: bool = parser.get_default("deterministic")


@attr.s(auto_attribs=True)
class BehavioralCloningSettings:

View on GitHub (pinned to 3ecb446f75)

Solutions

  1. Set memory_size to a positive even number, e.g. memory_size: 128 or 256.
  2. If you don't need memory, set use_recurrent: false instead of zeroing memory_size.
  3. Halve/double the value as needed — the LSTM size you request is scaled internally, so even round numbers are safest.

Example fix

# before
network_settings:
  use_recurrent: true
  memory_size: 127
# after
network_settings:
  use_recurrent: true
  memory_size: 128
Defensive patterns

Strategy: validation

Validate before calling

config = yaml.safe_load(open("config.yaml"))
ns = config["behavior"].get("network_settings", {})
if ns.get("use_recurrent", False):
    ms = ns.get("memory_size", 128)
    assert ms > 0 and ms % 2 == 0, f"memory_size must be a positive even integer, got {ms}"

Type guard

def memory_size_is_valid(use_recurrent: bool, memory_size: int) -> bool:
    return (not use_recurrent) or (memory_size > 0 and memory_size % 2 == 0)

Try / catch

from mlagents.trainers.exception import TrainerConfigError
try:
    run_training(config)
except TrainerConfigError as e:
    if "memory size" in str(e):
        config.network_settings.memory_size = 128
        run_training(config)

Prevention

When it happens

Trigger: Setting use_recurrent: true with memory_size: 0, a negative value, or an odd number (e.g. memory_size: 127) in network_settings of the trainer YAML.

Common situations: Copying a config where memory_size was zeroed out; hand-tuning LSTM size to odd values like 129; forgetting memory_size defaults matter when first enabling use_recurrent.

Understand the failure class

Background: "Invalid value" and "allowed values are" config errors: what your library rejected and how to fix it — this error's family across 41 libraries.

Related errors


AI-assisted analysis of Unity-Technologies/ml-agents@3ecb446f75 (2026-09-02). Data as JSON: /api/errors/b468b7ff3c22a5ba. Report an issue: GitHub.