How can i read Pt or Pth files?

username: Hassan_Ghavidel
cooked: <p>I finished running of my code. and some part of results are Pth file that i guess are loss and validation plots in terms of epochs. but when i unzip them i see some files with pkl formats. May you help me how can i see the result of my plots? i put its download link here.</p>
<aside class="onebox allowlistedgeneric" data-onebox-src="https://accounts.google.com/v3/signin/identifier?continue=https%3A%2F%2Fdrive.usercontent.google.com%2Fdownload%3Fid%3D1Zo7gZta0DFjIATAV7Vay3AC5aBuBUGVY%26export%3Ddownload%26authuser%3D0%26confirm%3Dt%26uuid%3Dec1f5244-94e1-472c-b8ed-1284ce8ae003%26at%3DAPZUnTUzp6OZMN4u2EQwORllTCYq%3A1720214494371&amp;followup=https%3A%2F%2Fdrive.usercontent.google.com%2Fdownload%3Fid%3D1Zo7gZta0DFjIATAV7Vay3AC5aBuBUGVY%26export%3Ddownload%26authuser%3D0%26confirm%3Dt%26uuid%3Dec1f5244-94e1-472c-b8ed-1284ce8ae003%26at%3DAPZUnTUzp6OZMN4u2EQwORllTCYq%3A1720214494371&amp;ifkv=AdF4I759mlCJgRQjDdRVLaGVGf_SXb8hy1HW2ZOVrD3wUYwE-H_rJDUW3GH_F7yShYxGcLa_y-q4dA&amp;passive=1209600&amp;service=wise&amp;flowName=GlifWebSignIn&amp;flowEntry=ServiceLogin&amp;dsh=S50534468%3A1720214520901755&amp;ddm=0">
  <header class="source">

      <a href="https://accounts.google.com/v3/signin/identifier?continue=https%3A%2F%2Fdrive.usercontent.google.com%2Fdownload%3Fid%3D1Zo7gZta0DFjIATAV7Vay3AC5aBuBUGVY%26export%3Ddownload%26authuser%3D0%26confirm%3Dt%26uuid%3Dec1f5244-94e1-472c-b8ed-1284ce8ae003%26at%3DAPZUnTUzp6OZMN4u2EQwORllTCYq%3A1720214494371&amp;followup=https%3A%2F%2Fdrive.usercontent.google.com%2Fdownload%3Fid%3D1Zo7gZta0DFjIATAV7Vay3AC5aBuBUGVY%26export%3Ddownload%26authuser%3D0%26confirm%3Dt%26uuid%3Dec1f5244-94e1-472c-b8ed-1284ce8ae003%26at%3DAPZUnTUzp6OZMN4u2EQwORllTCYq%3A1720214494371&amp;ifkv=AdF4I759mlCJgRQjDdRVLaGVGf_SXb8hy1HW2ZOVrD3wUYwE-H_rJDUW3GH_F7yShYxGcLa_y-q4dA&amp;passive=1209600&amp;service=wise&amp;flowName=GlifWebSignIn&amp;flowEntry=ServiceLogin&amp;dsh=S50534468%3A1720214520901755&amp;ddm=0" target="_blank" rel="noopener nofollow ugc">accounts.google.com</a>
  </header>

  <article class="onebox-body">
    

<h3><a href="https://accounts.google.com/v3/signin/identifier?continue=https%3A%2F%2Fdrive.usercontent.google.com%2Fdownload%3Fid%3D1Zo7gZta0DFjIATAV7Vay3AC5aBuBUGVY%26export%3Ddownload%26authuser%3D0%26confirm%3Dt%26uuid%3Dec1f5244-94e1-472c-b8ed-1284ce8ae003%26at%3DAPZUnTUzp6OZMN4u2EQwORllTCYq%3A1720214494371&amp;followup=https%3A%2F%2Fdrive.usercontent.google.com%2Fdownload%3Fid%3D1Zo7gZta0DFjIATAV7Vay3AC5aBuBUGVY%26export%3Ddownload%26authuser%3D0%26confirm%3Dt%26uuid%3Dec1f5244-94e1-472c-b8ed-1284ce8ae003%26at%3DAPZUnTUzp6OZMN4u2EQwORllTCYq%3A1720214494371&amp;ifkv=AdF4I759mlCJgRQjDdRVLaGVGf_SXb8hy1HW2ZOVrD3wUYwE-H_rJDUW3GH_F7yShYxGcLa_y-q4dA&amp;passive=1209600&amp;service=wise&amp;flowName=GlifWebSignIn&amp;flowEntry=ServiceLogin&amp;dsh=S50534468%3A1720214520901755&amp;ddm=0" target="_blank" rel="noopener nofollow ugc">Google Drive: Sign-in</a></h3>

  <p>Access Google Drive with a Google account (for personal use) or Google Workspace account (for business use).</p>


  </article>

  <div class="onebox-metadata">
    
    
  </div>

  <div style="clear: both"></div>
