Engineering.Reference:26.4.3.3 - Generate attempts, update and use the result
Consider two supplied strategies P and Q, each admitting actions a and b. In the qualified deterministic fixture, P,a returns 1, P,b returns 0, and either Q action returns 0.4. Three independent logits determine p=Pr(P), s=Pr(a|P) and t=Pr(a|Q). All start at zero, so the probabilities are 0.5. The choices and their meanings are supplied capabilities; the learner changes how often it selects them.
The code uses declared draws to generate P and Q once each and four executions per strategy. Those draws make the finite calculation reproducible; they are not a random-sample performance assessment. With K=2, P’s outcomes (1,0,0,0) give strategy score 0.5 and Q’s (0.4,0.4,0.4,0.4) give 0.4. Strategy advantages are (+1,-1). P’s action advantages are (sqrt(3),-1/sqrt(3),-1/sqrt(3),-1/sqrt(3)); Q’s are zero.
from math import exp, sqrt
def sigmoid(z):
return 1 / (1 + exp(-z))
def standardize(xs):
mean = sum(xs) / len(xs)
sd = sqrt(sum((x - mean)**2 for x in xs) / len(xs))
return [(x - mean) / sd for x in xs] if sd > 1e-12 else [0.0] * len(xs)
def outcome(plan, action):
return float(action == "a") if plan == "P" else 0.4
old_z = [0.0, 0.0, 0.0]
old = [sigmoid(z) for z in old_z]
task = "one P/Q task with the supplied action meanings"
groups = []
for draw in (0.2, 0.8):
plan_bit = int(draw < old[0])
plan = "P" if plan_bit else "Q"
slot = 1 if plan_bit else 2
group = {"bit": plan_bit, "history": task, "slot": slot,
"old": old[0] if plan_bit else 1 - old[0], "runs": []}
for action_draw in (0.1, 0.7, 0.8, 0.9):
bit = int(action_draw < old[slot])
action = "a" if bit else "b"
group["runs"].append({
"history": (task, plan), "bit": bit, "action": action,
"old": old[slot] if bit else 1 - old[slot],
"reward": outcome(plan, action)})
groups.append(group)
K = 2
assert all(1 <= K <= len(g["runs"]) for g in groups)
scores = [sum(sorted((r["reward"] for r in g["runs"]), reverse=True)[:K]) / K
for g in groups]
plan_advantages = standardize(scores)
for g in groups:
g["advantages"] = standardize([r["reward"] for r in g["runs"]])
def term(probability, bit, old_selected, advantage):
selected = probability if bit else 1 - probability
ratio = selected / old_selected
value = min(ratio * advantage, min(1.2, max(0.8, ratio)) * advantage)
saturated = ((advantage > 0 and ratio > 1.2)
or (advantage < 0 and ratio < 0.8))
derivative = 0.0 if saturated else ratio * advantage * (bit - probability)
return value, derivative
def objective_and_gradient(zs):
ps = [sigmoid(z) for z in zs]
total, gradient = 0.0, [0.0, 0.0, 0.0]
for g, advantage in zip(groups, plan_advantages):
value, derivative = term(ps[0], g["bit"], g["old"], advantage)
total += value / len(groups)
gradient[0] += derivative / len(groups)
for record, action_advantage in zip(g["runs"], g["advantages"]):
value, derivative = term(ps[g["slot"]], record["bit"],
record["old"], action_advantage)
weight = 1 / (len(groups) * len(g["runs"]))
total += weight * value
gradient[g["slot"]] += weight * derivative
return total, gradient
_, gradient = objective_and_gradient(old_z)
new_z = [z + 0.3 * g for z, g in zip(old_z, gradient)]
new = [sigmoid(z) for z in new_z]
def use(ps, draws):
plan = "P" if draws[0] < ps[0] else "Q"
slot = 1 if plan == "P" else 2
action = "a" if draws[1] < ps[slot] else "b"
return plan, action, outcome(plan, action)
def expected_return(ps):
return ps[0] * ps[1] + (1 - ps[0]) * 0.4
assert use(old, (0.52, 0.51)) == ("Q", "b", 0.4)
assert use(new, (0.52, 0.51)) == ("P", "a", 1.0)
assert standardize([0.4] * 4) == [0.0] * 4
print(gradient)
print(new, expected_return(old), expected_return(new))
The gradient is (0.5,sqrt(3)/8,0). One ascent step of 0.3 gives (p,s,t)=(0.5374298453,0.5162322701,0.5). The complete policy’s expected single-use return under the supplied model increases from 0.45 to 0.4624666909. The later run uses both the changed strategy probability and the changed conditional action probability. One can choose P,a directly from the known deterministic table. The update illustrates how strategy and execution probabilities change together.
This run consumes two strategy generations and eight task executions before the two displayed later executions. A language-model trial must also budget generated tokens, resets, feedback and any selected judges. 26.4.2 makes that allocation explicit. Embedding-based strategy selection or a self-judge adds its own computation and qualification; neither facility is needed by this finite version. Continue training with the current-to-old ratios and chosen clipping rule, refreshing rollout policy and experience when required by the recipe.