预测编码
Predictive coding
从预测误差推断两层隐状态,再用线性系统核对答案。
predictive-coding从问题出发
一句话里有个词被噪声盖住了。上下文会提示它可能是什么,实际听到的声音又可能迫使你修改理解。预测编码用层级模型研究这种交互:高层状态预测低层状态,预测与实际之间的误差再帮助修正状态。它是一类模型,并非已经证实的大脑通用算法。
Rao 与 Ballard 在 1999 年的视觉皮层模型中,用向下预测与向上传递的误差信号解释部分感受野现象;其中还区分了对当前输入的快速推断与模型参数的较慢适应。Predictive coding in the visual cortex: a functional interpretation of some extra-classical receptive-field effects
下面求解一个两层线性高斯模型。每项误差、状态更新与最终结果都能用一个 4×4 线性系统核对。计算可以严密,关于神经科学的论断则保留其实际适用范围。
定义与推导
观测为 x,低层隐状态为 r,高层为 s,固定预测矩阵为 A、B。假设 是均值 Ar、协方差单位阵的高斯分布; 均值为 Bs、精度为 ;s 均值为零、精度为 。正精度表示方差的倒数。去掉与状态无关的常数后,负对数后验为:
低层状态同时承受两种要求:解释观测,以及符合高层预测。分别对残差平方求导:
令 ,该二次函数对应一个对称精度矩阵。只要 ,它就是正定的,即使 A 不满秩,后验众数也唯一:
上述步长范围使固定步长梯度下降的每个特征方向都收缩。它适用于这个固定正定二次目标,不能直接推广到所有非线性预测编码网络。固定推断出的状态时,A 的参数梯度是 ,B 的是 。更新这些矩阵叫学习,为当前 x 更新 r、s 叫推断。高斯层级与误差更新的解释可对照 Bogacz 教程;这里的数字与推导来自本章构造模型。A tutorial on the free-energy framework for modelling perception and learning
动手计算
使用 CPython 3.12.4、NumPy 1.26.4。四个状态从零开始,alpha 为 2,先验精度为 0.7,以 0.05 步长更新 400 次。能量从 0.625 降到 0.280793808,与直接解线性系统所得状态的最大偏差约为 5.54e−8。算出的安全步长上界为 0.350946671。程序打印最终状态处的参数梯度,但没有进行参数学习循环。
import numpy as np
A=np.array([[1.,.4],[-.2,1.2]]);B=np.array([[.8,.1],[.3,1.]]);X=np.array([1.,-.5]);ALPHA=2.;PRIOR=.7
def energy_gradient(state):
r,s=np.asarray(state[:2]),np.asarray(state[2:]);e0=X-A@r;e1=r-B@s;energy=.5*(e0@e0+ALPHA*(e1@e1)+PRIOR*(s@s));gradient=np.r_[-A.T@e0+ALPHA*e1,-ALPHA*B.T@e1+PRIOR*s]
return float(energy),gradient,e0,e1
def precision():return np.block([[A.T@A+ALPHA*np.eye(2),-ALPHA*B],[-ALPHA*B.T,ALPHA*B.T@B+PRIOR*np.eye(2)]])
def infer(steps=400,rate=.05):
z=np.zeros(4);trace=[]
for _ in range(steps):energy,g,_,_=energy_gradient(z);trace.append(energy);z-=rate*g
return z,trace
if __name__=='__main__':
z,trace=infer();exact=np.linalg.solve(precision(),np.r_[A.T@X,[0.,0.]]);loss,g,e0,e1=energy_gradient(z);print('inferred lower / upper state',z[:2].round(9).tolist(),z[2:].round(9).tolist());print('energy before/after',trace[0],round(loss,9),'posterior-mode discrepancy',float(np.max(np.abs(z-exact))))
print('bottom / top prediction errors',e0.round(9).tolist(),e1.round(9).tolist(),'safe fixed-step upper bound',round(float(2/np.linalg.eigvalsh(precision()).max()),9))
print('weight gradients at fixed inferred state',(-np.outer(e0,z[:2])).round(9).tolist(),(-ALPHA*np.outer(e1,z[2:])).round(9).tolist())
inferred lower / upper state [0.609771879, -0.088316363] [0.481567186, -0.154657863]
energy before/after 0.625 0.280793808 posterior-mode discrepancy 5.542248288081808e-08
bottom / top prediction errors [0.425554666, -0.272065989] [0.239983917, -0.078128656] safe fixed-step upper bound 0.350946671
weight gradients at fixed inferred state [[-0.259491268, 0.03758344], [0.165898189, -0.024027879]] [[-0.231136759, 0.074230799], [0.075248394, -0.024166422]]
什么时候会失效
最优点处,两层残差都未必为零。观测、层间预测与先验相互竞争,逐像素解释得完全一致反而可能增加总能量。增大某项精度会提高相应证据的权重,但不会让证据自动变正确。
对 n 维隐状态,通用稠密分解的直接求解成本为 ,每次稠密迭代更新约为 ;稀疏结构会改变成本。能进行局部运算时,迭代有用,但“使用局部误差”本身不能证明生物合理性:本例仍然使用预测矩阵的转置,并同时更新状态。
视觉皮层论文给出的是特定现象的模型解释,不是所有皮层回路都运行这些方程的证明。预测编码也不同于对比预测编码,后者用对比预测目标学习表示。本例没有计算变分自由能界、熵项或完整后验协方差;它求的是众数,在这里的高斯情形中也等于均值。Predictive coding in the visual cortex: a functional interpretation of some extra-classical receptive-field effects;A tutorial on the free-energy framework for modelling perception and learning
练习与答案
- 为什么精度矩阵正定?
答案1: 对应二次型是 Ar 的平方、alpha 倍 r−Bs 的平方和 lambda 倍 s 的平方之和。为零要求先有 s=0,再有 r=0。
- 众数处底层误差一定为零吗?
答案2: 不一定。进一步降低它可能加重层间不匹配或先验罚项。
- alpha 增大改变什么?
答案3: 给定 s 时 r 的条件方差变小,偏离 Bs 的惩罚更强。
- 打印了权重梯度,能否说模型已经学会 A、B?
答案4: 不能。程序固定 A、B;训练流程必须实际更新并评估它们。
来源与延伸
Rao 与 Ballard 的层级描述提供历史假说,Bogacz 第 4.2 节给出多层高斯模型的讲解。该教程 2015 年在线发表、编入 2017 年期刊卷,两种日期不应混为一谈。扩展程序时,可用已知 A、B 生成观测,分开训练与评估信号,并分别报告状态推断误差与参数恢复程度。Predictive coding in the visual cortex: a functional interpretation of some extra-classical receptive-field effects;A tutorial on the free-energy framework for modelling perception and learning
先判断,再看解释
本例中哪项操作属于推断?
学习目标
- 推导状态更新并独立核验高斯后验众数。
- 区分推断、参数学习与神经科学假说。
先修与延伸
查看知识关联图 →参考文献
- Predictive coding in the visual cortex: a functional interpretation of some extra-classical receptive-field effects
Rajesh P. N. Rao; Dana H. Ballard · 1999 - A tutorial on the free-energy framework for modelling perception and learning
Rafal Bogacz · 2017
版本1 · 编写状态: 已完成记录中的技术复核
查看技术检查记录(AI)
Codex · 2026-09-07 · Codex single-AI-author technical check: primary-source scope, explicit assumptions, original calculations, independent mathematical references and bilingual tests; not independent human or second-agent review.
- 单AI作者技术检查:限定范围的一手来源阅读、明确假设、原创计算、可运行示例及对应双语公式、程序和习题。不冒充人工或第二代理独立审校。
- 每篇四项CPython 3.12.4检查通过,按主题核验有限差分梯度、样本协方差、教师与中心更新顺序、隐藏输入隔离、独立最小二乘、半正定投影、间隔可行性、共享分支导数和变换后的因素作用,并检查双语结构。八个程序均使用NumPy 1.26.4,属于教学计算,不冒充图像基准。
- 完整正文、公式、代码和习题迁入;测试只读取正式MDX,删除临时目录回退及草稿跳过,状态断言更新为checked。没有修改算法或独立参照,最终记录绑定两语SHA。
核对的来源位置:
- Predictive coding in the visual cortex: a functional interpretation of some extra-classical receptive-field effects · Physical pages 1–2, hierarchy, upward errors/downward predictions and fast inference versus slow learning; read author PDF at https://www.ni.cmu.edu/~tai/microns_papers/rao_ballard.pdf.
- A tutorial on the free-energy framework for modelling perception and learning · Journal metadata (2017 volume, online 2015); physical page 9 Section 4.2 Gaussian hierarchy and preceding Eq. 50 learning rule; read PDF at https://www.tnu.ethz.ch/fileadmin/user_upload/teaching/cpcourse/2020/Literature/Bogacz_2017.pdf.
相关主题