decent_bench.algorithms.utils#
Utilities for algorithm initialization and general helpers.
- decent_bench.algorithms.utils.initial_states(x0: InitialStates, network: Network) dict[Agent, Array][source]#
Build per-agent initial states, for use in
initialize().- Parameters:
x0 (InitialStates) –
None: initialize all agents to zeros, using each agent’s native shape/framework/device.Array: apply the same state to all agents.dict[Agent, Array]: explicit per-agent states.
network (Network) – network instance containing the target agents.
- Returns:
mapping from each network agent to its initial state.
- Return type:
- Raises:
ValueError – if
x0is missing required agent entries.TypeError – if
x0has an invalid type.
Notes
For
FedNetwork, explicitx0dictionaries must provide client entries. If the server entry is missing, it is inferred as the average of client initial states. Keys inx0not referring to agents in the network are silently ignored.
- decent_bench.algorithms.utils.normal_initialization(network: Network, mean: float = 0.0, std: float = 1.0) dict[Agent, Array][source]#
Build per-agent initial states sampled from a normal distribution.
- Parameters:
- Returns:
mapping from each agent to an independently sampled random initial state.
- Return type:
Notes
The states are created using each agent’s own
cost.shape,cost.framework, andcost.device.
- decent_bench.algorithms.utils.pytorch_initialization(network: Network, all_same: bool = False) dict[Agent, Array] | Array[source]#
Build per-agent initial states using
PyTorchCost.modelinitialization routine.Gets the initialized parameter tensor for every agent from
PyTorchCost.model(viatorch.nn.Module.parameters()), and flattens it. The returned dict is compatible withinitial_states()and can be passed directly asx0to any algorithm.- Parameters:
network (Network) – network instance containing the target agents. All agents must have a
PyTorchCost.all_same (bool) – if
True, use the first agent’s initialized state for all agents.
- Returns:
mapping from each network agent to its initial state, as a flattened parameter vector extracted from the initialized model.
- Return type:
- Raises:
TypeError – if any agent’s cost is not a
PyTorchCost.
- decent_bench.algorithms.utils.uniform_initialization(network: Network, low: float = 0.0, high: float = 1.0) dict[Agent, Array][source]#
Build per-agent initial states sampled from a uniform distribution.
- Parameters:
- Returns:
mapping from each agent to an independently sampled random initial state.
- Return type:
- Raises:
ValueError – if
highis not greater thanlow.
Notes
The states are created using each agent’s own
cost.shape,cost.framework, andcost.device.