REINFORCE Training Loop
Use this skill to recover or test Williams-style REINFORCE behavior on a small stochastic environment. It is appropriate when the evidence must show sampled stochastic actions, scalar reinforcement, score-function updates, and changed policy parameters. Do not use it for deterministic policy-gradient, Q-learning, or supervised imitation-only experiments.
Inputs
- Initial Bernoulli logit or equivalent stochastic policy parameters.
- Reward function mapping sampled actions to scalar rewards.
- Episode count, learning rate, random seed, and baseline value.
- Optional path to the
score_function_estimatorscript for cross-checking the local score term.
Outputs
- Episode-level trace containing action probability, sampled action, reward, advantage, update, and parameter value.
- Summary metrics before and after training.
params_beforeandparams_afterfields suitable for Distiller recovery validation.- Mechanism booleans for stochastic sampling and optimizer execution.
Workflow
- Initialize the stochastic policy and compute its expected reward before training.
- For every episode, sample an action from the current policy distribution.
- Observe scalar reward and compute the REINFORCE score-function update.
- Apply
theta += learning_rate * updateand record the parameter transition. - Report expected reward after training and verify that parameters changed.
- When used in recovery, save the trace under
recovery/logs/training_trace.json.
Validation
Run:
python scripts/train_bandit.py --episodes 128 --seed 7 --output /tmp/reinforce_trace.json
python -m pytest tests
The tests assert that the better action's probability and expected reward improve in a deterministic seeded two-action bandit.
Limitations
This skill is a reduced mechanism-faithful recovery harness. It does not claim to reproduce every historical example from the paper or delayed-reinforcement variants.