</aside>

<p>Thanks in advance,</p>

username: ptrblck
cooked: <p>If these files were created using <code>torch.save</code> you can load them via <code>torch.load</code>.<br>
The file extension (<code>.pt</code> or <code>.pth</code>) can be anything.</p>

username: Hassan_Ghavidel
cooked: <p>Thanks i run a code like this</p>
<p>import torch</p>
<h1><a name="path-to-your-pth-or-pt-file-1" class="anchor" href="#path-to-your-pth-or-pt-file-1"></a>Path to your .pth or .pt file</h1>
<p>file_path = “/path_to_Pth_file/”</p>
<h1><a name="load-the-model-state-dictionary-2" class="anchor" href="#load-the-model-state-dictionary-2"></a>Load the model state dictionary</h1>
<p>model_state_dict = torch.load(file_path)</p>
<h1><a name="print-keys-in-model_state_dict-to-see-whats-available-3" class="anchor" href="#print-keys-in-model_state_dict-to-see-whats-available-3"></a>Print keys in model_state_dict to see what’s available</h1>
<p>print(model_state_dict.keys())</p>
<h1><a name="access-and-use-model_state_dict-as-needed-4" class="anchor" href="#access-and-use-model_state_dict-as-needed-4"></a>Access and use model_state_dict as needed</h1>
<p>and results was sth like this:</p>
<p>‘transformer.patch_embed.proj.bias’, ‘transformer.patch_embed.norm.weight’, ‘transformer.patch_embed.norm.bias’, ‘transformer.layers.0.blocks.0.norm1.weight’, ‘transformer.layers.0.blocks.0.norm1.bias’, ‘transformer.layers.0.blocks.0.attn.relative_position_bias_table’, ‘transformer.layers.0.blocks.0.attn.relative_position_index’, ‘transformer.layers.0.blocks.0.attn.qkv.weight’, ‘transformer.layers.0.blocks.0.attn.proj.weight’, ‘transformer.layers.0.blocks.0.attn.proj.bias’, ‘transformer.layers.0.blocks.0.norm2.weight’, ‘transformer.layers.0.blocks.0.norm2.bias’, ‘transformer.layers.0.blocks.0.mlp.fc1.weight’, ‘transformer.layers.0.blocks.0.mlp.fc1.bias’, ‘transformer.layers.0.blocks.0.mlp.fc2.weight’, ‘transformer.layers.0.blocks.0.mlp.fc2.bias’, ‘transformer.layers.0.blocks.1.norm1.weight’, ‘transformer.layers.0.blocks.1.norm1.bias’, ‘transformer.layers.0.blocks.1.attn.relative_position_bias_table’, ‘transformer.layers.0.blocks.1.attn.relative_position_index’, ‘transformer.layers.0.blocks.1.attn.qkv.weight’, ‘transformer.layers.0.blocks.1.attn.proj.weight’, ‘transformer.layers.0.blocks.1.attn.proj.bias’, ‘transformer.layers.0.blocks.1.norm2.weight’, ‘transformer.layers.0.blocks.1.norm2.bias’, ‘transformer.layers.0.blocks.1.mlp.fc1.weight’, ‘transformer.layers.0.blocks.1.mlp.fc1.bias’, ‘transformer.layers.0.blocks.1.mlp.fc2.weight’, ‘transformer.layers.0.blocks.1.mlp.fc2.bias’, ‘transformer.layers.0.downsample.reduction.weight’, ‘transformer.layers.0.downsample.norm.weight’, ‘transformer.layers.0.downsample.norm.bias’, ‘transformer.layers.1.blocks.0.norm1.weight’, ‘transformer.layers.1.blocks.0.norm1.bias’, ‘transformer.layers.1.blocks.0.attn.relative_position_bias_table’, ‘transformer.layers.1.blocks.0.attn.relative_position_index’, ‘transformer.layers.1.blocks.0.attn.qkv.weight’, ‘transformer.layers.1.blocks.0.attn.proj.weight’, ‘transformer.layers.1.blocks.0.attn.proj.bias’, ‘transformer.layers.1.blocks.0.norm2.weight’, ‘transformer.layers.1.blocks.0.norm2.bias’, ‘transformer.layers.1.blocks.0.mlp.fc1.weight’, ‘transformer.layers.1.blocks.0.mlp.fc1.bias’, ‘transformer.layers.1.blocks.0.mlp.fc2.weight’, ‘transformer.layers.1.blocks.0.mlp.fc2.bias’, ‘transformer.layers.1.blocks.1.norm1.weight’, ‘transformer.layers.1.blocks.1.norm1.bias’, ‘transformer.layers.1.blocks.1.attn.relative_position_bias_table’, ‘transformer.layers.1.blocks.1.attn.relative_position_index’, ‘transformer.layers.1.blocks.1.attn.qkv.weight’, ‘transformer.layers.1.blocks.1.attn.proj.weight’, ‘transformer.layers.1.blocks.1.attn.proj.bias’, ‘transformer.layers.1.blocks.1.norm2.weight’, ‘transformer.layers.1.blocks.1.norm2.bias’, ‘transformer.layers.1.blocks.1.mlp.fc1.weight’, ‘transformer.layers.1.blocks.1.mlp.fc1.bias’, ‘transformer.layers.1.blocks.1.mlp.fc2.weight’, ‘transformer.layers.1.blocks.1.mlp.fc2.bias’, ‘transformer.layers.1.downsample.reduction.weight’, ‘transformer.layers.1.downsample.norm.weight’, ‘transformer.layers.1.downsample.norm.bias’, ‘transformer.layers.2.blocks.0.norm1.weight’, ‘transformer.layers.2.blocks.0.norm1.bias’, ‘transformer.layers.2.blocks.0.attn.relative_position_bias_table’, ‘transformer.layers.2.blocks.0.attn.relative_position_index’, ‘transformer.layers.2.blocks.0.attn.qkv.weight’, ‘transformer.layers.2.blocks.0.attn.proj.weight’, ‘transformer.layers.2.blocks.0.attn.proj.bias’, ‘transformer.layers.2.blocks.0.norm2.weight’, ‘transformer.layers.2.blocks.0.norm2.bias’, ‘transformer.layers.2.blocks.0.mlp.fc1.weight’, ‘transformer.layers.2.blocks.0.mlp.fc1.bias’, ‘transformer.layers.2.blocks.0.mlp.fc2.weight’, ‘transformer.layers.2.blocks.0.mlp.fc2.bias’, ‘transformer.layers.2.blocks.1.norm1.weight’, ‘transformer.layers.2.blocks.1.norm1.bias’, ‘transformer.layers.2.blocks.1.attn.relative_position_bias_table’, ‘transformer.layers.2.blocks.1.attn.relative_position_index’, ‘transformer.layers.2.blocks.1.attn.qkv.weight’, ‘transformer.layers.2.blocks.1.attn.proj.weight’, ‘transformer.layers.2.blocks.1.attn.proj.bias’, ‘transformer.layers.2.blocks.1.norm2.weight’, ‘transformer.layers.2.blocks.1.norm2.bias’, ‘transformer.layers.2.blocks.1.mlp.fc1.weight’, ‘transformer.layers.2.blocks.1.mlp.fc1.bias’, ‘transformer.layers.2.blocks.1.mlp.fc2.weight’, ‘transformer.layers.2.blocks.1.mlp.fc2.bias’, ‘transformer.layers.2.blocks.2.norm1.weight’, ‘transformer.layers.2.blocks.2.norm1.bias’, ‘transformer.layers.2.blocks.2.attn.relative_position_bias_table’, ‘transformer.layers.2.blocks.2.attn.relative_position_index’, ‘transformer.layers.2.blocks.2.attn.qkv.weight’, ‘transformer.layers.2.blocks.2.attn.proj.weight’, ‘transformer.layers.2.blocks.2.attn.proj.bias’, ‘transformer.layers.2.blocks.2.norm2.weight’, ‘transformer.layers.2.blocks.2.norm2.bias’, ‘transformer.layers.2.blocks.2.mlp.fc1.weight’, ‘transformer.layers.2.blocks.2.mlp.fc1.bias’, ‘transformer.layers.2.blocks.2.mlp.fc2.weight’, ‘transformer.layers.2.blocks.2.mlp.fc2.bias’, ‘transformer.layers.2.blocks.3.norm1.weight’, ‘transformer.layers.2.blocks.3.norm1.bias’, ‘transformer.layers.2.blocks.3.attn.relative_position_bias_table’, ‘transformer.layers.2.blocks.3.attn.relative_position_index’, ‘transformer.layers.2.blocks.3.attn.qkv.weight’, ‘transformer.layers.2.blocks.3.attn.proj.weight’, ‘transformer.layers.2.blocks.3.attn.proj.bias’, ‘transformer.layers.2.blocks.3.norm2.weight’, ‘transformer.layers.2.blocks.3.norm2.bias’, ‘transformer.layers.2.blocks.3.mlp.fc1.weight’, ‘transformer.layers.2.blocks.3.mlp.fc1.bias’, ‘transformer.layers.2.blocks.3.mlp.fc2.weight’, ‘transformer.layers.2.blocks.3.mlp.fc2.bias’, ‘transformer.layers.2.downsample.reduction.weight’, ‘transformer.layers.2.downsample.norm.weight’, ‘transformer.layers.2.downsample.norm.bias’, ‘transformer.layers.3.blocks.0.norm1.weight’, ‘transformer.layers.3.blocks.0.norm1.bias’, ‘transformer.layers.3.blocks.0.attn.relative_position_bias_table’, ‘transformer.layers.3.blocks.0.attn.relative_position_index’, ‘transformer.layers.3.blocks.0.attn.qkv.weight’, ‘transformer.layers.3.blocks.0.attn.proj.weight’, ‘transformer.layers.3.blocks.0.attn.proj.bias’, ‘transformer.layers.3.blocks.0.norm2.weight’, ‘transformer.layers.3.blocks.0.norm2.bias’, ‘transformer.layers.3.blocks.0.mlp.fc1.weight’, ‘transformer.layers.3.blocks.0.mlp.fc1.bias’, ‘transformer.layers.3.blocks.0.mlp.fc2.weight’, ‘transformer.layers.3.blocks.0.mlp.fc2.bias’, ‘transformer.layers.3.blocks.1.norm1.weight’, ‘transformer.layers.3.blocks.1.norm1.bias’, ‘transformer.layers.3.blocks.1.attn.relative_position_bias_table’, ‘transformer.layers.3.blocks.1.attn.relative_position_index’, ‘transformer.layers.3.blocks.1.attn.qkv.weight’, ‘transformer.layers.3.blocks.1.attn.proj.weight’, ‘transformer.layers.3.blocks.1.attn.proj.bias’, ‘transformer.layers.3.blocks.1.norm2.weight’, ‘transformer.layers.3.blocks.1.norm2.bias’, ‘transformer.layers.3.blocks.1.mlp.fc1.weight’, ‘transformer.layers.3.blocks.1.mlp.fc1.bias’, ‘transformer.layers.3.blocks.1.mlp.fc2.weight’, ‘transformer.layers.3.blocks.1.mlp.fc2.bias’, ‘transformer.norm0.weight’, ‘transformer.norm0.bias’, ‘transformer.norm1.weight’, ‘transformer.norm1.bias’, ‘transformer.norm2.weight’, ‘transformer.norm2.bias’, ‘transformer.norm3.weight’, ‘transformer.norm3.bias’, ‘up0.conv1.0.weight’, ‘up0.conv2.0.weight’, ‘up1.conv1.0.weight’, ‘up1.conv2.0.weight’, ‘up2.conv1.0.weight’, ‘up2.conv2.0.weight’, ‘up3.conv1.0.weight’, ‘up3.conv2.0.weight’, ‘up4.conv1.0.weight’, ‘up4.conv2.0.weight’, ‘c1.0.weight’, ‘c2.0.weight’, ‘reg_head.0.weight’, ‘reg_head.0.bias’, ‘spatial_trans.grid’, ‘spatial_trans_seg.grid’])</p>
<p>i am still wondering how can i plot loss from these keys…</p>

