@TOC
一、模型构建
首先随机构建一个网络模型,随后的state_dict()以及named_parameters都是在模型之后运行的
1 | import torch |
二、state_dict()
model.state_dict()
返回一个字典,里面包含了整个模型参数,包括buffer
Returns a dictionary containing a whole state of the module.
Both parameters and persistent buffers (e.g. running averages) are included. Keys are corresponding parameter and buffer names.
1 | print(model.state_dict().keys()) |
三、named_parameters()
named_parameters(prefix='', recurse=True)
这个方法和上面的state_dict()相同,都是含有模型的参数,不过这个方法返回的是一个迭代器
- prefix:在参数名字前面加上前缀
- recurse:如果为True,产生的参数包括当前模型以及子模型,如果为False,只包含当前模型
返回一个迭代器包括所有的模型参数,包括参数名以及参数值
(string, Parameter) – Tuple containing the name and parameter
Returns an iterator over module parameters, yielding both the name of the parameter as well as the parameter itself.
注意:这里还有一个parameters()方法,二者的唯一区别是paramters()方法只包含参数的迭代
1 | print(model.parameters()) |