Reinforcement Learning (10): Offline Reinforcement Learning
Chen Kai BOSS

Traditional reinforcement learning relies on online interaction between agents and environments — collecting experience through trial and error to gradually optimize policies. However, in many real-world scenarios, online interaction is costly or even infeasible: autonomous vehicles cannot freely explore on real roads, medical AI cannot conduct dangerous experiments on patients, and robot errors in production environments can cause massive losses. More importantly, many domains have already accumulated vast amounts of historical data — medical records, traffic logs, user behavior data — and if we could learn from this offline data, the deployment barrier for RL would dramatically lower. Offline reinforcement learning (Offline RL, also known as Batch RL) studies how to learn policies from fixed datasetswithout further environment interaction. This seemingly simple task is actually full of challenges: data distribution mismatches with the optimal policy's distribution (distributional shift), Q-functions produce unreliable estimates on unseen actions (extrapolation error), leading to catastrophic failure of learned policies. From Conservative Q-Learning's pessimistic estimation to Decision Transformer reframing RL as sequence modeling, Offline RL's methodology demonstrates how to safely learn under data constraints. This chapter systematically examines Offline RL's core challenges and solutions, and helps you implement the CQL algorithm through complete code.

Motivation and Challenges of Offline Reinforcement Learning

Why Do We Need Offline RL?

Limitations of Online RL: - Safety: Exploration may produce dangerous behaviors (e.g., autonomous vehicle crashes, medical misdiagnosis) - Cost: Interaction with real environments is expensive (e.g., industrial robot wear, data center electricity costs) - Efficiency: Learning from scratch wastes existing data (e.g., historical user logs, expert demonstrations)

Advantages of Offline RL: - Utilizes existing data without online exploration - Can learn from suboptimal or even random policy data - Supports counterfactual reasoning: "What would have happened if a different action was chosen?"

Application Scenarios: - Healthcare: Learning treatment policies from electronic medical records - Recommendation Systems: Optimizing recommendation algorithms from user historical behavior - Autonomous Driving: Learning safe policies from human driving logs - Robotics: Rapid policy initialization from demonstration data

Core Challenge 1: Distributional Shift

The datasetis generated by behavior policy, but we want to learn the optimal policy. The state-action distributions of both differ:

Problem: When the learned policyattempts to make decisions onthatrarely visits, Q-function estimates become unreliable.

Example: Supposeis a human driver who always slows down at yellow lights. Iflearns "accelerate at yellow lights," but there are almost no such samples in the data, the Q-function cannot accurately evaluate its value and may severely overestimate.

Core Challenge 2: Extrapolation Error

Q-learning updates through the Bellman equation:The problem lies in: ifis overestimated on unseen, this overestimation propagates through the Bellman operator to the entire state space.

Mathematically: Define extrapolation error as:For samples:

Consequence: The learned policymay select actions never appearing in data, leading to catastrophic failure.

Core Challenge 3: Value Overestimation

In online RL, overestimating Q-values is corrected through exploration — agents try overestimated actions, discover actual returns are low, and update Q-functions. But in Offline RL, without new exploration, overestimation cannot be corrected.

Double Q-learning's Insufficiency: Although Double Q alleviates maximization bias, it's still insufficient in Offline settings — because the problem isn't algorithmic randomness, but insufficient data coverage.

Conservative Q-Learning (CQL)

Core Idea: Pessimistic Estimation

CQL's strategy is: conservatively estimate Q-values within data distribution, severely penalize high Q-values outside data distribution. This forces policies to select only actions sufficiently supported by data.

CQL's Objective Function

Standard Q-learning optimizes Bellman error:CQL adds a conservative regularization term:

First term: maximizes expected Q-values of all actions (similar to softmax log-sum-exp).

Second term: minimizes Q-values of actions in data.

Effect: - Forappearing in data: second term reduces their Q-values - Fornot appearing in data: first term increases their Q-values, but since they're not in data, second term cannot offset, ultimately getting penalized

Intuition: CQL says: "I will penalize Q-values of actions I'm uncertain about, only trusting actions seen in data."

CQL Variants

