bentrevett / pytorch-pos-tagging

A tutorial on how to implement models for part-of-speech tagging using PyTorch and TorchText.
MIT License
178 stars 27 forks source link

Google Colab : GPU Error #6

Open sorghosh opened 3 years ago

sorghosh commented 3 years ago

Hi Ben..fanstactic repo.. quick one while running the same code - BiLSTM for PoS Tagging , I am getting the error in google colab :

RuntimeError: Expected all tensors to be on the same device, but found at least two devices, cuda:0 and cpu!

Please suggest

FelixJuch commented 3 years ago

Adding a .cuda() in the return statement in categorical_accuracy fixed it for me:

def categorical_accuracy(preds, y, tag_pad_idx): """ Returns accuracy per batch, i.e. if you get 8/10 right, this returns 0.8, NOT 8 """ max_preds = preds.argmax(dim = 1, keepdim = True) # get the index of the max probability non_pad_elements = (y != tag_pad_idx).nonzero() correct = max_preds[non_pad_elements].squeeze(1).eq(y[non_pad_elements]) return correct.sum() / torch.FloatTensor([y[non_pad_elements].shape[0]]).cuda()