首页 > 解决方案 > 使用 django 使用 pytorch 将权重保存在 .tar 文件中进行推理

问题描述

我正在做一个机器学习项目,我需要在网页上显示预测。该网页是使用 Django 构建的。我有预测函数和模型的权重,但是如何在 Django 代码中集成预测函数、模型和权重并进行预测。

我的预测代码

def predicting(model, device, loader):
    model.eval()
    total_preds = torch.Tensor()
    total_labels = torch.Tensor()
    with torch.no_grad():
        for solute_graphs, solvent_graphs, solute_lens, solvent_lens in loader:
            outputs, i_map = model(
            [solute_graphs.to(device), solvent_graphs.to(device), torch.tensor(solute_lens).to(device),
             torch.tensor(solvent_lens).to(device)])   
            print(outputs)
            total_preds = torch.cat((total_preds, outputs.cpu()), 0)
    return total_preds.numpy().flatten()

我已将权重保存在.tar文件中,因此我需要在加载权重进行预测时运行模型。我不知道在哪里保存我的 PyTorch 模型以及使用 Django 进行推理的权重。请帮忙。

标签: pythondjangodjango-modelsdjango-viewspytorch

解决方案


在 30 分钟内使用 Django 在 Heroku 部署 PyTorch 深度学习分类器

https://www.youtube.com/watch?v=MLk2We1rJPs

在 Django 应用程序中使用 PyTorch:

https://stefanbschneider.github.io/blog/pytorch-django

我希望这能帮助你开始!!!


推荐阅读