# base_strategy

Python API: `luxonis_train.strategies.base_strategy`

The base class every training strategy inherits.

## Classes

| Name | Description |
| --- | --- |
| [`BaseTrainingStrategy`](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/strategies/base_strategy.md#base_strategy-Classes-BaseTrainingStrategy) | Base class for the training strategies. |

### class `BaseTrainingStrategy`

```python
class BaseTrainingStrategy(ABC)
```

Base class for the training strategies.

A strategy adds parameter-group rules to the partition of the model parameters, and can change its groups on every step. A
subclass registers in the `STRATEGIES` registry under its class name, so `trainer.training_strategy.name` can name it.

[resolve_training_plan](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/lightning/training_plan.md#training_plan-Functions-resolve_training_plan)
evaluates the strategy rules after the `finetuning` entries of every node and before the default rule. A node entry therefore wins
over a strategy rule, and every parameter that the strategy does not claim still gets an optimizer.

> **Example**
> The `trainer` section of a config that uses a strategy:

```yaml
trainer:
  training_strategy:
    name: TripleLRSGDStrategy
    params:
      lr: 0.02
      warmup_epochs: 3
```

Attributes

| Name | Description |
| --- | --- |
| `group_handles` |  |
| `runtime` |  |

Methods

| Name | Description |
| --- | --- |
| [`__init__`](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/strategies/base_strategy.md#base_strategy-Classes-BaseTrainingStrategy-__init__) | Create the strategy for a Lightning module. |
| [`attach`](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/strategies/base_strategy.md#base_strategy-Classes-BaseTrainingStrategy-attach) | Store the runtime and the group handles of the strategy rules. |
| [`get_base_configs`](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/strategies/base_strategy.md#base_strategy-Classes-BaseTrainingStrategy-get_base_configs) | Return the base optimizer and scheduler of the strategy. |
| [`opaque_inners`](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/strategies/base_strategy.md#base_strategy-Classes-BaseTrainingStrategy-opaque_inners) | Return the optimizers that the strategy builds itself. |
| [`opaque_parameter_ids`](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/strategies/base_strategy.md#base_strategy-Classes-BaseTrainingStrategy-opaque_parameter_ids) | Return the parameters that the strategy claims outside the rules. |
| [`rules`](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/strategies/base_strategy.md#base_strategy-Classes-BaseTrainingStrategy-rules) | Return the parameter-group rules of the strategy, in order. |
| [`update_parameters`](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/strategies/base_strategy.md#base_strategy-Classes-BaseTrainingStrategy-update_parameters) | Change the groups of the strategy after a backward pass. |

#### method `__init__`

```python
def __init__(pl_module: lxt.LuxonisLightningModule, **kwargs)
```

Create the strategy for a Lightning module.

[luxonis_train.lightning.utils.build_training_strategy](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/lightning/utils.md#utils-Functions-build_training_strategy)
calls the constructor with the module and with the entries of `trainer.training_strategy.params` as keyword arguments.

Args

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `pl_module` | `lxt.LuxonisLightningModule` | The module to train. | required |
| `**kwargs` |  | The parameters of the strategy from the config. |  |

#### method `attach`

```python
def attach(runtime: TrainingPlanRuntime, handles: Mapping[str, tuple[GroupHandle, ...]])
```

Store the runtime and the group handles of the strategy rules.

[LuxonisLightningModule.configure_optimizers](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/lightning/luxonis_lightning.md#luxonis_lightning-Classes-LuxonisLightningModule-configure_optimizers)
calls the method after it builds the optimizers. The method sets the `runtime` and `group_handles` attributes. An override must
keep them, or call this method. A handle holds indices, so it stays valid after a checkpoint loads.

Args

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `runtime` | `TrainingPlanRuntime` | The optimizers and schedulers of the plan. `runtime.group(handle)` returns a group. | required |
| `handles` | `Mapping[str, tuple[GroupHandle, ...]]` | The handles of the groups of each rule, keyed by the rule tag. A rule that claims no parameter has no entry. | required |

#### method `get_base_configs`

```python
def get_base_configs() -> tuple[OptimizerConfig, SchedulerConfig]
```

Return the base optimizer and scheduler of the strategy.

While the strategy is active, the base configs replace `trainer.optimizer` and `trainer.scheduler`. The `finetuning` entries of
the nodes merge their overrides into them, and the default rule uses them. An implementation can raise `NotImplementedError`.
[resolve_training_plan](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/lightning/training_plan.md#training_plan-Functions-resolve_training_plan)
then uses `trainer.optimizer` and `trainer.scheduler`.

Returns

| Type | Description |
| --- | --- |
| `tuple[OptimizerConfig, SchedulerConfig]` | The base optimizer config and the base scheduler config. |

#### method `opaque_inners`

```python
def opaque_inners() -> list[tuple[Optimizer, Any]]
```

Return the optimizers that the strategy builds itself.

[build_training_plan](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/lightning/training_plan.md#training_plan-Functions-build_training_plan)
adds them after the inner optimizers of the plan. The base implementation returns an empty list. Only
[LegacyStrategyAdapter](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/strategies/legacy.md#legacy-Classes-LegacyStrategyAdapter)
overrides the method.

Returns

| Type | Description |
| --- | --- |
| `list[tuple[Optimizer, Any]]` | Pairs of an optimizer and its scheduler. The scheduler is a scheduler, a Lightning scheduler config dictionary, or `None`. |

#### method `opaque_parameter_ids`

```python
def opaque_parameter_ids() -> set[int]
```

Return the parameters that the strategy claims outside the rules.

[resolve_training_plan](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/lightning/training_plan.md#training_plan-Functions-resolve_training_plan)
leaves these parameters out of the plan. The base implementation returns an empty set. Only
[LegacyStrategyAdapter](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/strategies/legacy.md#legacy-Classes-LegacyStrategyAdapter)
overrides the method.

Returns

| Type | Description |
| --- | --- |
| `set[int]` | The `id()` of each claimed parameter. |

#### method `rules`

```python
def rules() -> list[StrategyRule]
```

Return the parameter-group rules of the strategy, in order.

The first rule whose selector accepts a free parameter claims it. Rules with the same optimizer name and the same scheduler share
one inner optimizer.

Returns

| Type | Description |
| --- | --- |
| `list[StrategyRule]` | The rules. A rule without a scheduler uses the scheduler of [get_base_configs](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/strategies/base_strategy.md#base_strategy-Classes-BaseTrainingStrategy-get_base_configs). |

#### method `update_parameters`

```python
def update_parameters()
```

Change the groups of the strategy after a backward pass.

[TrainingManager](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/callbacks/training_manager.md#training_manager-Classes-TrainingManager)
calls the method after each backward pass, before the optimizers step. The base implementation does nothing.