CQL(H): Replaces uniform distribution with policy's expectation:This more directly penalizes actions selected by policyoutside data.

CQL(R): Adds importance weights, adjusting distribution:whereis the proposal distribution (e.g., behavior policy).

Theoretical Guarantees

CQL proves: under certain regularization strength, the learned Q-functionis a lower bound of the true Q-function:wheremeasures the difference betweenand data distribution. This pessimistic estimation guarantees the safety of policy improvement.

Batch-Constrained Q-Learning (BCQ)

Core Idea: Behavior Cloning Constraint

BCQ argues: policyshould only select actions that behavior policywould choose, avoiding extrapolation error.

BCQ Architecture (Continuous Actions)

  1. VAE Models Behavior Policy: Train a variational autoencoder (VAE) to reconstruct actions in data:whereis a latent variable. Decoderlearns to generate behavior policy actions.

  2. Policy Constrained Within VAE's Support:wheresamples from VAE,is a small perturbation network (ensuring action remains near behavior policy).

  3. Q-function Update: Similar to standard Q-learning, butin target is replaced with:

BCQ's Advantages and Limitations

Advantages: - Explicitly models behavior policy, easy to understand - Excellent performance in continuous action spaces

Limitations: - Overly conservative — ifis a suboptimal policy, BCQ struggles to surpass it (because constrained near) - VAE training unstable, especially in high-dimensional action spaces

Implicit Q-Learning (IQL)

Core Idea: Avoiding Dynamic Programming

IQL observes: Q-learning's problem stems from— this maximization selects out-of-distribution actions. What if we don't explicitly maximize, but use the expectation Bellman equation?

IQL's Objective Function

IQL learns three functions: 1. Q-function

  1. Value function

  2. Policy Q-function Update (expectile regression):whereis asymmetric squared loss:𝟙When(e.g., 0.7), this loss penalizes positive errors less than negative errors, causing Q-values to be pulled toward upper quantiles.

Value Function Update (expectile of Q): learns the upper quantile of Q-values (e.g., 70th percentile), approximating.

Policy Update (weighted behavior cloning):Weight: - If: this action is better than average, increase its probability - If: this action is worse, decrease its probability

IQL's Advantages

No Dynamic Programming:update doesn't depend on, avoiding extrapolation error.

Flexibility: Control conservativeness by adjusting: -: median, similar to standard expectation -: upper quantile, more optimistic -: extremely optimistic, close to maximum

Experimental Performance: IQL outperforms CQL and BCQ on many D4RL benchmark tasks, with more stable training.

Decision Transformer: Sequence Modeling Perspective

Redefining RL

Decision Transformer (DT) proposes a revolutionary view: RL is a sequence modeling problem, not a dynamic programming problem.

Given trajectory, whereis future return, DT trains a Transformer to predict actions:

Key: DT doesn't learn value functions, only learns "under target return, what actionshould be chosen at state".

DT Architecture

Input Sequence:whereis desired return (actual return during training, manually set high return like "achieve highest score" during testing).

Embeddings: - Return embedding: - State embedding: - Action embedding: - Positional encoding: Transformer: - Self-attention layers process sequence - Output predicts next action Loss:Only supervised learning, no TD error or Bellman equation needed.

DT's Advantages and Limitations

Advantages: - Simple: No value function, target network, or experience replay — just supervised learning - Avoids Bootstrapping: No error propagation, unaffected by extrapolation error - Controllable: Specify desired return during testing, control policy behavior (e.g., "pursue high score" vs "pursue safety")

Limitations: - Lack of Generalization: Can only reach maximum return in data, cannot exceed it - Long-term Dependencies: Transformer context length limitation (e.g., 512 steps) - No Causal Reasoning: Doesn't understand action-reward causality, only pattern matching

Subsequent Improvements: - Trajectory Transformer: Simultaneously predicts states and rewards, supports model planning - Q-learning Decision Transformer: Combines DT and Q-learning, supports online fine-tuning - Online Decision Transformer: Collects data online, continuously improves DT

Complete Code Implementation: CQL

