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

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

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

问题

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

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

device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') # 设备为cuda

with open('intents.json') as f:
    intents = json.load(f)

file = 'data.pth'
data = torch.load(file)

input_size = data['input_size']
model_state = data['model_state']
output_size = data['output_size']
hidden_size = data['hidden_size']
all_words = data['all_words']
tags = data['tags']

model = NeuralNetwork(input_size, hidden_size, output_size)
model.load_state_dict(model_state)
model.eval()

@jit(target_backend='cuda')
def get_response(pattern):
    sentence = tokenize(pattern)
    BoW = bag_of_word(sentence, all_words)
    BoW = torch.from_numpy(BoW).to(device)
    output = model.forward_propagation(BoW)
    # print(output)
    _, predicted = torch.max(output, dim=-1)
    tag = tags[predicted.item()] # 给出输入语音的预测标签
    # print(tag)
    probs = torch.softmax(output, dim=-1)  # 使输出概率在-1和1之间
    # print(props)
    prob = probs[predicted.item()] # 选择最大概率
    # print(prob)
    return prob, tag

pattern = speech_to_text()
prob, tag = get_response(pattern)
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:

device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') # device = cuda

with open('intents.json') as f:
    intents = json.load(f)

file = 'data.pth'
data = torch.load(file)

input_size = data['input_size']
model_state = data['model_state']
output_size = data['output_size']
hidden_size = data['hidden_size']
all_words = data['all_words']
tags = data['tags']

model = NeuralNetwork(input_size,hidden_size,output_size)
model.load_state_dict(model_state)
model.eval()

@jit(target_backend='cuda')  
def get_response(pattern):
    sentence = tokenize(pattern)
    BoW = bag_of_word(sentence,all_words)
    BoW = torch.from_numpy(BoW).to(device)
    output = model.forward_propagation(BoW)
    # print(output)
    _,predicted = torch.max(output,dim=-1)
    tag = tags[predicted.item()] # give prediction tag for input speech
    # print(tag)
    probs = torch.softmax(output,dim=-1)  # to make output probability between -1 and 1
    # print(props)
    prob = probs[predicted.item()] # to select the big probability
    # print(prob)
    return prob,tag

pattern = speech_to_text()
prob,tag = get_response(pattern)
print(tag)

答案1

得分: 1

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

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

Move the model to() the device:

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:

确定