Skip to main content
MahiDev — Learn. Build. Become AI-ready.

Lesson 72

Dyna-Q

Learn a small model of the environment and replay imagined transitions so one real step teaches several updates. One example is on this page.

Intermediate12 min
Dyna-Q MahiDev

What You Will Learn

  • Idea
  • Example
  • When to use it

Idea

Dyna-Q does a normal Q-learning update on the real transition, and it stores what happened. It then samples stored transitions and updates Q again as if those experiences had just occurred. Planning and acting share the same Q-table.

Example

python
alpha, gamma = 0.5, 0.9
q = 0.0
q = q + alpha * (0.0 + gamma * 0.0 - q)
q = q + alpha * (1.0 + gamma * 0.0 - q)
print(f"Q after real + imagined: {q:.2f}")

Output:

text
Q after real + imagined: 0.50

When to use it

  • Use it when real steps are expensive and a simple model of past transitions is trustworthy.
  • The first update is the real reward of 0. The second is an imagined reward of 1 from the model.
  • A wrong model rehearses the wrong lesson. Planning does not fix a bad model.

Loading playground…