Below implements CQL training on D4RL Gym environments (e.g., HalfCheetah). Includes: - CQL's conservative regularization term - Offline dataset loading - Q-function and policy network training

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
import torch
import torch.nn as nn
import torch.optim as optim
import torch.nn.functional as F
import numpy as np
import gym
import d4rl # pip install git+https://github.com/rail-berkeley/d4rl

# ============ Network Definitions ============
class QNetwork(nn.Module):
"""Twin Q-networks (reduce overestimation)"""
def __init__(self, state_dim, action_dim, hidden_dim=256):
super().__init__()
# Q1
self.q1 = nn.Sequential(
nn.Linear(state_dim + action_dim, hidden_dim),
nn.ReLU(),
nn.Linear(hidden_dim, hidden_dim),
nn.ReLU(),
nn.Linear(hidden_dim, 1)
)
# Q2
self.q2 = nn.Sequential(
nn.Linear(state_dim + action_dim, hidden_dim),
nn.ReLU(),
nn.Linear(hidden_dim, hidden_dim),
nn.ReLU(),
nn.Linear(hidden_dim, 1)
)

def forward(self, state, action):
sa = torch.cat([state, action], dim=1)
return self.q1(sa), self.q2(sa)

def q1_forward(self, state, action):
sa = torch.cat([state, action], dim=1)
return self.q1(sa)

class GaussianPolicy(nn.Module):
"""Gaussian policy (continuous actions)"""
def __init__(self, state_dim, action_dim, hidden_dim=256, log_std_min=-20, log_std_max=2):
super().__init__()
self.log_std_min = log_std_min
self.log_std_max = log_std_max

self.net = nn.Sequential(
nn.Linear(state_dim, hidden_dim),
nn.ReLU(),
nn.Linear(hidden_dim, hidden_dim),
nn.ReLU()
)
self.mean = nn.Linear(hidden_dim, action_dim)
self.log_std = nn.Linear(hidden_dim, action_dim)

def forward(self, state):
x = self.net(state)
mean = self.mean(x)
log_std = self.log_std(x)
log_std = torch.clamp(log_std, self.log_std_min, self.log_std_max)
std = torch.exp(log_std)
return mean, std

def sample(self, state):
mean, std = self.forward(state)
normal = torch.distributions.Normal(mean, std)
z = normal.rsample() # reparameterization trick
action = torch.tanh(z) # squash to [-1, 1]

# Compute log probability (correct tanh transformation)
log_prob = normal.log_prob(z) - torch.log(1 - action.pow(2) + 1e-6)
log_prob = log_prob.sum(dim=1, keepdim=True)

return action, log_prob

def get_action(self, state):
with torch.no_grad():
mean, std = self.forward(state)
action = torch.tanh(mean)
return action

# ============ CQL Agent ============
class CQLAgent:
def __init__(self, state_dim, action_dim, device='cpu',
lr=3e-4, gamma=0.99, tau=0.005, alpha=1.0, cql_weight=1.0):
self.gamma = gamma
self.tau = tau
self.alpha = alpha # Temperature parameter (auto-tuned)
self.cql_weight = cql_weight # CQL regularization weight
self.device = device

# Q networks
self.q_net = QNetwork(state_dim, action_dim).to(device)
self.target_q_net = QNetwork(state_dim, action_dim).to(device)
self.target_q_net.load_state_dict(self.q_net.state_dict())

# Policy network
self.policy = GaussianPolicy(state_dim, action_dim).to(device)

# Optimizers
self.q_optimizer = optim.Adam(self.q_net.parameters(), lr=lr)
self.policy_optimizer = optim.Adam(self.policy.parameters(), lr=lr)

# Auto-tune alpha
self.target_entropy = -action_dim
self.log_alpha = torch.zeros(1, requires_grad=True, device=device)
self.alpha_optimizer = optim.Adam([self.log_alpha], lr=lr)

def compute_cql_loss(self, states, actions, next_states):
"""CQL conservative regularization term"""
batch_size = states.size(0)

# Sample actions from policy
sampled_actions, _ = self.policy.sample(states)

