在django的views视图中加载pytorch模型,但是提示没有自己写的model
def insert(request):
if request.method == "POST":
username = request.POST.get("username")
text_ints = Init.converts(username)
new_text_ints = Init.reset_text([text_ints], seq_len=200) # 注意这里要添加一个[],因为,reset_text处理的二维数据
text_tensor = torch.from_numpy(new_text_ints)
load_model = torch.load("E:/Code/Pycharm/Django/NLP/main_other.pkl")
device = "cuda" if torch.cuda.is_available() else "cpu"
batch_size = text_tensor.size(0) # 这里是1
hs = load_model.init_hidden(batch_size) # 初始化隐藏状态
text_tensor = text_tensor.to(device)
pred, hs = load_model(text_tensor, hs) # 判断
pred = torch.round(pred)
pred = "评论正面" if pred==1 else "评论负面"
twz = models.message.objects.create(username=username,pred=pred)
twz.save()
return redirect('show/')
return render(request, "insert.html")