最佳答案
parameters()
仅给出模块参数,即权重和偏差。
Returns an iterator over module parameters.
您可以查看参数列表如下:
for name, param in model.named_parameters():
if param.requires_grad:
print(name)
另一方面,state_dict
返回包含模块的整个状态的字典。检查其 source code
它不仅包含对 parameters
的调用,还包含 buffers
等
Both parameters and persistent buffers (e.g. running averages) are included. Keys are the corresponding parameter and buffer names.
使用以下方法检查 state_dict
包含的所有键:
model.state_dict().keys()
例如,在 state_dict
中,您会发现 bn1.running_mean
和 running_var
等条目,它们在 中不存在.parameters()
.
如果你只想访问参数,你可以简单地使用.parameters()
,而对于像在迁移学习中保存和加载模型这样的目的,你需要保存state_dict
不仅仅是参数。
关于python - PyTorch:state_dict 和 parameters() 有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54746829/