# Random sample actions (to cover broader action space)
random_actions = torch.FloatTensor(batch_size, actions.size(1)).uniform_(-1, 1).to(self.device)

# Compute Q-values
q1_data, q2_data = self.q_net(states, actions)
q1_policy, q2_policy = self.q_net(states, sampled_actions)
q1_random, q2_random = self.q_net(states, random_actions)

# CQL loss: log-sum-exp of all actions' Q-values - data actions' Q-values
# Simplified here as: policy actions + random actions Q-values vs data actions Q-values
q1_all = torch.cat([q1_policy, q1_random], dim=0)
q2_all = torch.cat([q2_policy, q2_random], dim=0)

# logsumexp
q1_logsumexp = torch.logsumexp(q1_all, dim=0, keepdim=True)
q2_logsumexp = torch.logsumexp(q2_all, dim=0, keepdim=True)

cql_loss = (q1_logsumexp.mean() + q2_logsumexp.mean()) - (q1_data.mean() + q2_data.mean())

return cql_loss

def update(self, states, actions, rewards, next_states, dones):
"""Update Q-function and policy"""
states = torch.FloatTensor(states).to(self.device)
actions = torch.FloatTensor(actions).to(self.device)
rewards = torch.FloatTensor(rewards).unsqueeze(1).to(self.device)
next_states = torch.FloatTensor(next_states).to(self.device)
dones = torch.FloatTensor(dones).unsqueeze(1).to(self.device)

# ========== Update Q-function ==========
with torch.no_grad():
# Target Q-value: min(Q1, Q2) - alpha * log_prob
next_actions, next_log_probs = self.policy.sample(next_states)
target_q1, target_q2 = self.target_q_net(next_states, next_actions)
target_q = torch.min(target_q1, target_q2) - self.alpha * next_log_probs
target = rewards + self.gamma * (1 - dones) * target_q

# Current Q-values
current_q1, current_q2 = self.q_net(states, actions)

# Bellman loss
q_loss = F.mse_loss(current_q1, target) + F.mse_loss(current_q2, target)

# CQL conservative regularization term
cql_loss = self.compute_cql_loss(states, actions, next_states)

# Total loss
total_q_loss = q_loss + self.cql_weight * cql_loss

self.q_optimizer.zero_grad()
total_q_loss.backward()
self.q_optimizer.step()

# ========== Update Policy ==========
sampled_actions, log_probs = self.policy.sample(states)
q1_pi, q2_pi = self.q_net(states, sampled_actions)
min_q_pi = torch.min(q1_pi, q2_pi)

policy_loss = (self.alpha * log_probs - min_q_pi).mean()

self.policy_optimizer.zero_grad()
policy_loss.backward()
self.policy_optimizer.step()

# ========== Update alpha (auto-tune) ==========
alpha_loss = -(self.log_alpha * (log_probs + self.target_entropy).detach()).mean()

self.alpha_optimizer.zero_grad()
alpha_loss.backward()
self.alpha_optimizer.step()

self.alpha = self.log_alpha.exp().item()

# ========== Soft update target network ==========
for param, target_param in zip(self.q_net.parameters(), self.target_q_net.parameters()):
target_param.data.copy_(self.tau * param.data + (1 - self.tau) * target_param.data)

return {
'q_loss': q_loss.item(),
'cql_loss': cql_loss.item(),
'policy_loss': policy_loss.item(),
'alpha': self.alpha
}

def select_action(self, state):
"""Select action (testing)"""
state = torch.FloatTensor(state).unsqueeze(0).to(self.device)
action = self.policy.get_action(state)
return action.cpu().numpy()[0]

# ============ Train CQL ============
def train_cql(env_name='halfcheetah-medium-v2', num_steps=100000, batch_size=256):
# Load environment and offline data
env = gym.make(env_name)
dataset = d4rl.qlearning_dataset(env)

state_dim = env.observation_space.shape[0]
action_dim = env.action_space.shape[0]

print(f"Environment: {env_name}")
print(f"State dimension: {state_dim}, Action dimension: {action_dim}")
print(f"Dataset size: {len(dataset['observations'])}")

device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
agent = CQLAgent(state_dim, action_dim, device=device)

