{"record":{"id":"85c09f68456df0b7","repo":"Unity-Technologies/ml-agents","slug":"when-using-a-recurrent-network-memory-size-must-b-85c09f","errorCode":null,"errorMessage":"When using a recurrent network, memory size must be divisible by 2.","messagePattern":"When using a recurrent network, memory size must be divisible by 2\\.","errorType":"validation","errorClass":"TrainerConfigError","httpStatus":null,"severity":"error","filePath":"ml-agents/mlagents/trainers/settings.py","lineNumber":131,"sourceCode":"    HYPER = \"hyper\"\n    NONE = \"none\"\n\n\n@attr.s(auto_attribs=True)\nclass NetworkSettings:\n    @attr.s\n    class MemorySettings:\n        sequence_length: int = attr.ib(default=64)\n        memory_size: int = attr.ib(default=128)\n\n        @memory_size.validator\n        def _check_valid_memory_size(self, attribute, value):\n            if value <= 0:\n                raise TrainerConfigError(\n                    \"When using a recurrent network, memory size must be greater than 0.\"\n                )\n            elif value % 2 != 0:\n                raise TrainerConfigError(\n                    \"When using a recurrent network, memory size must be divisible by 2.\"\n                )\n\n    normalize: bool = False\n    hidden_units: int = 128\n    num_layers: int = 2\n    vis_encode_type: EncoderType = EncoderType.SIMPLE\n    memory: Optional[MemorySettings] = None\n    goal_conditioning_type: ConditioningType = ConditioningType.HYPER\n    deterministic: bool = parser.get_default(\"deterministic\")\n\n\n@attr.s(auto_attribs=True)\nclass BehavioralCloningSettings:\n    demo_path: str\n    steps: int = 0\n    strength: float = 1.0\n    samples_per_update: int = 0","sourceCodeStart":113,"sourceCodeEnd":149,"githubUrl":"https://github.com/Unity-Technologies/ml-agents/blob/3ecb446f75d1e7400eb404c562dc005d3164cffc/ml-agents/mlagents/trainers/settings.py#L113-L149","documentation":"ML-Agents raises this when the `memory_size` hyperparameter in trainer config is set to an even-but-invalid negative or zero is caught separately, but an odd memory_size is rejected here. Recurrent networks (LSTM) require the memory size to be divisible by 2 because the LSTM cell internally splits memory between hidden state and cell state per direction. Odd values cannot be halved into two equal state tensors, so the config is rejected at load time via the attrs validator `_check_valid_memory_size`.","triggerScenarios":"Setting `memory_size` to any odd positive integer (e.g. 65, 129) in the `network_settings` block of a trainer YAML while using a recurrent network.","commonSituations":"Users copy a config and tweak memory_size to a 'round' odd number, or scale memory size up by a small increment (128 -> 129) without realizing the divisibility constraint for LSTM.","solutions":["Round memory_size up to the next even number (e.g. 129 -> 130, or prefer 128/256).","Set memory_size to 0 to disable recurrence entirely if LSTM is not needed.","Use power-of-two values like 64, 128, 256 which are always valid."],"exampleFix":"# before\nnetwork_settings:\n  memory_size: 129\n\n# after\nnetwork_settings:\n  memory_size: 130","handlingStrategy":"validation","validationCode":"memory_size = cfg['network_settings']['memory_size']\nif memory_size % 2 != 0 or memory_size <= 0:\n    raise ValueError(f'memory_size must be a positive even integer, got {memory_size}')","typeGuard":"def is_valid_memory_size(v) -> bool:\n    return isinstance(v, int) and v > 0 and v % 2 == 0","tryCatchPattern":"from mlagents.trainers.exception import TrainerConfigError\ntry:\n    load_trainer_config(path)\nexcept TrainerConfigError as e:\n    print(f'Invalid memory_size: {e}')","preventionTips":["Only use power-of-two memory sizes (64, 128, 256)","Add a config lint step that checks divisibility before training","Keep memory_size: 0 unless LSTM is required"],"tags":["config","recurrent-network","lstm","validation"],"backgroundTag":"invalid-hyperparameter-value","analyzedSha":"3ecb446f75d1e7400eb404c562dc005d3164cffc","analyzedAt":"2026-09-02T16:33:12.832Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-09T21:17:11.164Z"}