在不同的Python文件中加载保存的NN模型

在不同的Python文件中加载保存的NN模型,python,deep-learning,pytorch,Python,Deep Learning,Pytorch,我正在尝试从Pytorch实现代码。但是我已经编写了将保存的模型加载到另一个Python文件中的代码。 FashionClassify文件包含的代码与教程中的代码完全相同 代码如下: from FashionClassify import NeuralNetwork from FashionClassify import test_data import torch model = NeuralNetwork() model.load_state_dict(torch.load("m

我正在尝试从Pytorch实现代码。但是我已经编写了将保存的模型加载到另一个Python文件中的代码。
FashionClassify
文件包含的代码与教程中的代码完全相同

代码如下:

from FashionClassify import NeuralNetwork
from FashionClassify import test_data
import torch

model = NeuralNetwork()
model.load_state_dict(torch.load("model.pth"))
classes = [
    "T-shirt/top", "Trouser","Pullover","Dress","Coat","Sandal","Shirt","Sneaker","Bag","Ankle boot",
]

model.eval()
x, y = test_data[0][0], test_data[0][1]
with torch.no_grad():
    pred = model(x)
    predicted, actual = classes[pred[0].argmax(0)],classes[y]
    print(f'Predicted: "{predicted}", Actual: "{actual}"')
但是,当我运行此命令时,整个培训过程将再次开始。为什么会这样? 或 这是一种预期的行为吗

(我已经浏览了几个网页和StackOverflow答案,但找不到我的问题)

格式文件代码:

import torch
from torch import nn
from torch.utils.data import DataLoader  # wraps an iterable around dataset
from torchvision import datasets  # stores samples and their label
from torchvision.transforms import ToTensor, Lambda, Compose
import matplotlib as plt

training_data = datasets.FashionMNIST(root='data', train=True, download=True, transform=ToTensor(), )
test_data = datasets.FashionMNIST(root='data', train=False, download=True, transform=ToTensor(), )

batch_size = 64
train_dataLoader = DataLoader(training_data, batch_size=batch_size)
test_dataLoader = DataLoader(test_data, batch_size=batch_size)

for X, y in test_dataLoader:
    print('Shape of X [N,C,H,W]:', X.size())
    print('Shape of y:', y.shape, y.dtype)
    break

device = 'cuda' if torch.cuda.is_available() else 'cpu'
print('Using {} device'.format(device))


# to define a NN, we inherit a class from nn.Module
class NeuralNetwork(nn.Module):
    def __init__(self):
        # will specify how data will proceed in the forward pass
        super(NeuralNetwork, self).__init__()
        self.flatten = nn.Flatten()
        self.linear_relu_stack = nn.Sequential(
            nn.Linear(28 * 28, 512),
            nn.ReLU(),
            nn.Linear(512, 512),
            nn.ReLU(),
            nn.Linear(512, 10),
            nn.ReLU()
        )

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


model = NeuralNetwork().to(device)
print(model)

loss_fn = nn.CrossEntropyLoss()
optimizer = torch.optim.SGD(model.parameters(), lr=1e-3)


def train(dataloader, model, loss_fn, optimizer):
    size = len(dataloader.dataset)
    for batch, (X,y) in enumerate(dataloader):
        X,y = X.to(device), y.to(device)

        #compute prediction error
        pred = model(X)
        loss = loss_fn(pred, y)

        #backprop
        optimizer.zero_grad()
        loss.backward()
        optimizer.step()

        if batch%100 ==0:
            loss,current = loss.item(), batch * len(X)
            print(f"loss: {loss:>7f} [{current:>5d}/{size:>5d}]")

def test(dataloader, model):
    size = len(dataloader.dataset)
    model.eval()
    test_loss, correct = 0,0
    with torch.no_grad():
        for X, y in dataloader:
            X,y = X.to(device), y.to(device)
            pred = model(X)
            test_loss += loss_fn(pred, y).item()
            correct +=  (pred.argmax(1) == y).type(torch.float).sum().item()
    test_loss /= size
    correct /= size
    print(f"Test Error: \n Accuracy: {(100*correct):>0.1f}%, Avg loss: {test_loss:>8f} \n")

epochs = 5
for t in range(epochs):
    print(f"Epoch {t+1}\n-------------------------------")
    train(train_dataLoader, model, loss_fn, optimizer)
    test(test_dataLoader, model)
print("Done!")

torch.save(model.state_dict(), "model.pth")
print("Saved PyTorch Model State to model.pth")

这就是导入另一个文件时发生的情况。所有代码都将重新运行

相反,在您的培训文件中:

class FancyNetwork(nn.Module):
    [...]

def train():
    [train code]

if __name__ == "__main__":
    train()
现在,当您运行此文件时,train()将被调用,但当您将此文件导入另一个文件时,train不会自动被调用