AI Wiki中文/EN

基于模型的强化学习:先学世界,再决定往哪走

Model-based reinforcement learning

从经验转移表做规划,算清模型误差、真实样本与模拟预算,识别未覆盖动作的陷阱。

本页目录直觉理解数学与方法代码实现局限与误解实验与练习研究脉络

地图画对了,路线才有用

设想一个教学用送货机器人。它可以走一条慢路,也可以试一条较快、却偶尔把它送回原地的通道。直接学习价值的方法会反复尝试,从回报中调整选择。基于模型的强化学习还会问:执行这个动作后,可能到哪里?每种结果有多大概率?奖励是多少?有了这些预测,就可以在真正移动之前计算路线。

这里的“模型”指环境结果的预测器,不是所有机器学习模型的统称。一个只输出动作的神经网络是策略;一个只输出动作价值的网络是价值函数。它们并不因为用了神经网络,就自动成为环境模型。环境模型可以是一张转移概率表,也可以是生成下一状态与奖励的采样程序。分布模型与采样模型

需要分开两项工作:学习模型,使它接近环境;使用模型,寻找回报较好的策略。已知精确规则时,第二项属于规划,不需要先学习规则。规则由经验估计时,规划算得再精确,也可能精确地解决了一个错误问题。本篇先用有限表格把这个差别算出来;Dyna 篇再把学习和规划放进同一个在线循环。

经验模型预测与策略真实价值有差别;存活曲线展示微小转移误差如何被反复使用。
经验模型预测与策略真实价值有差别;存活曲线展示微小转移误差如何被反复使用。

把三种误差分开

采用有限状态、有限动作的马尔可夫决策过程。状态为 ss,动作为 aa,下一状态为 s′s';奖励有界,折扣 0≤γ<10\le\gamma<1。这里关心期望折扣回报,定义:

P(s′∣s,a)=Pr⁡(St+1=s′∣St=s,At=a),r(s,a)=E[Rt+1∣St=s,At=a].P(s'\mid s,a)=\Pr(S_{t+1}=s'\mid S_t=s,A_t=a),\qquad r(s,a)=\mathbb E[R_{t+1}\mid S_t=s,A_t=a].

对这种期望目标,转移分布与条件奖励均值足以进行 Bellman 备份。若要生成完整随机回报轨迹、研究风险或奖励与下一状态的相关性,就应保存相应的联合分布;仅有奖励均值不能回答所有问题。显式分布模型能对所有下一状态求和,采样模型只生成一次可能结果,两者的计算成本和估计方差不同。模型提供什么

假设收集了 N(s,a)>0N(s,a)>0 次真实转移,其中到达 s′s' 的次数为 N(s,a,s′)N(s,a,s')。一个经验模型是:

P^(s′∣s,a)=N(s,a,s′)N(s,a),r^(s,a)=1N(s,a)∑i:(si,ai)=(s,a)ri.\widehat P(s'\mid s,a)=\frac{N(s,a,s')}{N(s,a)},\qquad \widehat r(s,a)=\frac{1}{N(s,a)}\sum_{i:(s_i,a_i)=(s,a)}r_i.

分母为零时,答案是未知,不是“这个动作的奖励和风险都是零”。程序会主动查询每一个非终止状态动作对,所以没有暗中填补未覆盖动作。这种可从指定状态动作取样的访问方式称为生成式查询;真实机器人通常只能沿自身轨迹收集数据,不能免费跳到任意状态。

给定经验模型,价值迭代使用下列算子。终止状态约定为奖励零、留在自身的吸收状态,故其价值为零:

