icon

Read PT File

Last modified: 2023-03-26

A PT file is a machine learning model file generated by PyTorch.

Load Model from PT

import torch
import torch.nn as nn

class ExampleModel(nn.Module):
    def __init__(self):
        super().__init__()
        self.flatten = nn.Flatten()
        self.linear_relu_stack = nn.Sequential(
            nn.Linear(28*28, 512),
            nn.ReLU(),
            nn.Linear(512, 10))

    def forward(self, x):
        x = self.flatten(x)
        logits = self.linear_relu_stack(x)
        return logits

model = ExampleModel()
model.load_state_dict(torch.load('example.pt'))
print(model)