username: anantguptadbl
cooked: <p>If you look closely these are more of the model structure ( the weights and biases of layers )<br>
Loss is more of a compute phenomenon that requires predictedOutput and actualOutput. You can use the model_state_dict to reload the model object and run the prediction again</p>
<p>e.g. code</p>
<ul>
<li>Saving the model</li>
</ul>
<pre><code class="lang-auto">torch.save(model, "modelFile.pth")
</code></pre>
<ul>
<li>In a different python session. We will reload the model and do a single prediction and calculate the loss</li>
</ul>
<pre><code class="lang-auto">import os
import numpy as np

import torch
from torch import nn
from torch.autograd import Variable

class linearRegressionOLS(nn.Module):
    def __init__(self):
        super(linearRegressionOLS,self).__init__()
        self.linearModel=nn.Linear(10,1)
    def forward(self,x):
        x = self.linearModel(x)
        return x

learning_rate=0.001
num_epochs=10
model=linearRegressionOLS()

model = torch.load("modelFile.pth")

# My Input Data
X=np.random.rand(100,10).astype(np.float32)
# My expected Output
Y=np.random.randint(2,size=(100)).reshape(100,1).astype(np.float32)
inputVal=Variable(torch.from_numpy(X).to('cuda'))
outputVal=Variable(torch.from_numpy(Y).to('cuda'))
criterion=nn.MSELoss()
dataOutput = model(inputVal)
# My loss between actual prediction and expected value
loss = criterion(dataOutput, outputVal)
print(loss)
</code></pre>