[LAB 10 · 从 RGB 图像训练小型检测器](/course/notebooks/a14237bfa9dd413d959ab4e458e11f42.html) # 选读热身:四维分类的参数更新 · 实现代码 [独立阅读](/course/notebook-blocks/bec8290eec6f4d2099698e2e9a5971f7.html) 主线阅读与优先实验 RGB图像、单特征层锚框检测器、分类/位置损失、NMS、独立检测评价 展开完整实现代码 ``` import numpy as np rng=np.random.default_rng(7) X=rng.normal(size=(64,4)); true=np.array([.8,-.4,.3,.2]); y=(X@true>0).astype(float);w=np.zeros(4) for step in range(200): p=1/(1+np.exp(-X@w));w-=.2*(X.T@(p-y))/len(y) print('four-dimensional logistic warm-up, training accuracy:',float(((X@w>0)==y).mean())) print('learned weights:',w.round(3)) # A fresh held-out matrix answers a different question from training accuracy. held=np.random.default_rng(107).normal(size=(256,4));held_y=held@true>0 print('same-rule held-out accuracy:',float(((held@w>0)==held_y).mean())) shuffled=np.random.default_rng(17).permutation(y);ws=np.zeros(4) for step in range(200): ps=1/(1+np.exp(-X@ws));ws-=.2*(X.T@(ps-shuffled))/len(shuffled) print('only training labels shuffled, held-out true-rule accuracy:',float(((held@ws>0)==held_y).mean())) ```