python - 使用 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 进行推理的权重。请帮忙。
解决方案
在 30 分钟内使用 Django 在 Heroku 部署 PyTorch 深度学习分类器
https://www.youtube.com/watch?v=MLk2We1rJPs
在 Django 应用程序中使用 PyTorch:
https://stefanbschneider.github.io/blog/pytorch-django
我希望这能帮助你开始!!!
推荐阅读
- dart - 颤振:输入'列表
' 不是类型 'List 的子类型 ' - dictionary - map[byte]int 和 map[string]int 有不同的内存使用
- java - 更改从 Java 控制器返回的 JSON 的默认结构
- windows - 如何使用命令行设置 Windows 服务以在系统关闭时运行
- c# - 在 While 循环中写入时,Console.ReadLine 缺少输入
- docker - Papertrail 不使用系统主机名
- kotlin - 调用带参数的高阶函数
- angular - 使用 @Input Observable var 对 Angular 7 组件进行单元测试并在 ngOnInit 方法中订阅此 var
- python - 如何处理多个包含斜杠的参数?
- java - 使用 JAXB 的动态 XML 属性