RuntimeError: 期望所有张量在相同的设备上,但至少发现两个不同的设备

huangapple go评论330阅读模式
英文:

RuntimeError: Expected all tensors to be on the same device, but found at least two devices

问题

RuntimeError: 预期所有张量位于相同设备上,但发现至少有两个设备,cuda:0 和 cpu!(在检查方法wrapper_mm中参数mat2的参数时)

我尝试在GPU上运行此代码并打印标签,但出现了此错误。我的训练代码如下:

  1. device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') # 设备为cuda
  2. with open('intents.json') as f:
  3. intents = json.load(f)
  4. file = 'data.pth'
  5. data = torch.load(file)
  6. input_size = data['input_size']
  7. model_state = data['model_state']
  8. output_size = data['output_size']
  9. hidden_size = data['hidden_size']
  10. all_words = data['all_words']
  11. tags = data['tags']
  12. model = NeuralNetwork(input_size, hidden_size, output_size)
  13. model.load_state_dict(model_state)
  14. model.eval()
  15. @jit(target_backend='cuda')
  16. def get_response(pattern):
  17. sentence = tokenize(pattern)
  18. BoW = bag_of_word(sentence, all_words)
  19. BoW = torch.from_numpy(BoW).to(device)
  20. output = model.forward_propagation(BoW)
  21. # print(output)
  22. _, predicted = torch.max(output, dim=-1)
  23. tag = tags[predicted.item()] # 给出输入语音的预测标签
  24. # print(tag)
  25. probs = torch.softmax(output, dim=-1) # 使输出概率在-1和1之间
  26. # print(props)
  27. prob = probs[predicted.item()] # 选择最大概率
  28. # print(prob)
  29. return prob, tag
  30. pattern = speech_to_text()
  31. prob, tag = get_response(pattern)
  32. print(tag)
英文:

I have gotten the response from the chatbot using GPU, i get the following errors:

RuntimeError: Expected all tensors to be on the same device, but found at least two devices, cuda:0 and cpu! (when checking argument for argument mat2 in method wrapper_mm)

I tried running this code on GPU and print tag but I get on this error.
My training code is as follows:

  1. device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') # device = cuda
  2. with open('intents.json') as f:
  3. intents = json.load(f)
  4. file = 'data.pth'
  5. data = torch.load(file)
  6. input_size = data['input_size']
  7. model_state = data['model_state']
  8. output_size = data['output_size']
  9. hidden_size = data['hidden_size']
  10. all_words = data['all_words']
  11. tags = data['tags']
  12. model = NeuralNetwork(input_size,hidden_size,output_size)
  13. model.load_state_dict(model_state)
  14. model.eval()
  15. @jit(target_backend='cuda')
  16. def get_response(pattern):
  17. sentence = tokenize(pattern)
  18. BoW = bag_of_word(sentence,all_words)
  19. BoW = torch.from_numpy(BoW).to(device)
  20. output = model.forward_propagation(BoW)
  21. # print(output)
  22. _,predicted = torch.max(output,dim=-1)
  23. tag = tags[predicted.item()] # give prediction tag for input speech
  24. # print(tag)
  25. probs = torch.softmax(output,dim=-1) # to make output probability between -1 and 1
  26. # print(props)
  27. prob = probs[predicted.item()] # to select the big probability
  28. # print(prob)
  29. return prob,tag
  30. pattern = speech_to_text()
  31. prob,tag = get_response(pattern)
  32. print(tag)

答案1

得分: 1

将模型 to() 移动到设备上:

  1. model = NeuralNetwork(input_size, hidden_size, output_size).to(device)
英文:

Move the model to() the device:

  1. model = NeuralNetwork(input_size, hidden_size, output_size).to(device)

huangapple
  • 本文由 发表于 2023年2月24日 01:01:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/75548003.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定