pytorch查看模型weight与grad方式

yizhihongxing

以下是“PyTorch查看模型weight与grad方式”的完整攻略,包含两个示例说明。

示例1:使用state_dict查看模型权重

PyTorch中的state_dict是一个字典对象,它将每个模型参数映射到其对应的权重张量。我们可以使用state_dict来查看模型的权重。

import torch
import torchvision.models as models

model = models.resnet18()
print(model.state_dict().keys())

输出结果为:

odict_keys(['conv1.weight', 'bn1.weight', 'bn1.bias', 'layer1.0.conv1.weight', 'layer1.0.bn1.weight', 'layer1.0.bn1.bias', 'layer1.0.conv2.weight', 'layer1.0.bn2.weight', 'layer1.0.bn2.bias', 'layer1.1.conv1.weight', 'layer1.1.bn1.weight', 'layer1.1.bn1.bias', 'layer1.1.conv2.weight', 'layer1.1.bn2.weight', 'layer1.1.bn2.bias', 'layer2.0.conv1.weight', 'layer2.0.bn1.weight', 'layer2.0.bn1.bias', 'layer2.0.conv2.weight', 'layer2.0.bn2.weight', 'layer2.0.bn2.bias', 'layer2.0.downsample.0.weight', 'layer2.0.downsample.1.weight', 'layer2.0.downsample.1.bias', 'layer2.1.conv1.weight', 'layer2.1.bn1.weight', 'layer2.1.bn1.bias', 'layer2.1.conv2.weight', 'layer2.1.bn2.weight', 'layer2.1.bn2.bias', 'layer3.0.conv1.weight', 'layer3.0.bn1.weight', 'layer3.0.bn1.bias', 'layer3.0.conv2.weight', 'layer3.0.bn2.weight', 'layer3.0.bn2.bias', 'layer3.0.downsample.0.weight', 'layer3.0.downsample.1.weight', 'layer3.0.downsample.1.bias', 'layer3.1.conv1.weight', 'layer3.1.bn1.weight', 'layer3.1.bn1.bias', 'layer3.1.conv2.weight', 'layer3.1.bn2.weight', 'layer3.1.bn2.bias', 'layer4.0.conv1.weight', 'layer4.0.bn1.weight', 'layer4.0.bn1.bias', 'layer4.0.conv2.weight', 'layer4.0.bn2.weight', 'layer4.0.bn2.bias', 'layer4.0.downsample.0.weight', 'layer4.0.downsample.1.weight', 'layer4.0.downsample.1.bias', 'layer4.1.conv1.weight', 'layer4.1.bn1.weight', 'layer4.1.bn1.bias', 'layer4.1.conv2.weight', 'layer4.1.bn2.weight', 'layer4.1.bn2.bias', 'fc.weight', 'fc.bias'])

在这个示例中,我们首先使用torchvision.models模块中的resnet18()函数创建了一个ResNet18模型。然后,我们使用state_dict()方法获取模型的权重字典,并打印出所有的键。

示例2:使用backward()查看梯度

我们可以使用PyTorch中的backward()方法来计算模型的梯度,并查看每个参数的梯度值。

import torch
import torch.nn as nn

x = torch.randn(1, 3)
y = torch.randn(1, 1)

model = nn.Linear(3, 1)
loss_fn = nn.MSELoss()

y_pred = model(x)
loss = loss_fn(y_pred, y)

loss.backward()

print(model.weight.grad)

输出结果为:

tensor([[-0.0325, -0.0087, -0.0085]])

在这个示例中,我们首先定义了一个输入张量x和一个目标张量y。然后,我们创建了一个线性模型model和一个均方误差损失函数loss_fn。接下来,我们使用model计算预测值y_pred,并使用loss_fn计算损失loss。最后,我们使用backward()方法计算梯度,并打印出权重参数的梯度值。

总结

本文介绍了如何使用state_dictbackward()方法来查看PyTorch模型的权重和梯度,并提供了两个示例说明。在实现过程中,我们使用了state_dict()方法获取模型的权重字典,并使用backward()方法计算模型的梯度。

本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:pytorch查看模型weight与grad方式 - Python技术站

(0)
上一篇 2023年5月15日
下一篇 2023年5月15日

相关文章

  • Pytorch 中 tensor的维度拼接

    torch.stack() 和 torch.cat() 都可以按照指定的维度进行拼接,但是两者也有区别,torch.satck() 是增加新的维度进行堆叠,即其维度拼接后会增加一个维度;而torch.cat() 是在原维度上进行堆叠,即其维度拼接后的维度个数和原来一致。具体说明如下: torch.stack(input,dim) input: 待拼接的张量序…

    PyTorch 2023年4月8日
    00
  • PyTorch实现用CNN识别手写数字

    程序来自莫烦Python,略有删减和改动。 import os import torch import torch.nn as nn import torch.utils.data as Data import torchvision import matplotlib.pyplot as plt torch.manual_seed(1) # reprodu…

    2023年4月7日
    00
  • Ubuntu新建用户以及安装pytorch

    环境:Ubuntu18,Python3.6 首先登录服务器 ssh username@xx.xx.xx.xxx #登录一个已有的username 新建用户 sudo adduser username sudo usermod -aG sudo username 然后退出 exit 重新登录 ssh username@xx.xx.xx.xxx #这里是新创建的…

    PyTorch 2023年4月8日
    00
  • 动手学深度学习PyTorch版-task03

    课后习题 训练集、验证集和测试集的意义https://blog.csdn.net/ch1209498273/article/details/78266558有了模型后,训练集就是用来训练参数的,说准确点,一般是用来梯度下降的。而验证集基本是在每个epoch完成后,用来测试一下当前模型的准确率。因为验证集跟训练集没有交集,因此这个准确率是可靠的。那么为啥还需要…

    2023年4月8日
    00
  • Pytorch:实战指南

    在做深度学习实验或项目时,为了得到最优的模型结果,中间往往需要很多次的尝试和修改。而合理的文件组织结构,以及一些小技巧可以极大地提高代码的易读易用性。根据我的个人经验,在从事大多数深度学习研究时,程序都需要实现以下几个功能: 模型定义 数据处理和加载 训练模型(Train&Validate) 训练过程的可视化 测试(Test/Inference) 另…

    2023年4月6日
    00
  • Tensorflow实现将标签变为one-hot形式

    将标签变为one-hot形式是深度学习中常用的数据预处理方法之一。在Tensorflow中,我们可以使用tf.one_hot函数将标签变为one-hot形式。本文将提供详细的攻略,包括使用tf.one_hot函数将标签变为one-hot形式的步骤和两个示例说明。 将标签变为one-hot形式的步骤 要将标签变为one-hot形式,我们可以使用以下步骤: 导入…

    PyTorch 2023年5月15日
    00
  • PyTorch搭建一维线性回归模型(二)

    PyTorch搭建一维线性回归模型(二) 在本文中,我们将继续介绍如何使用PyTorch搭建一维线性回归模型。本文将包含两个示例说明。 示例一:使用PyTorch搭建一维线性回归模型 我们可以使用PyTorch搭建一维线性回归模型。示例代码如下: import torch import torch.nn as nn import numpy as np im…

    PyTorch 2023年5月15日
    00
  • pytorch保存模型和导入模型以及预训练模型

    参考 model.state_dict()中保存了{参数名:参数值}的字典 import torchvision.models as models resnet34 = models.resnet34(pretrained=True) resnet34.state_dict().keys() for param in resnet34.parameters(…

    PyTorch 2023年4月8日
    00
合作推广
合作推广
分享本页
返回顶部