# Training
for step in range(num_steps):
# Sample batch
indices = np.random.randint(0, len(dataset['observations']), batch_size)
states = dataset['observations'][indices]
actions = dataset['actions'][indices]
rewards = dataset['rewards'][indices]
next_states = dataset['next_observations'][indices]
dones = dataset['terminals'][indices]

# Update
info = agent.update(states, actions, rewards, next_states, dones)

# Logging
if step % 1000 == 0:
print(f"Step {step}: Q_loss={info['q_loss']:.4f}, CQL_loss={info['cql_loss']:.4f}, "
f"Policy_loss={info['policy_loss']:.4f}, Alpha={info['alpha']:.4f}")

# Evaluation
if step % 10000 == 0 and step > 0:
eval_rewards = []
for _ in range(10):
state = env.reset()
episode_reward = 0
done = False
while not done:
action = agent.select_action(state)
state, reward, done, _ = env.step(action)
episode_reward += reward
eval_rewards.append(episode_reward)

avg_reward = np.mean(eval_rewards)
normalized_score = env.get_normalized_score(avg_reward) * 100
print(f"=== Evaluation Step {step}: Average Return={avg_reward:.2f}, D4RL Score={normalized_score:.2f} ===")

return agent

# ============ Main Program ============
if __name__ == "__main__":
# Train CQL
agent = train_cql(env_name='halfcheetah-medium-v2', num_steps=100000, batch_size=256)

# Save model
# torch.save(agent.policy.state_dict(), 'cql_policy.pth')

Code Analysis

Network Components: - QNetwork: Twin Q-networks (Q1 and Q2), reduce overestimation - GaussianPolicy: Gaussian policy, outputs mean and standard deviation, samples actions using reparameterization trick, and corrects log probability for tanh transformation

CQL Core: - compute_cql_loss: - Sample actions from policy - Random sample actions - Compute, where - Subtract Q-values of actions in data - This difference is CQL's conservative penalty

Update Process: 1. Q-function: Bellman loss + CQL loss 2. Policy: Maximize(standard SAC objective) 3. Alpha: Auto-tune temperature parameter, balancing exploration and exploitation 4. Target Network: Soft update Training: - Sample batches from D4RL dataset - Update for 100k steps - Evaluate 10 episodes every 10k steps, compute D4RL normalized score

Performance: - HalfCheetah-medium-v2: CQL achieves approximately 45-50 score (max 100) - Walker2d-medium-expert-v2: CQL achieves approximately 110 score

In-Depth Q&A

Q1: Why is CQL's Conservative Regularization Effective?

Mathematical Intuition: CQL's goal is to learn a lower bound of the Q-function, ensuring policy improvement doesn't overestimate unseen actions.

The regularization termcan be rewritten as: (through the relationship between softmax log-sum-exp and expectation).

Effect: - First term increases average Q-values of all actions - Second term decreases Q-values of actions in data - Result: Q-values of out-of-distribution actions are relatively increased, but since the second term cannot offset them, they ultimately get penalized

Experimental Verification: Papers show that CQL's learned Q-values are 10-20% lower than true Q-values on data distribution, but 50%+ lower outside data — exactly the pessimistic estimation we want.

Q2: Why Does BCQ Use VAE Instead of Simple Behavior Cloning?

Problem: Simple behavior cloning learns, but in continuous action spaces, it's difficult to precisely match distributions —may generate actions never seen by.

VAE's Advantages: 1. Explicit Density Model: VAE's decodermodels's conditional density, sampled actionsare guaranteed within's support 2. Smooth Interpolation: Latent variableprovides continuous representation space, allowing smooth interpolation between behavior policy actions 3. Perturbation Mechanism: Perturbation networkcan make small corrections on top of VAE, improving Q-values without straying too far from data distribution

Disadvantages: VAE training is complex, especially in high-dimensional action spaces (e.g., robot control) requiring extensive hyperparameter tuning.

Q3: Why Doesn't IQL Need Explicit Dynamic Programming?

Key Insight: IQL uses expectile regression to learn upper quantiles of Q-values, rather than maximum values. This avoids the extrapolation of.

