'Viewing Pytorch weights from a *.pth file

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)


Solution 1:[1]

import torch

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

(Verify and confirm)

Solution 2:[2]

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)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 user18174484
Solution 2 ki-ljl