PyTorch:state_dict 和 parameters() 有什么区别? [英] PyTorch: What's the difference between state_dict and parameters()?

查看:25
本文介绍了PyTorch:state_dict 和 parameters() 有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了在pytorch中访问模型的参数,我看到了两种方法:

In order to access a model's parameters in pytorch, I saw two methods:

使用state_dict 并使用 parameters()

我想知道有什么区别,或者一个是好的做法,另一个是不好的做法.

I wonder what's the difference, or if one is good practice and the other is bad practice.

谢谢

推荐答案

parameters() 只给出模块参数,即权重和偏差.

The parameters() only gives the module parameters i.e. weights and biases.

返回对模块参数的迭代器.

Returns an iterator over module parameters.

您可以查看参数列表如下:

You can check the list of the parameters as follows:

for name, param in model.named_parameters():
    if param.requires_grad:
        print(name)

另一方面,state_dict 返回一个包含模块整个状态的字典.检查其源代码 不仅包含对 parameters 的调用,还包含对 buffers 等的调用.

On the other hand, state_dict returns a dictionary containing a whole state of the module. Check its source code that contains not just the call to parameters but also buffers, etc.

包括参数和持久缓冲区(例如运行平均值).键是对应的参数和缓冲区名称.

Both parameters and persistent buffers (e.g. running averages) are included. Keys are the corresponding parameter and buffer names.

使用以下方法检查 state_dict 包含的所有键:

Check all keys that state_dict contains using:

model.state_dict().keys()

例如,在 state_dict 中,您会发现 bn1.running_meanrunning_var 之类的条目,它们在 中不存在.parameters().

For example, in state_dict, you'll find entries like bn1.running_mean and running_var, which are not present in .parameters().

如果你只想访问参数,你可以简单地使用.parameters(),而为了像迁移学习那样保存和加载模型,你需要保存state_dict 不仅仅是参数.

If you only want to access parameters, you can simply use .parameters(), while for purposes like saving and loading model as in transfer learning, you'll need to save state_dict not just parameters.

这篇关于PyTorch:state_dict 和 parameters() 有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