Pytorch奇技淫巧

如何打印pytorch模型的参数名与权重


import torch
import torch.nn as nn
net = nn.Linear(5,5)

for name,parameters in net.named_parameters():
    print(name,':',parameters.size())
    print(getattr(net,name))

#################### print ######################
weight : torch.Size([5, 5])
Parameter containing:
tensor([[-0.0215,  0.0533,  0.4388,  0.2390, -0.0038],
        [-0.2239,  0.3163,  0.2874,  0.0172,  0.3185],
        [-0.1604, -0.2267,  0.0481, -0.1901,  0.1286],
        [ 0.2276, -0.3078,  0.2144, -0.0147,  0.0572],
        [-0.1794, -0.1573,  0.3466, -0.0096, -0.3968]], requires_grad=True)
bias : torch.Size([5])
Parameter containing:
tensor([-0.1338, -0.3623, -0.2986,  0.2171,  0.3925], requires_grad=True)

你可能感兴趣的:(pytorch)