(T^V)(s)=max⁡a[r^(s,a)+γ∑s′P^(s′∣s,a)V(s′)].(\widehat T V)(s)=\max_a\left[\widehat r(s,a)+\gamma\sum_{s'}\widehat P(s'\mid s,a)V(s')\right].

一次迭代先计算每个动作的得分,再取最大值。因为概率加权平均不会扩大最大绝对差,最大操作也不会,两个输入价值的距离经过该算子至多变成原来的 γ\gamma 倍。因此它有唯一固定点 V^∗\widehat V^*。让残差 ρ=∥T^V−V∥∞\rho=\|\widehat T V-V\|_\infty 表示当前表格离自身备份有多远,三角不等式给出:

∥V−V^∗∥∞≤ρ+γ∥V−V^∗∥∞,∥V−V^∗∥∞≤ρ1−γ.\|V-\widehat V^*\|_\infty\le\rho+\gamma\|V-\widehat V^*\|_\infty, \qquad \|V-\widehat V^*\|_\infty\le\frac{\rho}{1-\gamma}.

这控制的是对经验模型的数值求解误差。它没有说明经验模型是否正确,更没有保证真实策略最优。程序用残差除以 1−γ1-\gamma 作为价值误差界,而不是把“相邻两次看起来差不多”直接当成真实环境验证。

怎样看模型本身的误差?固定同一个策略 π\pi,用上标表示按它对动作求平均后的奖励与转移矩阵。两个 Bellman 方程相减,并把价值差留在右侧的模型转移项中:

V^π−Vπ=(r^π−rπ)+γ(P^π−Pπ)Vπ+γP^π(V^π−Vπ).\widehat V^\pi-V^\pi=(\widehat r^\pi-r^\pi) +\gamma(\widehat P^\pi-P^\pi)V^\pi +\gamma\widehat P^\pi(\widehat V^\pi-V^\pi).

设两模型奖励均值差至多 εr\varepsilon_r,每个状态动作的转移分布 L1 距离至多 εP\varepsilon_P,真实奖励绝对值至多 Rmax⁡R_{\max}。由几何级数有 ∥Vπ∥∞≤Rmax⁡/(1−γ)\|V^\pi\|_\infty\le R_{\max}/(1-\gamma)。逐项取最大范数,再把最后一项移到左侧,得到本篇独立推导的有限模型界:

∥V^π−Vπ∥∞≤εr1−γ+γεPRmax⁡(1−γ)2.\|\widehat V^\pi-V^\pi\|_\infty \le\frac{\varepsilon_r}{1-\gamma} +\frac{\gamma\varepsilon_P R_{\max}}{(1-\gamma)^2}.

这里用的是 L1 距离;若写成总变差距离,常数要相应换算。这个界比较的是同一个策略在两个模型中的价值,不应直接改名为“所学策略的最优性差距”。而且,有限验证集上的平均预测损失并不自动提供全状态动作上的这两个上界。

一个算得清的反例:每走一步得 1 分,真实系统有 0.99 的概率继续存活,模型却预测永远存活;退出后奖励为零。取折扣 0.99,当前步奖励先于是否退出的结果:

V=11−0.99×0.99=10000199≈50.2513,V^=11−0.99=100.V=\frac{1}{1-0.99\times0.99}=\frac{10000}{199}\approx50.2513, \qquad\widehat V=\frac{1}{1-0.99}=100.

转移 L1 误差只有 0.02,价值差却约为 49.7487。上面的通用界为 198,能覆盖实际差异,但相当松。这里说明了折扣接近 1 时误差可能被反复使用,不是说所有一步误差都会按这个最坏界增长。

同一个模型,预测分数和真实得分各算一次

下面是原创的三状态、两动作例子,Python 3.12 以上标准库即可运行。状态 2 为终止;状态 0 的动作 0 得 0.05 分,以 0.15 的概率到状态 1,否则留在原地;动作 1 扣 0.02 分,但到状态 1 的概率为 0.9。状态 1 的动作 0 得 0.7 分,有 0.2 的概率留下;动作 1 得 1 分并终止。每次奖励在本例中是确定的,只有下一状态随机。

折扣为 0.9。精确最优策略在两个非终止状态都选动作 1:状态 1 的价值是 1;在状态 0 有 V0=−0.02+0.9(0.1V0+0.9)V_0=-0.02+0.9(0.1V_0+0.9),所以 V0=79/91≈0.868132V_0=79/91\approx0.868132。这可以不依赖价值迭代求出,适合检查程序。

学习阶段对四个非终止状态动作对各查询 NN 次,共 4N4N 次真实查询。规划只接收经验模型;选完策略后,才用真实环境的二元线性方程计算实际价值。这种真实模型求值是教学诊断,现实系统通常还需要独立真实评估。不能把它混进训练,再声称没有使用真实规则。

"""Original finite-model teaching experiment; Python 3.12+, standard library."""
import json
import math
import random

GAMMA = 0.9
# Rows are (mean reward, next-state probabilities); state 2 is absorbing.
TRUE_MODEL = [
    [(0.05, [0.85, 0.15, 0.0]), (-0.02, [0.1, 0.9, 0.0])],
    [(0.7, [0.0, 0.2, 0.8]), (1.0, [0.0, 0.0, 1.0])],
    [(0.0, [0.0, 0.0, 1.0]), (0.0, [0.0, 0.0, 1.0])],
]


def validate(model, gamma):
    if not 0 <= gamma < 1 or not model:
        raise ValueError("Require a nonempty model and discount in [0, 1).")
    size = len(model)
    for actions in model:
        if not actions:
            raise ValueError("Each state needs an action.")
        for reward, probs in actions:
            if (not math.isfinite(reward) or len(probs) != size
                    or any(not math.isfinite(p) or p < 0 for p in probs)
                    or abs(sum(probs) - 1) > 1e-12):
                raise ValueError("Invalid reward or transition distribution.")


def backup(model, values, gamma):
    return [[reward + gamma * sum(p * v for p, v in zip(probs, values))
             for reward, probs in actions] for actions in model]


def plan(model, gamma=GAMMA, tolerance=1e-10, max_iterations=10000):
    validate(model, gamma)
    if tolerance <= 0 or not math.isfinite(tolerance) or max_iterations < 1:
        raise ValueError("Positive finite tolerance and iteration budget required.")
    values = [0.0] * len(model)
    for iteration in range(1, max_iterations + 1):
        values = [max(row) for row in backup(model, values, gamma)]
        q = backup(model, values, gamma)
        residual = max(abs(max(row) - v) for row, v in zip(q, values))
        if residual <= tolerance * (1 - gamma):
            policy = [max(range(len(row)), key=row.__getitem__) for row in q]
            return dict(values=values, policy=policy, residual=residual,
                        iterations=iteration, value_error_bound=residual / (1 - gamma))
    raise RuntimeError("Planning did not reach its residual tolerance.")


def fit_model(samples_per_pair, seed):
    """Generative queries to all four nonterminal pairs, not an online trajectory."""
    if not isinstance(samples_per_pair, int) or samples_per_pair < 1:
        raise ValueError("At least one query per pair is required.")
    rng = random.Random(seed)
    model = []
    counts = []
    for state in range(2):
        actions, state_counts = [], []
        for reward, probs in TRUE_MODEL[state]:
            hits = [0, 0, 0]
            reward_sum = 0.0
            for _ in range(samples_per_pair):
                u = rng.random()
                cumulative = 0.0
                for next_state, probability in enumerate(probs):
                    cumulative += probability
                    if u < cumulative:
                        hits[next_state] += 1
                        break
                reward_sum += reward  # deterministic rewards in this task
            actions.append((reward_sum / samples_per_pair,
                            [n / samples_per_pair for n in hits]))
            state_counts.append(hits)
        model.append(actions)
        counts.append(state_counts)
    # Termination and the zero-reward absorbing convention are supplied, not learned.
    model.append([(0.0, [0.0, 0.0, 1.0]), (0.0, [0.0, 0.0, 1.0])])
    return model, counts


def evaluate_two_state(policy, model=TRUE_MODEL, gamma=GAMMA):
    """Independent 2x2 linear solve; terminal value is zero."""
    validate(model, gamma)
    r0, p0 = model[0][policy[0]]
    r1, p1 = model[1][policy[1]]
    a, b = 1 - gamma * p0[0], -gamma * p0[1]
    c, d = -gamma * p1[0], 1 - gamma * p1[1]
    determinant = a * d - b * c
    return [(r0 * d - b * r1) / determinant,
            (a * r1 - c * r0) / determinant, 0.0]


def experiment():
    exact = plan(TRUE_MODEL)
    rows = []
    for n in (2, 10, 100):
        for seed in range(8):
            model, counts = fit_model(n, seed)
            learned = plan(model)
            actual = evaluate_two_state(learned['policy'])[0]
            l1 = max(sum(abs(x - y) for x, y in zip(p, phat))
                     for original, estimated in zip(TRUE_MODEL, model)
                     for (_, p), (_, phat) in zip(original, estimated))
            rows.append(dict(n=n, seed=seed, real_queries=4*n, counts=counts,
                             policy=learned['policy'], model_value=learned['values'][0],
                             true_policy_value=actual, regret=exact['values'][0]-actual,
                             max_transition_l1=l1, planning_iterations=learned['iterations'],
                             planning_residual=learned['residual']))
    return dict(exact=exact, rows=rows)


if __name__ == '__main__':
    print(json.dumps(experiment(), indent=2))
查看完整运行结果
{
  "exact": {
    "values": [
      0.8681318681293009,
      1.0,
      0.0
    ],
    "policy": [
      1,
      1,
      0
    ],
    "residual": 2.336242310718717e-12,
    "iterations": 12,
    "value_error_bound": 2.3362423107187176e-11
  },
  "rows": [
    {
      "n": 2,
      "seed": 0,
      "real_queries": 8,
      "counts": [
        [
          [
            2,
            0,
            0
          ],
          [
            0,
            2,
            0
          ]
        ],
        [
          [
            0,
            0,
            2
          ],
          [
            0,
            0,
            2
          ]
        ]
      ],
      "policy": [
        1,
        1,
        0
      ],
      "model_value": 0.88,
      "true_policy_value": 0.8681318681318682,
      "regret": -2.567279722143212e-12,
      "max_transition_l1": 0.39999999999999997,
      "planning_iterations": 2,
      "planning_residual": 0.0
    },
    {
      "n": 2,
      "seed": 1,
      "real_queries": 8,
      "counts": [
        [
          [
            2,
            0,
            0
          ],
          [
            0,
            2,
            0
          ]
        ],
        [
          [
            0,
            0,
            2
          ],
          [
            0,
            0,
            2
          ]
        ]
      ],
      "policy": [
        1,
        1,
        0
      ],
      "model_value": 0.88,
      "true_policy_value": 0.8681318681318682,
      "regret": -2.567279722143212e-12,
      "max_transition_l1": 0.39999999999999997,
      "planning_iterations": 2,
      "planning_residual": 0.0
    },
    {
      "n": 2,
      "seed": 2,
      "real_queries": 8,
      "counts": [
        [
          [
            0,
            2,
            0
          ],
          [
            2,
            0,
            0
          ]
        ],
        [
          [
            0,
            0,
            2
          ],
          [
            0,
            0,
            2
          ]
        ]
      ],
      "policy": [
        0,
        1,
        0
      ],
      "model_value": 0.9500000000000001,
      "true_policy_value": 0.7872340425531915,
      "regret": 0.08089782557610936,
      "max_transition_l1": 1.8,
      "planning_iterations": 2,
      "planning_residual": 0.0
    },
    {
      "n": 2,
      "seed": 3,
      "real_queries": 8,
      "counts": [
        [
          [
            2,
            0,
            0
          ],
          [
            0,
            2,
            0
          ]
        ],
        [
          [
            0,
            1,
            1
          ],
          [
            0,
            0,
            2
          ]
        ]
      ],
      "policy": [
        1,
        0,
        0
      ],
      "model_value": 1.125454545444855,
      "true_policy_value": 0.7378718842133476,
      "regret": 0.13025998391595328,
      "max_transition_l1": 0.6000000000000001,
      "planning_iterations": 32,
      "planning_residual": 5.3297366520155265e-12
    },
    {
      "n": 2,
      "seed": 4,
      "real_queries": 8,
      "counts": [
        [
          [
            2,
            0,
            0
          ],
          [
            0,
            2,
            0
          ]
        ],
        [
          [
            0,
            1,
            1
          ],
          [
            0,
            0,
            2
          ]
        ]
      ],
      "policy": [
        1,
        0,
        0
      ],
      "model_value": 1.125454545444855,
      "true_policy_value": 0.7378718842133476,
      "regret": 0.13025998391595328,
      "max_transition_l1": 0.6000000000000001,
      "planning_iterations": 32,
      "planning_residual": 5.3297366520155265e-12
    },
    {
      "n": 2,
      "seed": 5,
      "real_queries": 8,
      "counts": [
        [
          [
            2,
            0,
            0
          ],
          [
            0,
            2,
            0
          ]
        ],
        [
          [
            0,
            0,
            2
          ],
          [
            0,
            0,
            2
          ]
        ]
      ],
      "policy": [
        1,
        1,
        0
      ],
      "model_value": 0.88,
      "true_policy_value": 0.8681318681318682,
      "regret": -2.567279722143212e-12,
      "max_transition_l1": 0.39999999999999997,
      "planning_iterations": 2,
      "planning_residual": 0.0
    },
    {
      "n": 2,
      "seed": 6,
      "real_queries": 8,
      "counts": [
        [
          [
            2,
            0,
            0
          ],
          [
            0,
            2,
            0
          ]
        ],
        [
          [
            0,
            1,
            1
          ],
          [
            0,
            0,
            2
          ]
        ]
      ],
      "policy": [
        1,
        0,
        0
      ],
      "model_value": 1.125454545444855,
      "true_policy_value": 0.7378718842133476,
      "regret": 0.13025998391595328,
      "max_transition_l1": 0.6000000000000001,
      "planning_iterations": 32,
      "planning_residual": 5.3297366520155265e-12
    },
    {
      "n": 2,
      "seed": 7,
      "real_queries": 8,
      "counts": [
        [
          [
            2,
            0,
            0
          ],
          [
            1,
            1,
            0
          ]
        ],
        [
          [
            0,
            0,
            2
          ],
          [
            0,
            0,
            2
          ]
        ]
      ],
      "policy": [
        1,
        1,
        0
      ],
      "model_value": 0.7818181818051804,
      "true_policy_value": 0.8681318681318682,
      "regret": -2.567279722143212e-12,
      "max_transition_l1": 0.8,
      "planning_iterations": 32,
      "planning_residual": 7.150835479308171e-12
    },
    {
      "n": 10,
      "seed": 0,
      "real_queries": 40,
      "counts": [
        [
          [
            10,
            0,
            0
          ],
          [
            0,
            10,
            0
          ]
        ],
        [
          [
            0,
            1,
            9
          ],
          [
            0,
            0,
            10
          ]
        ]
      ],
      "policy": [
        1,
        1,
        0
      ],
      "model_value": 0.88,
      "true_policy_value": 0.8681318681318682,
      "regret": -2.567279722143212e-12,
      "max_transition_l1": 0.30000000000000004,
      "planning_iterations": 2,
      "planning_residual": 0.0
    },
    {
      "n": 10,
      "seed": 1,
      "real_queries": 40,
      "counts": [
        [
          [
            10,
            0,
            0
          ],
          [
            2,
            8,
            0
          ]
        ],
        [
          [
            0,
            2,
            8
          ],
          [
            0,
            0,
            10
          ]
        ]
      ],
      "policy": [
        1,
        1,
        0
      ],
      "model_value": 0.853658536579944,
      "true_policy_value": 0.8681318681318682,
      "regret": -2.567279722143212e-12,
      "max_transition_l1": 0.30000000000000004,
      "planning_iterations": 16,
      "planning_residual": 4.446110146716364e-12
    },
    {
      "n": 10,
      "seed": 2,
      "real_queries": 40,
      "counts": [
        [
          [
            8,
            2,
            0
          ],
          [
            0,
            10,
            0
          ]
        ],
        [
          [
            0,
            3,
            7
          ],
          [
            0,
            0,
            10
          ]
        ]
      ],
      "policy": [
        1,
        1,
        0
      ],
      "model_value": 0.88,
      "true_policy_value": 0.8681318681318682,
      "regret": -2.567279722143212e-12,
      "max_transition_l1": 0.20000000000000007,
      "planning_iterations": 2,
      "planning_residual": 0.0
    },
    {
      "n": 10,
      "seed": 3,
      "real_queries": 40,
      "counts": [
        [
          [
            10,
            0,
            0
          ],
          [
            0,
            10,
            0
          ]
        ],
        [
          [
            0,
            2,
            8
          ],
          [
            0,
            0,
            10
          ]
        ]
      ],
      "policy": [
        1,
        1,
        0
      ],
      "model_value": 0.88,
      "true_policy_value": 0.8681318681318682,
      "regret": -2.567279722143212e-12,
      "max_transition_l1": 0.30000000000000004,
      "planning_iterations": 2,
      "planning_residual": 0.0
    },
    {
      "n": 10,
      "seed": 4,
      "real_queries": 40,
      "counts": [
        [
          [
            9,
            1,
            0
          ],
          [
            0,
            10,
            0
          ]
        ],
        [
          [
            0,
            2,
            8
          ],
          [
            0,
            0,
            10
          ]
        ]
      ],
      "policy": [
        1,
        1,
        0
      ],
      "model_value": 0.88,
      "true_policy_value": 0.8681318681318682,
      "regret": -2.567279722143212e-12,
      "max_transition_l1": 0.19999999999999998,
      "planning_iterations": 2,
      "planning_residual": 0.0
    },
    {
      "n": 10,
      "seed": 5,
      "real_queries": 40,
      "counts": [
        [
          [
            7,
            3,
            0
          ],
          [
            1,
            9,
            0
          ]
        ],
        [
          [
            0,
            4,
            6
          ],
          [
            0,
            0,
            10
          ]
        ]
      ],
      "policy": [
        1,
        0,
        0
      ],
      "model_value": 0.9515796703233558,
      "true_policy_value": 0.7378718842133476,
      "regret": 0.13025998391595328,
      "max_transition_l1": 0.4000000000000001,
      "planning_iterations": 25,
      "planning_residual": 4.041544876542957e-12
    },
    {
      "n": 10,
      "seed": 6,
      "real_queries": 40,
      "counts": [
        [
          [
            10,
            0,
            0
          ],
          [
            0,
            10,
            0
          ]
        ],
        [
          [
            0,
            3,
            7
          ],
          [
            0,
            0,
            10
          ]
        ]
      ],
      "policy": [
        1,
        1,
        0
      ],
      "model_value": 0.88,
      "true_policy_value": 0.8681318681318682,
      "regret": -2.567279722143212e-12,
      "max_transition_l1": 0.30000000000000004,
      "planning_iterations": 2,
      "planning_residual": 0.0
    },
    {
      "n": 10,
      "seed": 7,
      "real_queries": 40,
      "counts": [
        [
          [
            10,
            0,
            0
          ],
          [
            2,
            8,
            0
          ]
        ],
        [
          [
            0,
            4,
            6
          ],
          [
            0,
            0,
            10
          ]
        ]
      ],
      "policy": [
        1,
        0,
        0
      ],
      "model_value": 0.9359756097476781,
      "true_policy_value": 0.7378718842133476,
      "regret": 0.13025998391595328,
      "max_transition_l1": 0.4000000000000001,
      "planning_iterations": 25,
      "planning_residual": 5.388467450018197e-12
    },
    {
      "n": 100,
      "seed": 0,
      "real_queries": 400,
      "counts": [
        [
          [
            79,
            21,
            0
          ],
          [
            11,
            89,
            0
          ]
        ],
        [
          [
            0,
            20,
            80
          ],
          [
            0,
            0,
            100
          ]
        ]
      ],
      "policy": [
        1,
        1,
        0
      ],
      "model_value": 0.866814650381144,
      "true_policy_value": 0.8681318681318682,
      "regret": -2.567279722143212e-12,
      "max_transition_l1": 0.11999999999999994,
      "planning_iterations": 12,
      "planning_residual": 6.589173651150304e-12
    },
    {
      "n": 100,
      "seed": 1,
      "real_queries": 400,
      "counts": [
        [
          [
            87,
            13,
            0
          ],
          [
            12,
            88,
            0
          ]
        ],
        [
          [
            0,
            20,
            80
          ],
          [
            0,
            0,
            100
          ]
        ]
      ],
      "policy": [
        1,
        1,
        0
      ],
      "model_value": 0.8654708520158838,
      "true_policy_value": 0.8681318681318682,
      "regret": -2.567279722143212e-12,
      "max_transition_l1": 0.04000000000000001,
      "planning_iterations": 13,
      "planning_residual": 1.8316459460265833e-12
    },
    {
      "n": 100,
      "seed": 2,
      "real_queries": 400,
      "counts": [
        [
          [
            78,
            22,
            0
          ],
          [
            16,
            84,
            0
          ]
        ],
        [
          [
            0,
            20,
            80
          ],
          [
            0,
            0,
            100
          ]
        ]
      ],
      "policy": [
        1,
        1,
        0
      ],
      "model_value": 0.8598130841028792,
      "true_policy_value": 0.8681318681318682,
      "regret": -2.567279722143212e-12,
      "max_transition_l1": 0.13999999999999996,
      "planning_iterations": 14,
      "planning_residual": 7.935430090810769e-12
    },
    {
      "n": 100,
      "seed": 3,
      "real_queries": 400,
      "counts": [
        [
          [
            76,
            24,
            0
          ],
          [
            8,
            92,
            0
          ]
        ],
        [
          [
            0,
            19,
            81
          ],
          [
            0,
            0,
            100
          ]
        ]
      ],
      "policy": [
        1,
        1,
        0
      ],
      "model_value": 0.8706896551693413,
      "true_policy_value": 0.8681318681318682,
      "regret": -2.567279722143212e-12,
      "max_transition_l1": 0.17999999999999997,
      "planning_iterations": 11,
      "planning_residual": 2.8513857941447895e-12
    },
    {
      "n": 100,
      "seed": 4,
      "real_queries": 400,
      "counts": [
        [
          [
            84,
            16,
            0
          ],
          [
            15,
            85,
            0
          ]
        ],
        [
          [
            0,
            23,
            77
          ],
          [
            0,
            0,
            100
          ]
        ]
      ],
      "policy": [
        1,
        1,
        0
      ],
      "model_value": 0.8612716762965646,
      "true_policy_value": 0.8681318681318682,
      "regret": -2.567279722143212e-12,
      "max_transition_l1": 0.10000000000000003,
      "planning_iterations": 14,
      "planning_residual": 3.471556375700402e-12
    },
    {
      "n": 100,
      "seed": 5,
      "real_queries": 400,
      "counts": [
        [
          [
            79,
            21,
            0
          ],
          [
            5,
            95,
            0
          ]
        ],
        [
          [
            0,
            18,
            82
          ],
          [
            0,
            0,
            100
          ]
        ]
      ],
      "policy": [
        1,
        1,
        0
      ],
      "model_value": 0.874345549737596,
      "true_policy_value": 0.8681318681318682,
      "regret": -2.567279722143212e-12,
      "max_transition_l1": 0.11999999999999994,
      "planning_iterations": 10,
      "planning_residual": 5.95745675013859e-13
    },
    {
      "n": 100,
      "seed": 6,
      "real_queries": 400,
      "counts": [
        [
          [
            88,
            12,
            0
          ],
          [
            12,
            88,
            0
          ]
        ],
        [
          [
            0,
            24,
            76
          ],
          [
            0,
            0,
            100
          ]
        ]
      ],
      "policy": [
        1,
        1,
        0
      ],
      "model_value": 0.8654708520158838,
      "true_policy_value": 0.8681318681318682,
      "regret": -2.567279722143212e-12,
      "max_transition_l1": 0.08000000000000002,
      "planning_iterations": 13,
      "planning_residual": 1.8316459460265833e-12
    },
    {
      "n": 100,
      "seed": 7,
      "real_queries": 400,
      "counts": [
        [
          [
            87,
            13,
            0
          ],
          [
            12,
            88,
            0
          ]
        ],
        [
          [
            0,
            18,
            82
          ],
          [
            0,
            0,
            100
          ]
        ]
      ],
      "policy": [
        1,
        1,
        0
      ],
      "model_value": 0.8654708520158838,
      "true_policy_value": 0.8681318681318682,
      "regret": -2.567279722143212e-12,
      "max_transition_l1": 0.04000000000000001,
      "planning_iterations": 13,
      "planning_residual": 1.8316459460265833e-12
    }
  ]
}

保留了种子 0—7 的全部结果。N=2N=2 时四个种子选错策略;N=10N=10 时两个种子选错;N=100N=100 时八个种子都选中了本例最优策略。模型预测的起点价值与该策略真实价值之间,平均绝对差分别约为 0.180929、0.060703、0.004156。完整输出中的极小负 regret 来自数值停止误差,量级约 10−1210^{-12},不表示策略超过真实最优值。

这些是 8、40、400 次生成式查询下的结果,不是八个、四十个或四百个完整回合。样本更多没有带来每个种子的单调改善;更不能由八个种子的成功推断一个普遍样本量保证。表格规划次数也单独记录,因为降低真实采样量并不等于降低计算耗时。

一般密集表格有 SS 个状态、每状态 AA 个动作时,保存转移分布需要 O(S2A)O(S^2A) 空间,每次全量期望备份需要 O(S2A)O(S^2A) 运算;做 KK 次迭代为 O(KS2A)O(KS^2A)。稀疏转移可以只访问非零项。本文采样器顺序扫描至多 SS 个结果,若每对采 NN 次,其采样计算为 O(NS2A)O(NS^2A),真实查询数仍是 NSANSA;这两个单位不能混用。

模型会把自己的盲区当机会

再看一个两动作反例。真实奖励为 0.5 和 0,但数据只包含动作 0。一个模型恰好正确预测动作 0 的 0.5,却给未见过的动作 1 赋了乐观预测 1;观测数据上的均方误差为零,规划仍会选实际奖励为零的动作。乐观值是这里明确设置的假设,不能当成数据支持的估计。策略优化会主动寻找模型认为最好的位置,恰好可能离数据很远。

MBPO 讨论了模型误差与策略分布变化,并用从真实回放状态出发的短模型分支减少长预测链的影响。它的任务时域可以很长,模型分支却很短:从已有真实状态重新开始,并不等于强迫任务提前结束。论文方法使用概率模型集成,结合 SAC 从生成经验中学习;本篇表格程序没有实现 MBPO。模型使用与短分支

模型集成成员意见接近,也不等于已证明预测可靠;它们可能共享相同的偏差。论文中条件性的回报界依赖特定误差量与假设,不能把一个验证损失直接代入,再承诺神经模型训练必然提高回报。理论与实际算法的条件

还有模型范围的问题。观察不是马尔可夫状态时,下一步预测需要历史或隐状态;真实环境变化时,旧数据可能描述了已经不存在的通道。选择方法前应比较:只用真实数据的价值学习基线,是否已经足够?模型是否覆盖策略将访问的区域?模拟节省的交互是否值得新增的拟合和规划成本?

先预测,再查看答案

  1. 理解: 经验模型中的残差为 10−510^{-5},折扣为 0.9。能给出什么误差界?它是否保证真实环境也一样准确?

    答案: 对该经验模型的最优价值误差至多 10−410^{-4}。这只控制数值求解误差;模型本身可能错,所以不保证真实环境价值或策略最优性。

  2. 手算: 在生存例子中把折扣改为 0.5,其他条件不变,真实价值与模型价值分别是多少?

    答案: 真实值为 1/(1−0.5×0.99)=200/101≈1.9801981/(1-0.5\times0.99)=200/101\approx1.980198,模型值为 2,差 2/101≈0.0198022/101\approx0.019802。未来权重降低后,同一转移偏差对本例价值的影响小得多。

  3. 反例: 只见过动作 0 的二动作例子,观测误差为零,为什么还会选错?增加模拟次数能补上缺失信息吗?

    答案: 误差只在已见动作上计算,未见动作的乐观预测没有被验证。更多模拟会继续复用该假设;需要真实覆盖、合理约束或针对未知区域的评估,而不是把重复生成当成新观测。

  4. 迁移: 表格任务每对查询 10 次后得到了一个策略。你只有在线机器人,不能指定任意状态起步。可以直接声称只需 40 次真实动作吗?

    答案: 不可以。40 是生成式查询接口下四个状态动作对的总样本数。在线访问还要付出到达状态、探索与可能复位的成本,并处理覆盖不均;应按真实轨迹预算重新设计和评估。

从规划模型读到学得模型

Sutton 与 Barto 的第二版工作稿区分分布模型、采样模型及从模型经验中学习的规划方法。这里实际核读的是 ©2014/2015 工作稿的物理 PDF 209—220 页,非 2018 完成版;因此引用范围和版本一起保留。教材工作稿

Janner 等人的 MBPO 论文(NeurIPS 2019)把问题进一步推进到“模型究竟该用多长”。本篇核读会议 PDF 第 2—6 页,另读第一页身份与摘要部分,实看第 4、6 页;没有核完附录证明或复现控制基准。上面的有限表格、误差推导及反例均为原创教学计算,不冒充论文实验。MBPO 原文

接着读 Dyna 看真实转移如何同时供模型与价值学习使用;世界模型 与 MuZero 则是继续辨别预测目标、表示和规划接口的方向。

先判断,再看解释

经验模型中的Bellman残差接近零,能说明什么?

选择一个答案

学习目标

  • 从转移计数与奖励均值建立有限模型,用Bellman残差判断规划是否算完。
  • 区分数值误差、模型误差和策略实际回报,并解释短模型分支的动机。

先修与延伸

查看知识关联图 →

参考文献

  1. Reinforcement Learning: An Introduction — second-edition working draft, chapter 8
    Richard S. Sutton; Andrew G. Barto · 2015
  2. When to Trust Your Model: Model-Based Policy Optimization — scoped methods
    Michael Janner; Justin Fu; Marvin Zhang; Sergey Levine · 2019

版本1 · 编写状态: 已完成记录中的技术复核

查看技术检查记录(AI)

Codex · 2026-09-23 · Single AI author. Scoped reading: Copyright2014/2015 working draft, not the final2018 edition. Read physical PDF209–220, viewed215: models, Dyna, changing environments and Dyna-Q+ footnote. Later prioritized-sweeping algorithm not audited; PDF hash and scope retained in research records. NeurIPS32; read first1800 characters of PDF1 and all text of2–6, viewed4/6. Model error, policy shift, short branches from real states and probabilistic ensembles; no complete appendix/benchmark audit or transcription of ambiguous PDF5 formulas. 7 paired equations and four explained exercises. Finite empirical counts, Bellman residual bound, fixed-policy L1 perturbation bound and uncovered-action counterexample checked. Independent 2x2 solves evaluate all four policies; all24 generative-query settings retained. Exact real model is evaluation-only after planning. No MBPO benchmark reproduction or uniform finite-sample guarantee.

  • 核经验计数、Bellman残差、固定策略L1误差界及未覆盖动作反例;独立二元线性解核四策略,保留24生成式查询设置。真实模型只作规划后的教学评价,不冒称MBPO复现或普遍样本量保证。
  • 每题四张计算SVG,9数值/几何与12个当前响应式组合通过;六张当前选定图均直接实看,英文标题修订已解决保留的初次裁切失败,资产与稿件SHA绑定,完整页面验收另记。

核对的来源位置:

相关主题

没有匹配结果,试试英文名或缩写。

已显示 0 条结果

将整个查询放入双引号,可按短语匹配。