Q-learning
Neural Networks
System Update
Machine Learning
AI Upgrade

Updating an old system to Q-learning with Neural Networks

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Upgrading an older decision system from tabular Q-learning to a neural-network-based approach is not just a model swap. You are replacing a fixed lookup table with function approximation, which changes how the system generalizes, how it trains, and how it can fail.

Why the Old Table Stops Working

Tabular Q-learning works well when the state space is small and discrete. It becomes awkward when:

  • states are high-dimensional
  • features are continuous
  • similar states should share information
  • you cannot realistically visit every state-action pair often enough

That is the usual reason teams consider a neural network: the table no longer scales to the state representation the system now needs.

What Changes With a Neural Network

In tabular Q-learning, each Q(state, action) entry is stored explicitly.

With a neural network, you train a function approximator:

  • input: state features
  • output: one Q-value per action
  • training target: Bellman-style target values

That gives you generalization across similar states. It also introduces instability that the simple table did not have.

A Minimal Q-Network Example

python
1import torch
2import torch.nn as nn
3
4class QNetwork(nn.Module):
5    def __init__(self, state_dim, action_dim):
6        super().__init__()
7        self.net = nn.Sequential(
8            nn.Linear(state_dim, 64),
9            nn.ReLU(),
10            nn.Linear(64, 64),
11            nn.ReLU(),
12            nn.Linear(64, action_dim),
13        )
14
15    def forward(self, x):
16        return self.net(x)
17
18model = QNetwork(state_dim=8, action_dim=4)
19print(model(torch.randn(1, 8)))

This replaces the explicit Q-table with a model that predicts action values from feature vectors.

Do Not Migrate Everything at Once

A safe upgrade path is incremental.

Good sequence:

  • keep the current environment and reward logic unchanged
  • replace only the Q-value representation first
  • evaluate offline on logged transitions or simulation
  • compare decisions against the old policy
  • only then consider live deployment or online learning

If you change reward shaping, state encoding, and the learning algorithm all at once, you lose the ability to identify which change caused regressions.

Training Looks Different From the Table Case

With a network, one update changes predictions for many nearby states, not just one exact state-action pair. That is why stabilizers matter.

python
1import torch
2import torch.nn.functional as F
3
4gamma = 0.99
5states = torch.randn(32, 8)
6next_states = torch.randn(32, 8)
7actions = torch.randint(0, 4, (32,))
8rewards = torch.randn(32)
9dones = torch.randint(0, 2, (32,), dtype=torch.float32)
10
11q_values = model(states)
12chosen_q = q_values.gather(1, actions.unsqueeze(1)).squeeze(1)
13
14with torch.no_grad():
15    next_q = model(next_states).max(dim=1).values
16    targets = rewards + gamma * next_q * (1 - dones)
17
18loss = F.mse_loss(chosen_q, targets)
19loss.backward()

This is the rough training shape, but a production-worthy system normally needs more than this basic loop.

Stabilize Before You Scale

The common stabilizers are:

  • replay buffer
  • target network
  • normalized input features
  • bounded rewards
  • careful learning rate choice

Without them, a direct table-to-network port often becomes unstable because the model is learning from targets generated by its own changing predictions.

If the old system has logs of transitions, use them. Offline replay is one of the safest ways to validate the new learner before production rollout.

Keep the Old Policy as a Baseline

The legacy system is valuable even if you plan to replace it.

Use it as:

  • a fallback policy
  • a regression baseline
  • a source of logged experience
  • a comparison target for offline evaluation

This is a systems migration, not just a machine-learning experiment. Keep the old system until the new one proves itself quantitatively.

Common Pitfalls

The biggest mistake is replacing the Q-table with a network while leaving the rest of the training assumptions unchanged.

Another mistake is going straight to online training without offline replay, simulation, or shadow evaluation.

A third issue is ignoring state normalization and reward scaling. Neural methods are far more sensitive to these than a plain table.

Finally, many migrations fail because the reward function is misaligned with the business objective. The network then learns exactly the wrong thing more efficiently than the old system did.

Summary

  • Move to neural Q-learning when tabular state-action storage no longer scales.
  • Treat the migration as a system redesign, not a drop-in replacement.
  • Start with a small Q-network and validate offline before production use.
  • Use replay, target networks, and normalized inputs to stabilize training.
  • Keep the old policy as a benchmark and fallback during rollout.
  • The hardest part is usually system design and reward definition, not writing the network class.

Course illustration
Course illustration

All Rights Reserved.