我正在做一个机器学习项目,我需要在网页上显示预测。该网页是使用 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 进行推理的权重。请帮忙。