Skip to content

Deep reinforcement learning and decision models · Method reference · DRL001 · Algorithm

Deep Q-network (DQN)

Learn action values directly from observations, using a neural network to choose actions that maximize expected discounted reward.

Runs in CodeSplash (Experimental) Component demo, not a trained model CodeSplash's run and the reference's record

In CodeSplash

A CodeSplash method runs this entry in Python and R. Set it up from the method's page. The methods that run it are Experimental.

Do it in CodeSplash Run it on your own data. It's in the Experimental lane in the app.

Availability
Runs in CodeSplash (Experimental)
Code example
Component demo, not a trained model
Who ran it
CodeSplash's run and the reference's record
CodeSplash's run
Verified in Python and R through decide.rl_dqn. The reference's example ran in Python and R here, and they agree.
The reference's record
Python: Ran in the reference's review. The report doesn't record which version of the code ran. R: Reviewed but not run: R wasn't available to the reference's authors.

Purpose

Learn action values directly from observations, using a neural network to choose actions that maximize expected discounted reward. DQN is a foundational discrete-action control method, not an unsupervised clustering model or a statistical hypothesis test.

Question / hypotheses

Which action should be taken in a state to maximize long-run return? The learning objective is Bellman-error reduction coupled to exploration; it does not provide a universal null hypothesis or a calibrated p-value.

Architecture / computational structure

An observation encoder, classically a convolutional stack for Atari frames, feeds one Q-value per discrete action. An online network is trained from a replay buffer; a delayed target network supplies bootstrap values. Frame stacking or recurrence may be needed for partial observations.

Assumptions

A Markov state or adequate history representation, a stable reward definition, and sufficient exploratory coverage are needed. Replay data can come from older policies, but unrestricted extrapolation to unseen states/actions is unreliable. Environment termination must be distinguished from time-limit truncation.

Formula / computational definition

y=r+γ(1−d)max⁡a′Qθ−(s′,a′)y=r+\gamma(1-d)\max_{a^\prime}Q_{\theta^-}(s^\prime,a^\prime) and L(θ)=E[(Qθ(s,a)−y)2]\mathcal L(\theta)=\mathbb E[(Q_\theta(s,a)-y)^2]. Here d=1d=1 denotes a genuine terminal transition and the target is treated as fixed during this gradient update; robust TD losses are common variants.

Parameters and notation

Learned parameters are encoder and Q-head weights; target weights are copied or averaged from them. Hyperparameters include discount, replay capacity, warm-up, batch size, target-update period, learning rate, exploration schedule, gradient clipping, and reward preprocessing. Action count fixes the output width.

Fitting, fine-tuning, and adaptation

Warm-start an encoder or Q-network only when observation and action meanings align. On a changed reward or task, reset or carefully adapt the value head, refresh replay, and synchronize target weights. Compare frozen-encoder and fully updated variants using validation environments; deployment changes need new evaluation.

How to use and calibrate

Evaluate return distributions across independent training seeds and separate evaluation episodes, with exploration behavior stated. Track TD error, action coverage, and value scale; a decreasing training loss is not sufficient evidence of a good policy. Match environment versions and frame budgets in comparisons.

Example application

Choose joystick actions from a small history of game frames. The code calculates one minibatch Bellman target and squared TD loss from supplied network outputs; it does not collect trajectories or train a convolutional controller.

Interpretation / pitfalls

Watch out

Maximization can overestimate noisy values, and bootstrapping with function approximation can diverge. Reward clipping changes the optimized return scale. A large replay buffer can retain obsolete dynamics; excessive target delay can slow adaptation. This original method is not the default answer for continuous actions.

Python and R code examples

Scope: computational component described in the application subsection; not a complete estimator, neural trainer, or environment loop. Dependencies/validation: Python with NumPy executed successfully; base R not executed. Targets and supplied network outputs are ordinary arrays here, so this does not validate automatic-differentiation boundaries.

Python and R

The reference's own programs, shown as written. A component demo shows one central piece of the method; it doesn't train a full model.

Python

Component demo, not a trained model — computational component described in the application subsection; not a complete estimator, neural trainer, or environment loop. · needs numpy

import numpy as np
next_q=np.array([[2.,1.],[.5,1.5],[4.,3.]])
reward=np.array([1.,0.,-1.]); terminal=np.array([0.,1.,0.]); chosen_q=np.array([.8,.2,2.])
target=reward+.99*(1-terminal)*next_q.max(axis=1)
loss=np.mean((chosen_q-target)**2)
assert target[1]==reward[1]; print(target,loss)
R

Component demo, not a trained model — computational component described in the application subsection; not a complete estimator, neural trainer, or environment loop.

next_q <- matrix(c(2,1,.5,1.5,4,3),ncol=2,byrow=TRUE)
reward <- c(1,0,-1); terminal <- c(0,1,0); chosen_q <- c(.8,.2,2)
target <- reward+.99*(1-terminal)*apply(next_q,1,max)
loss <- mean((chosen_q-target)^2)
stopifnot(target[2]==reward[2]); print(target); print(loss)

Sources

From the CodeSplash reference, entry DRL001.