[LAB 02 · 参数迁移:冻结、局部与全量微调](/course/notebooks/4bd450d54870432db91c80a4df563b8e.html) # 2. “冻住”到底冻住哪些参数? · 实现代码 [独立阅读](/course/notebook-blocks/79753dc070ca46108d4cc8ab56dbe083.html) 按问题继续 同源参数的冻结/局部/全量/scratch与BN模式辨析 展开完整实现代码 ``` MODES=['head','late','all','scratch'] def make_target_model(mode): assert mode in MODES torch.manual_seed(SEED) model=make_cnn() if mode!='scratch':model.load_state_dict(source_state) torch.manual_seed(SEED+1) model.logits=nn.Linear(32,2) for name,param in model.named_parameters(): param.requires_grad_(mode in ('all','scratch') or name.startswith('logits.') or (mode=='late' and name.startswith(('conv2.','hidden.')))) return model scope_table=[] for mode in MODES: model=make_target_model(mode) scope_table.append({'mode':mode,'trainable':sum(p.numel() for p in model.parameters() if p.requires_grad),'total':sum(p.numel() for p in model.parameters())}) print(json.dumps(scope_table,indent=2)) ```