RuntimeError: Expected all tensors to be on the same device, but found at least two devices
Question:
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)
Answers:
Move the model to()
the device:
model = NeuralNetwork(input_size, hidden_size, output_size).to(device)
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)
Move the model to()
the device:
model = NeuralNetwork(input_size, hidden_size, output_size).to(device)