Mathematically: The expectile regression objective is:When,approximates the 70th percentile of. If most actions in data are good,will approach; if data contains bad actions,will ignore them, focusing only on the good parts.

Advantages: - No need to select specific, avoiding the dilemma of "which action to choose" -is a weighted average of Q-values in data, won't extrapolate outside data

Experiments: IQL outperforms CQL on many D4RL tasks, especially on tasks with poor data quality (e.g., antmaze-medium-play).

Q4: Is Decision Transformer Really RL?

Controversy: DT doesn't learn value functions, doesn't use Bellman equations, doesn't do policy improvement — does it even count as RL?

Supporters: - The essence of RL is learning policies to maximize returns, not specific algorithms (like TD learning) - DT learns "how to achieve target returns" by conditioning on returns, which is policy optimization

Opponents: - DT is conditional behavior cloning, can only mimic trajectories in data, cannot discover new policies - RL should involve credit assignment (which action led to returns), while DT is just sequence prediction

Compromise View: DT is "implicit RL"— it doesn't explicitly optimize values, but achieves similar effects through supervised learning. It's effective in Offline settings, but not suitable for online learning or tasks requiring long-term planning.

Q5: When Does Offline RL Fail?

Scenario 1: Insufficient Data Coverage - If dataset only contains expert trajectories, policy never learns "how to recover from mistakes" - During testing, policy makes a small error, enters unseen state, then catastrophically fails

Scenario 2: Extremely Poor Data Quality - If all data is generated by random policy, Offline RL struggles to learn anything useful - CQL becomes overly conservative, BCQ clones random behavior, DT mimics random trajectories

Scenario 3: Excessive Distributional Shift - If test environment differs from training data environment (e.g., physical parameter changes), policy generalization fails - Offline RL has no exploration mechanism, cannot adapt to new environments

Solutions: - Hybrid RL: Offline pretraining, then Online fine-tuning - Conservative Exploration: After Offline learning, use low-risk exploration to improve policy - Model-Assisted: Learn environment model, simulate exploration in model

Q6: How to Choose CQL's Hyperparameter?

Theoretical Guidance: Paper provesshould be proportional to distributional shift:

In Practice: - Good data quality (e.g., expert data):, slightly conservative - Medium data quality (e.g., medium data):, moderately conservative - Poor data quality (e.g., random data):, extremely conservative

Auto-tuning: Latest CQL versions use Lagrange multiplier method to automatically adjust, keeping policy distribution's KL divergence from data distribution within target range.

Q7: Difference Between Offline RL and Imitation Learning?

Imitation Learning: - Learn expert policy, objective is - Only uses expert demonstrations, doesn't consider rewards - Cannot surpass expert

Offline RL: - Learn optimal policy, objective is - Uses any data (expert, suboptimal, mixed), utilizes reward signals - May surpass policies in data (by stitching together excellent segments from different trajectories)

Example: Suppose dataset contains: - Expert performs well in first half of game - Novice accidentally discovers high-score technique in second half

Imitation learning only learns expert's first half, ignoring second half; Offline RL learns both, combining into better policy.

Q8: Why is Offline RL Difficult in Robotics?

Challenge 1: Partial Observability - Limited robot sensors (e.g., camera field of view, tactile range) - Incomplete state representation, requires memory or state estimation - Offline data lacks exploration, cannot cover all hidden states

Challenge 2: High-Dimensional Continuous Control - Large robot action spaces (e.g., 7-DOF robot arm) - Distributional shift more severe in continuous control - BCQ's VAE unstable in high-dimensional spaces

Challenge 3: Physical Constraints - Real robots have dynamics constraints, collision detection, stability requirements - Offline policies may output unsafe actions (e.g., excessive torque) - Need additional safety layer

Solutions: - Generate large amounts of data using simulator (sim-to-real) - Offline pretraining + Online fine-tuning (safe learning first, then cautious exploration) - Incorporate expert knowledge (e.g., physical priors, safety constraints)

Q9: How Does Decision Transformer's "Return Conditioning" Work?

