copy_optimizer

copy_optimizer(optimizer)[source]

Create a new optimizer instance with the same configuration as the given optimizer.

This function creates a fresh copy of an optimizer with identical configuration parameters but with reset internal state. This is useful when multiple programs need their own optimizer instances to avoid state contamination.

Tip

Use this function when preparing a batch of programs that will run in parallel. Pass a fresh copy of the optimizer to each program instance to ensure thread safety.

Parameters:

optimizer (Optimizer) – The optimizer to copy.

Return type:

Optimizer

Returns:

A new optimizer instance with the same configuration but fresh state.

Raises:

ValueError – If the optimizer type is not recognized.