Learning Action Values From Experience
Botie may not know the transition probabilities. A sampled action, reward, and next state can still correct one entry in an action-value table.
15 sections · 6 questions · 3 coding challenges
The arrow keys move between sections too.
Replacing a Known Model With Sampled Transitions
BOTIE
This belongs to temporal-difference learning: an estimate is moved toward a target partly built from another estimate. Our tabular examples expose the update, including the important exception when a transition ends the episode.
Sources for this history
What If Nobody Gave Botie the Transition Table?
The planner in the previous lesson had a complete description of the little greenhouse. In a new environment, Botie may only know what happened after an action he tried. He needs a way to learn from that transition without pretending he has already measured every possible outcome.
We can keep one estimate for each state-action pair. After Botie tries an action, its reward gives us new evidence. If the task continues, our estimates for the next state contribute to the target too. We use that target to correct the entry for the action he actually took, and repeat as more experience arrives.
The transition is supplied so we can inspect one Q-learning update.
Step 1 of 4
Record the transition that happened
Botie tried one action in one state. Only that table entry receives this update.
- 1
Before taking the action
Stored estimate Q(s,a): 2 - 2
Take the action and observe
Reward received: 1 - 3
Inspect the state reached
- Next action A has value 4
- Next action B has value 6
Step 2 of 4
Build a target
With discount one half, the best next-state estimate contributes three, giving a target of four.
Best next action value = max(4, 6) = 6 Target = 1 + (0.5 × 6) = 4 Difference = 4 − 2 = 2
Step 3 of 4
Move partway toward it
With learning rate one quarter, the updated entry becomes two and a half.
Old estimate: 2 Update = 0.25 × 2 = 0.5 New estimate = 2 + 0.5 = 2.5
Step 4 of 4
A stopped recording is not always a terminal state
The terminal flag describes the environment, not just the end of the array we saved. Reaching a genuine end removes continuation value. Reaching a logging or time limit may still leave a next state with future rewards.
| Reason for stopping | Can the environment continue? | Continuation in the target |
|---|---|---|
| A true terminal state | No | Zero |
| Recording stopped at a time limit | Possibly | Requires the task's truncation convention |
A true terminal state
- Can the environment continue?
- No
- Continuation in the target
- Zero
Recording stopped at a time limit
- Can the environment continue?
- Possibly
- Continuation in the target
- Requires the task's truncation convention
Experience supplies a target for the visited action
The target combines observed reward with a permitted continuation. The learning rate scales the correction to the one visited estimate.
Which one estimate should this experience change?
Botie took one action and observed what followed. Compare an ordinary transition with a terminal one, where there is no future action value to add.
Choose an action and watch one Q entry change
BOTIE
The learning rate is 0.5 and discount is 0.9. You choose the behaviour actions; the Q-learning target uses the largest next-state estimate. This deterministic teaching environment is not a real watering controller.
Current state: dry. Updates made: 0.
| State | Water | Wait |
|---|---|---|
| Dry | 0 | 0 |
| Moist | 0 | 0 |
BOTIE
Updated value of the visited state-action pair
2.5
Follow the operations
1. Visited entry's old Q value
2
2. Learning rate
0.25
3. Observed reward
1
4. Discount
0.5
5. Next-state action values
- 04
- 16
6. Largest value
6
7. True terminal flag: 0 or 1
0
8. Stop at a true terminal state
6
9. Multiply
3
10. Add
4
11. Subtract
2
12. Multiply
0.5
13. Add
2.5
14. Result
2.5
The terminal flag must be zero or one, and alpha and gamma must lie in [0,1]. This is one tabular update, not a convergence demonstration.
1. Store Values by State and Action
The Q table has one row per state and one column per action. An entry estimates the expected discounted return from taking that action and then following the target behaviour.
The action taken to collect data may be exploratory. Q-learning's target uses a greedy continuation, so it is called off-policy: the data-collection policy and target policy need not be identical.
Q[state, action] → one estimated returnThe table size grows with the state and action spaces. A small tabular example is not a solution to arbitrary continuous control.
2. Build the Target Before Updating the Entry
For a nonterminal transition, add the observed reward to the discounted greatest action value in the next state. Subtract the old entry to obtain a temporal-difference error.
Multiply the error by the learning rate and add it back to the old entry. Compute the target from the old table before mutating it, especially when the next state equals the current state.
target = reward + gamma × max(Q[next_state])
error = target - Q[state,action]
Q_new[state,action] = Q[state,action] + alpha × errorA sampled target can be noisy, so the learning rate controls how much one transition changes the estimate.
3. Stop the Continuation at a True Terminal State
If the transition genuinely terminates the task, the target is just its reward. There is no next action whose return should be added.
A time limit imposed by a training script can instead truncate an ongoing process. Whether to bootstrap there depends on the problem definition and whether a valid continuing state is available. Treating all truncations as terminal changes the objective.
True terminal transition: target = rewardCount the final reward once, and do not add an arbitrary value from a terminal table row.
Separate the target from the size of the update
Botie receives a reward of two and reaches a nonterminal state. Its best current action estimate is eight, but the old estimate for the action just taken is only three.
import numpy as np
old_value = 3.0
next_values = np.array([4., 8.])
reward, gamma, rate = 2.0, 0.5, 0.5
target = reward + gamma * next_values.max()
correction = rate * (target - old_value)
print(float(target))
print(float(old_value + correction))6.0
4.5The target is six, while the half-sized correction only moves the estimate to four and a half. The table update in practice must change the visited entry and leave all others alone.
4. Keep Exploration and Evaluation Separate
Updating only visited pairs requires enough exploration to learn useful alternatives. Tabular convergence results require conditions such as adequate visitation and suitable learning-rate schedules; they do not promise success after a handful of fixed-rate trials.
Inspect learning across several runs, then evaluate the resulting policy without confusing exploratory mistakes with its intended deployment behaviour. The workshop isolates one update, while a replay can repeat transitions to show values changing over experience.
Function approximation, deep Q networks, and safe real-world control introduce additional concerns beyond this tabular lesson.
5. Give the Environment and the Learner Different Jobs
One table update is not yet a learner. We need something that supplies experience and something that changes its estimates from that experience. In the greenhouse example the environment keeps track of whether a bed is dry or moist. The learner keeps the Q table.
An environment step receives an action and returns a next state, a reward and a flag saying whether the task ended. It does not return the correct action value. The learner must estimate that value by repeating the task. Keep these roles separate in the code so a known environment rule does not accidentally become the answer.
State rows: 0 = dry, 1 = moist
Action columns: 0 = water, 1 = wait
Water: next state moist, reward -1, not terminal
Wait while dry: next state dry, reward -2, not terminal
Wait while moist: reward 3, terminalThese are invented rewards for a tiny task. Reward measures the objective we wrote down, not a universal definition of good behaviour.
6. Collect an Episode Instead of Replaying One Supplied Transition
At the start of an episode, reset the environment to dry while retaining the learned Q table. Choose an action, receive a transition, update the visited entry and continue until the task ends. This order matters: the next state used in the target is the state returned by the action just taken.
Use a NumPy array of shape (2,2) for Q. Keep state and action as integer indices. A tuple returned by the environment keeps the next state, reward and terminal flag together. A list of episode returns can record how training changes over time without replacing the table.
An epsilon-greedy behaviour policy sometimes chooses an action uniformly at random and otherwise chooses the largest entry in the current row. Early mistakes supply information. The greedy action can change after an update, so choose again at every step.
For each episode:
state = initial state
choose action from the current Q row
next_state, reward, terminal = environment(state, action)
update only Q[state, action]
continue from next_state unless terminalA training step cap prevents an endless toy episode. Reaching that cap is a truncation, not evidence that the environment returned a terminal state.
Check yourself
Questions on the Environment and the Episode
Question 1 of 2
What should be reset at the start of the next episode?
Use this setup.
The previous episode has ended. Q contains the estimates learned so far.7. Ask Whether Experience Changes a Later Decision
In the action widget, first water and then wait. The final reward changes the moist/wait entry. Begin another episode and water again. This time the target for the earlier action can use what was learned about the later opportunity. That is how delayed reward moves back through the table.
Run the same experiment in code over many episodes. Inspect the policy, which is the action selected in each state, as well as the numerical entries. A large value for one action is not enough if the competing action has never been tried.
For the greedy two-step route, gamma = 0.9:
Return from moist/wait = 3
Return from dry/water = -1 + 0.9 × 3 = 1.7The calculation provides an independent check for this deterministic environment. A learned table approaches these values through updates; the numbers are not assigned to it.
8. Evaluate Without Quietly Continuing to Train
Freeze the table and choose greedy actions for evaluation. Do not update Q from those evaluation transitions. Record whether the episode finishes, its reward and its length. For this tiny task, the useful policy waters a dry bed and waits at a moist one.
Repeat training with several seeds. In a stochastic environment, evaluate over multiple episodes too, because one lucky route can hide an unreliable policy. Compare with a simple policy such as always watering; a larger training return alone does not show that learning helped.
A larger state space would make a table impractical. That is the motivation for learning a function that predicts action values. Before introducing a deep Q network, retain the same distinction between experience collection, target calculation and independent evaluation.
The later model changes the representation of values. It does not remove the need to define the environment, explore and evaluate honestly.
Check yourself
Questions on Parts 1 to 8
Question 1 of 4
What is the updated Q value?
Use the information below.
Old Q=2, reward=1, best next Q=6, gamma=0.5, alpha=0.25. Transition is nonterminal.Try it with Botie
Build with Botie
Write and run Python