从 *.pth 文件查看 Pytorch 权重

发布于 2025-01-19 07:19:45 字数 330 浏览 5 评论 0原文

我有一个使用 Pytorch 创建的带有权重的 .pth 文件。我如何才能查看该文件中的权重?

我尝试加载和查看此代码,但它不起作用(作为新手,我可能完全错了)-

import torch
import torchvision.models as models

torch.save('weights\kharif_crops_final.pth')

models.load_state_dict(torch.load('weights\kharif_crops_final.pth'))
models.eval()
print(models)

I have a .pth file created with Pytorch with weights. How would I be able to view the weights from this file?

I tried this code to load and view but it was not working (as a newbie, I might be entirely wrong)-

import torch
import torchvision.models as models

torch.save('weights\kharif_crops_final.pth')

models.load_state_dict(torch.load('weights\kharif_crops_final.pth'))
models.eval()
print(models)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

子栖 2025-01-26 07:19:45
import torch

model = torch.load('path')
print(model)

(验证并确认)

import torch

model = torch.load('path')
print(model)

(Verify and confirm)

一萌ing 2025-01-26 07:19:45
PATH = 'weights\kharif_crops_final.pth'
state = {'model': model.state_dict()}
torch.save(state, PATH)
model.load_state_dict(torch.load(PATH)['model'])
# print weights
for k, v in model.named_parameters():
    print(k, v)
PATH = 'weights\kharif_crops_final.pth'
state = {'model': model.state_dict()}
torch.save(state, PATH)
model.load_state_dict(torch.load(PATH)['model'])
# print weights
for k, v in model.named_parameters():
    print(k, v)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文