galsang / BiDAF-pytorch

Re-implementation of BiDAF(Bidirectional Attention Flow for Machine Comprehension, Minjoon Seo et al., ICLR 2017) on PyTorch.
244 stars 85 forks source link

RuntimeError: Expected object of backend CUDA but got backend CPU for argument #3 'index'.There is a small bug during running on gpu #14

Closed IrvingBei closed 5 years ago

IrvingBei commented 5 years ago

There is a small bug during running on gpu causing by the version of torchtext!

when I run this code on GPU, unfortunately,

RuntimeError: Expected object of backend CUDA but got backend CPU for argument #3 'index'

So, I guess the tensor may be not on GPU, and there is a warning:

The `device` argument should be set by using `torch.device` or passing a string as an argument. This behavior will be deprecated soon and currently defaults to CPU.

I change the parameter device, and then working now.

device = torch.device(f"cuda:{args.gpu}" if torch.cuda.is_available() else "cpu")
self.train_iter, self.dev_iter = \
            data.BucketIterator.splits((self.train, self.dev),
            batch_sizes=[args.train_batch_size, args.dev_batch_size],
            device=device,
            sort_key=lambda x: len(x.c_word))

maybe you could change it (●'◡'●)

galsang commented 5 years ago

solved by #15