Jump to text
On this page
Copy and source
Published source confirmed at last check

Source changed 2026-11-23 00:01:58 UTC · snapshot created 2026-11-23 00:02:28 UTC · last check 2026-11-23 06:20:10 UTC

Engineering.Reference:26.4.4.2 - Execute continuations, obtain a first update and retain its result

The finite fixture has two sessions. In either, the supplied self-answering operation succeeds with probability 0.75. Consulting in the first session obtains a correct answer at cost 0.3 and stores a verified entry. Only this action writes memory. After context reset, the second question is related with probability 0.4. A fixed controller uses the stored answer if related; otherwise it self-answers. There are no other costs or discounts. The learned logit controls only the first consultation probability, initially 0.5; answer generation, retrieval and applicability recognition are supplied operations.

The code obtains future values by executing a declared population of twenty continuations from each of two boundary states: empty memory and retained verified entry. Eight questions are related and twelve are new. Each block of four supplied self-answering outcomes has three successes. The resulting continuation means are 0.75 and 0.85. All repetitions of each boundary state have the same future law in this fixture, so one set of twenty continuations can supply that state’s estimate. Count the forty continuations as additional training work. Each later use of an estimate relies on the same twenty continuations from that state.

from math import exp

def sigmoid(z):
    return 1 / (1 + exp(-z))

def first_session(consult, self_draw):
    # The fixture's expert is correct; only consultation stores its answer.
    memory = {"related": "verified answer"} if consult else {}
    reward = 0.7 if consult else float(self_draw < 0.75)
    return reward, {"memory": memory, "context": (), "remaining": 1}

def continue_session(state, query, self_draw):
    assert state["context"] == () and state["remaining"] == 1
    if query in state["memory"]:
        return 1.0
    return float(self_draw < 0.75)

def construct_update(related_count):
    assert 0 <= related_count <= 20 and related_count % 4 == 0
    self_draws = (0.1, 0.3, 0.6, 0.9)
    future_cases = [
        ("related" if i < related_count else "new", self_draws[i % 4])
        for i in range(20)]
    records = []
    for i, action_draw in enumerate((0.1, 0.2, 0.3, 0.4, 0.6, 0.7, 0.8, 0.9)):
        bit = int(action_draw < 0.5)
        reward, after = first_session(bool(bit), self_draws[i % 4])
        records.append({
            "history": ("first question", "empty memory", "two sessions left"),
            "bit": bit, "old_probability": 0.5, "reward": reward, "after": after})
    # These two state classes are equivalent within each class in this fixture.
    after_states = {r["bit"]: r["after"] for r in records}
    values = {
        bit: sum(continue_session(state, query, draw)
                 for query, draw in future_cases) / len(future_cases)
        for bit, state in after_states.items()}
    targets = [r["reward"] + values[r["bit"]] for r in records]
    before_value = sum(targets) / len(targets)
    signals = [target - before_value for target in targets]
    # One initial ascent step at the unchanged rollout policy; ratios equal 1.
    gradient = sum(a * (r["bit"] - 0.5)
                   for a, r in zip(signals, records)) / len(records)
    probability = sigmoid(0.3 * gradient)
    return probability, gradient, values, before_value, records

new_probability, gradient, values, baseline, records = construct_update(8)
assert abs(values[0] - 0.75) < 1e-12
assert abs(values[1] - 0.85) < 1e-12
assert abs(baseline - 1.525) < 1e-12
assert abs(gradient - 0.0125) < 1e-12

def use(probability, action_draw, future_query_draw, self_draws):
    consult = action_draw < probability
    first_reward, after = first_session(consult, self_draws[0])
    query = "related" if future_query_draw < 0.4 else "new"
    second_reward = continue_session(after, query, self_draws[1])
    return consult, bool(after["memory"]), first_reward + second_reward

assert use(0.5, 0.5005, 0.2, (0.9, 0.9)) == (False, False, 0.0)
assert use(new_probability, 0.5005, 0.2, (0.9, 0.9)) == (True, True, 1.7)
without_recurrence = construct_update(0)
assert abs(without_recurrence[1] + 0.0125) < 1e-12
print(new_probability, gradient, values, baseline)
print(without_recurrence[:4])

The eight first-session records contain four consultations and four self-answers. Their mean immediate rewards are 0.7 and 0.75. Adding the estimated continuation yields conditional total values 1.55 and 1.5; their equal old mixture is 1.525. The code centres the signals on this balanced empirical mixture. For sampled training data, select and qualify the value and baseline estimators used by the trainer. The balance in this example does not establish that a sample-based estimator is unbiased.

The gradient 0.0125 changes the consultation probability to 0.5009374989. Under the stated law, expected two-session return changes from 1.525 to 1.5250468749. The displayed later runs use draws near the changed decision boundary to expose the changed action and subsequent use of retained memory. They estimate no empirical improvement rate. The initial training run uses eight first sessions, including four consultations, and forty future continuations; the two later runs add four sessions and one consultation. The separate no-recurrence construction repeats those 48 training sessions under its changed condition.

The updated probability persists into the later run; context clears at the boundary and the stored answer persists. Distinguish the policy update, retained memory and cleared context when attributing a change in performance. If the experiment also removes the expert or changes memory retrieval, include those conditions in the comparison.