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

查看:189
本文介绍了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 返回包含模块整个状态的字典.检查其包含的 source code 不仅是对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天全站免登陆