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


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)

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)
Asked By: Mohamed Adel



Move the model to() the device:

model = NeuralNetwork(input_size, hidden_size, output_size).to(device)
Answered By: blue_lama
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.