During Training: - Input real trajectories, whereis actual obtained return - Model learns: "When I want return, at stateI should do"

During Testing: - Manually set high return(e.g., maximum return in data) - Input, model predicts - Execute, observe, compute remaining return - Repeat until episode ends

Intuition: Model internalizes "different return goals correspond to different behaviors"— low return corresponds to conservative policy, high return corresponds to aggressive policy. During testing, specifying high return makes model behave like expert.

Limitation: If data lacks high-return trajectories, whenexceeds data range, model extrapolation fails.

Q10: Future Directions for Offline RL?

1. Combining with Online RL: - Offline pretraining provides initialization, Online fine-tuning improves performance - How to balance both? When to switch?

2. Multimodal Data: - Utilize video, text, multi-sensor data - Combine with large models (like GPT), use language to guide policies

3. Causal Reasoning: - Infer action-reward causality from data - Counterfactual reasoning: "What would have happened if another action was chosen?"

4. Interpretability: - Why does policy choose this action? - Which data samples are most important for learning?

5. Theoretical Guarantees: - Stricter convergence analysis - Sample complexity bounds - Safety guarantees (avoiding catastrophic failures)

Q11: How to Handle Multi-Modal Behavior Policies in Offline Data?

Problem: Real-world datasets often contain data from multiple behavior policies — expert demonstrations, suboptimal human behavior, automated exploration — each with different characteristics. How should Offline RL handle this heterogeneity?

CQL's Approach: CQL's conservative regularization naturally handles multi-modal data. By penalizing Q-values for all out-of-distribution actions uniformly, it doesn't explicitly model which behavior policy generated which data. This makes CQL robust to data heterogeneity but potentially overly conservative.

BCQ's Challenge: BCQ's VAE must model the entire behavior distribution. For multi-modal data, the VAE might: - Learn a mixture distribution covering all modes - Focus on dominant modes, ignoring minority behaviors - Struggle with mode collapse in high-dimensional spaces

Solution: Use mixture of VAE models or conditional VAE where latent codes explicitly capture policy identity.

IQL's Natural Fit: IQL's expectile regression elegantly handles multi-modal data. By learning upper quantiles of Q-values, it automatically focuses on better actions regardless of which behavior policy generated them. This makes IQL particularly effective on heterogeneous datasets.

Practical Recommendation: For datasets with known multiple behavior policies, consider: - Explicitly conditioning policies on behavior ID (if available) - Using hierarchical models with policy-specific components - Weighting samples based on estimated behavior policy quality

Q12: What is the Role of Model-Based Methods in Offline RL?

Pure Model-Free Challenges: Model-free Offline RL (like CQL, BCQ, IQL) must be extremely conservative because they cannot verify policy performance without environment interaction.

Model-Based Advantages: 1. Uncertainty Quantification: Learn environment modelwith uncertainty estimates (e.g., ensemble models, Bayesian neural networks) 2. Safe Exploration in Model: Use model to simulate policy rollouts, detect potentially dangerous actions before deployment 3. Data Augmentation: Generate synthetic transitions to improve data coverage

MOReL (Model-Based Offline RL): - Learn ensemble of dynamics models from offline data - Use model disagreement to identify uncertain regions - Add penalty for high-uncertainty transitions - Plan using penalized model

MOPO (Model-based Offline Policy Optimization):whereis model uncertainty. This creates "pessimistic MDP" where uncertain transitions are penalized.

Limitations: - Model learning errors compound during long rollouts - High-dimensional state spaces (e.g., images) challenge model accuracy - Computational overhead of model training and planning

Best of Both Worlds: Combine model-free conservatism (CQL) with model-based uncertainty quantification — use models for short-horizon planning within conservative Q-function guidance.

Q13: How Does Offline RL Scale to Large-Scale Real-World Datasets?

Computational Challenges: - Real-world datasets may contain millions to billions of transitions (e.g., entire fleets of autonomous vehicles, years of recommendation logs) - Standard Offline RL requires multiple passes through entire dataset - Neural network training becomes bottleneck

Solutions:

