net网络查看其参数state_dict,data,named_parameters

69 篇文章 8 订阅

代码如下:

net = nn.Linear(2, 2)
print(net.state_dict())

输出结果:

OrderedDict([
('weight', tensor([[ 0.6155, -0.4649],
                   [-0.1848, -0.0663]])), 
('bias', tensor([-0.0265, -0.4134]))])

从输出结果可以看出,在适用nn.Linear的时候会自动给weight和bias赋初值,点进Linear后可以发现

Attributes:
weight: the learnable weights of the module of shape :math: ( out_features , in_features ) (\text{out\_features}, \text{in\_features}) (out_features,in_features). The values are initialized from :
m a t h : U ( − k , k ) , w h e r e m a t h : k = 1 in_features math: \mathcal{U}(-\sqrt{k}, \sqrt{k}), where math:k = \frac{1}{\text{in\_features}} math:U(k ,k ),wheremath:k=in_features1
bias: the learnable bias of the module of shape :math: ( out_features ) (\text{out\_features}) (out_features).
If :attr:bias is True, the values are initialized from
m a t h : U ( − k , k ) w h e r e m a t h : k = 1 in_features math:\mathcal{U}(-\sqrt{k}, \sqrt{k}) where math:k = \frac{1}{\text{in\_features}} math:U(k ,k )wheremath:k=in_features1
即nn.Linear会自动给初始化

自定义初始化方法如下:


net = nn.Sequential(nn.Flatten(), nn.Linear(784, 10))
#nn.Flatten将多维张量压缩为二维(一行)进而进行线性操作、
 
def init_weights(m):
    if type(m) == nn.Linear:
        nn.init.normal_(m.weight, std=0.01)
 
net.apply(init_weights);

其他参数获取方法:

net = nn.Linear(2, 2)
print('net.state_dict()\n', net.state_dict())
print('net.bias\n', net.bias)
print('net.bias.data\n', net.bias.data)
print('net.bias.grad\n', net.bias.grad)  # 因为没有backward所以没有赋梯度值
print('net.named_parameters()\n', net.named_parameters())
print([(name, param) for name, param in net.named_parameters()])

输出结果:

net.state_dict()
 OrderedDict([('weight', tensor([[-0.6907, -0.4128],
        [-0.4212,  0.0620]])), ('bias', tensor([-0.2019, -0.3731]))])
net.bias
 Parameter containing:
tensor([-0.2019, -0.3731], requires_grad=True)
net.bias.data
 tensor([-0.2019, -0.3731])
net.bias.grad
 None
net.named_parameters()
 <generator object Module.named_parameters at 0x151b22ba0>
[('weight', Parameter containing:
tensor([[-0.6907, -0.4128],
        [-0.4212,  0.0620]], requires_grad=True)), ('bias', Parameter containing:
tensor([-0.2019, -0.3731], requires_grad=True))]

从李沐老师的手动实现一个网络层的定义可以看出这里边的权重,偏置也是经过初始化的

在这里插入图片描述

这个错误是因为你尝试在一个generator对象上调用named_parameters()方法,而generator对象没有该方法。\[1\]named_parameters()方法是用于获取模型的参数的生成器(generator)对象,而不是直接调用。如果你想要获取模型的参数,你可以使用for循环或者.next()方法来逐个读取参数值。另外,你还可以使用state_dict()方法来获取模型的参数字典。\[1\]如果你想要加载保存的网络模型,你应该使用model.load_state_dict()方法来加载参数字典,而不是直接使用torch.load()方法。\[2\]这样可以确保正确地加载模型的参数。此外,torch.save()方法在模型版本发生变化时可能无法重新加载存储好的模型,因此建议使用save_pretrained()和from_pretrained()方法来保存和加载模型。\[3\] #### 引用[.reference_title] - *1* *3* [Pytorch模型存储心得小记](https://blog.csdn.net/GJ_0418/article/details/122295368)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [pytorch载入模型时出现错误AttributeError Generator object has no attribute copy](https://blog.csdn.net/LLLLUFFY/article/details/106561536)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值