model.named_parameters()
作者:互联网
说明:返回每一层的参数的名称和参数内容(权重和偏置)
作用:一般来说,类中的成员都是私有的,可以通过这种方式获得模型中的参数
例如:
import torch import torch.nn as nn class TestModel(nn.Module): def __init__(self): super(TestModel, self).__init__() self.layer1 = nn.Sequential( nn.Linear(in_features=3,out_features=2) ) if __name__ == '__main__': T = TestModel() for name,parameters in T.named_parameters(): print('name:',name) print('parameters:',parameters)
输出:
name: layer1.0.weight parame: Parameter containing: tensor([[-0.1038, 0.3773, 0.1975], [-0.3853, 0.3771, 0.4450]], requires_grad=True) name: layer1.0.bias parame: Parameter containing: tensor([ 0.2626, -0.4278], requires_grad=True)
标签:__,named,parameters,nn,self,TestModel,model,name 来源: https://www.cnblogs.com/jiu-fang/p/16310642.html