1. Prioritized Sampling: - Not all data equally valuable - Prioritize high-reward trajectories, diverse states, or high TD-error samples - Reduces effective dataset size while maintaining performance

2. Representation Learning: - Pre-train state encoders on large dataset (self-supervised learning) - Fine-tune RL on encoded representations - Particularly effective for high-dimensional observations (images, text)

3. Distributed Training: - Parallelize Q-function updates across multiple GPUs - Use distributed replay buffers - Frameworks like Acme and RLlib support distributed Offline RL

4. Dataset Distillation: - Synthesize smaller "distilled" dataset that captures essential information - Train Offline RL on distilled dataset - Recent work shows 10-100x dataset compression with minimal performance loss

5. Continual Learning: - As new data arrives, incrementally update policies - Avoid catastrophic forgetting of previously learned behaviors - Use regularization (EWC, PackNet) or memory buffers

Real-World Success: Companies like Waymo and Cruise use Offline RL on massive driving datasets (petabytes) by combining all these techniques — distributed training on pre-learned representations with careful data prioritization.

Core Papers

  1. CQL:
    Kumar et al. (2020). "Conservative Q-Learning for Offline Reinforcement Learning". NeurIPS.
    https://arxiv.org/abs/2006.04779

  2. BCQ:
    Fujimoto et al. (2019). "Off-Policy Deep Reinforcement Learning without Exploration". ICML.
    https://arxiv.org/abs/1812.02900

  3. IQL:
    Kostrikov et al. (2021). "Offline Reinforcement Learning with Implicit Q-Learning". ICLR.
    https://arxiv.org/abs/2110.06169

  4. Decision Transformer:
    Chen et al. (2021). "Decision Transformer: Reinforcement Learning via Sequence Modeling". NeurIPS.
    https://arxiv.org/abs/2106.01345

  5. D4RL Benchmark:
    Fu et al. (2020). "D4RL: Datasets for Deep Data-Driven Reinforcement Learning". arXiv.
    https://arxiv.org/abs/2004.07219

  6. AWAC:
    Nair et al. (2020). "Accelerating Online Reinforcement Learning with Offline Datasets". arXiv.
    https://arxiv.org/abs/2006.09359

  7. TD3+BC:
    Fujimoto & Gu (2021). "A Minimalist Approach to Offline Reinforcement Learning". NeurIPS.
    https://arxiv.org/abs/2106.06860

  8. MOPO:
    Yu et al. (2020). "MOPO: Model-based Offline Policy Optimization". NeurIPS.
    https://arxiv.org/abs/2005.13239

  9. MOReL:
    Kidambi et al. (2020). "MOReL: Model-Based Offline Reinforcement Learning". NeurIPS.
    https://arxiv.org/abs/2005.05951

Benchmarks and Code

Summary

Offline reinforcement learning transforms RL from the "learning while doing" online paradigm to the "learning from historical data" offline paradigm, dramatically lowering deployment barriers. But this also brings new challenges: distributional shift, extrapolation error, value overestimation — these problems force us to rethink RL's fundamental principles.

CQL uses pessimistic estimation to ensure Q-functions are conservative outside data, preventing policies from selecting unseen actions.

BCQ explicitly models behavior policy through VAE, constraining policy near data distribution, preventing extrapolation.

IQL avoids dynamic programming, using expectile regression to learn upper quantiles of Q-values, bypassing the pitfalls ofoperation.

Decision Transformer reframes RL as sequence modeling, using Transformers to directly learn return-conditioned policies, freeing itself from value function constraints.

Future Offline RL will deeply integrate with online RL, imitation learning, and causal reasoning, becoming the core technology for learning intelligent decision-making from large-scale data — from healthcare to autonomous driving, from recommendation systems to robotics, Offline RL is opening a new era of AI applications.

  • Post title:Reinforcement Learning (10): Offline Reinforcement Learning
  • Post author:Chen Kai
  • Create time:2024-09-20 16:15:00
  • Post link:https://www.chenk.top/reinforcement-learning-10-offline-reinforcement-learning/
  • Copyright Notice:All articles in this blog are licensed under BY-NC-SA unless stating additionally.
 Comments