RewardShaper
RewardShaper(context: RewardShapingContext) -> NoneBase class for trainer-owned reward shapers.
Initialize the reward shaper.
Parameters:
context(RewardShapingContext) – Shared shaping runtime context.
Properties
context
context: RewardShapingContextGet the shaping runtime context.
Returns:
RewardShapingContext– The shared shaping context.
metric_namespace
metric_namespace: strGet the metric namespace used for this shaper's custom metrics.
Returns:
str– The metric namespace.
Methods
add_averaged_metric
add_averaged_metric(name: str, value: float) -> NoneLog a shaper-specific averaged metric.
The value is buffered and written out at the step passed to the next flush_metrics.
Parameters:
name(str) – The metric name suffix.value(float) – The metric value.
add_episode_metric
add_episode_metric(name: str, env_id: int, value: float) -> NoneAccumulate a shaper-specific metric to be logged once per episode.
Parameters:
name(str) – The episode metric name suffix.env_id(int) – The environment ID.value(float) – The value to add to the episode total.
add_scalar_metric
add_scalar_metric(name: str, value: float) -> NoneLog a shaper-specific scalar metric.
The value is buffered and written out at the step passed to the next flush_metrics.
Parameters:
name(str) – The metric name suffix.value(float) – The metric value.
flush_episode_metrics
flush_episode_metrics(env_id: int) -> NoneEmit and clear accumulated episode metrics for one environment.
Parameters:
env_id(int) – The environment ID.
flush_metrics
flush_metrics(step: int) -> NoneEmit any throttled scalar and averaged metrics accumulated since the last flush.
Parameters:
step(int) – The step at which to log.
learn
learn(batch: RewardShapingBatch) -> NoneUpdate learner-owned shaper state from a batch.
Stateless or actor-only shapers can keep the default no-op implementation.
Parameters:
batch(RewardShapingBatch) – The learner batch to consume.
load_state_dict
load_state_dict(state: dict[str, Any]) -> NoneRestore shaper state from a checkpoint dictionary.
Parameters:
state(dict[str, Any]) – The serialised state dictionary.
metadata_key
metadata_key(name: str) -> strBuild a namespaced learner-metadata key for this shaper.
Parameters:
name(str) – The metadata name suffix.
Returns:
str– The full metadata key.
on_episode_end
on_episode_end(env_id: int, step: int) -> NoneNotify the shaper that an environment episode has ended.
Parameters:
env_id(int) – The environment ID.step(int) – The global step at episode end.
on_episode_start
on_episode_start(env_id: int, initial_observation: np.ndarray) -> NoneNotify the shaper that an environment has started a fresh episode.
Parameters:
env_id(int) – The environment ID.initial_observation(ndarray) – The reset observation for the new episode.
reset_episode_metrics
reset_episode_metrics(env_id: int) -> NoneClear any accumulated episode metrics for one environment.
Parameters:
env_id(int) – The environment ID.
shape
shape(step: RewardShapingStep) -> floatShape a reward for one environment step.
Parameters:
step(RewardShapingStep) – The step information, including observations and reward.
Returns:
float– The new reward value after this shaper has run.
shape_batch
shape_batch(steps: Sequence[RewardShapingStep]) -> list[float]Shape a batch of environment steps.
The default implementation delegates to shape one item at a time. Shapers with efficient batched inference can override this.
Parameters:
steps(Sequence[RewardShapingStep]) – The steps to shape.
Returns:
list[float]– A shaped reward for each input step.
shape_batch_with_metadata
shape_batch_with_metadata(steps: Sequence[RewardShapingStep]) -> list[RewardShapingOutput]Shape a batch of environment steps and emit learner metadata for each step.
Shapers that need learner-side routing information can override this instead of shape_batch.
Parameters:
steps(Sequence[RewardShapingStep]) – The steps to shape.
Returns:
list[RewardShapingOutput]– A shaped reward plus optional learner metadata for each step.
state_dict
state_dict() -> dict[str, Any]Capture serializable shaper state for checkpoints.
Returns:
dict[str, Any]– A serializable state dictionary.
Values
metric_namespace_override
metric_namespace_override: str | None = Nonereward_stream
reward_stream: RewardStream = RewardStream.EXTRINSIC