LSTM(
(embed): Embedding(139948, 12, padding_idx=0)
(lstm): LSTM(12, 12, num_layers=2, batch_first=True, bidirectional=True)
(lin): Linear(in_features=240, out_features=6, bias=True)
)
Train epoch : 1, loss : 771.319284286499, accuracy :0.590
=================================================================================================
Traceback (most recent call last):enter code here
File "C:/Users/Administrator/PycharmProjects/untitled/example.py", line 297, in <module>
scores = model(x_test, x_test_seq_length)
File "C:\ProgramData\Anaconda3\lib\site-packages\torch\nn\modules\module.py", line 493, in __call__
result = self.forward(*input, **kwargs)
File "C:/Users/Administrator/PycharmProjects/untitled/example.py", line 141, in forward
x = self.embed(x) # sequence_length(max_len), batch_size, embed_size
File "C:\ProgramData\Anaconda3\lib\site-packages\torch\nn\modules\module.py", line 493, in __call__
result = self.forward(*input, **kwargs)
File "C:\ProgramData\Anaconda3\lib\site-packages\torch\nn\modules\sparse.py", line 117, in forward
self.norm_type, self.scale_grad_by_freq, self.sparse)
File "C:\ProgramData\Anaconda3\lib\site-packages\torch\nn\functional.py", line 1506, in embedding
return torch.embedding(weight, input, padding_idx, scale_grad_by_freq, sparse)
RuntimeError: Expected object of backend CUDA but got backend CPU for argument #3 'index'
它在训练集上工作得很好,但我在测试集中不断得到这个错误。我已经想了10个小时了。
有什么问题??
发布于 2019-11-22 12:29:26
您的程序似乎期望使用GPU运行,而不是在CPU上运行。请确保正确设置了程序的GPU设置,并且正在使用的CUDA版本是最新的。
你可以在这里找到更多的信息(假设你正在使用tensorflow):https://www.tensorflow.org/install/gpu
发布于 2019-11-22 17:36:28
如果您在CPU上训练了模型,那么测试数据将以某种方式加载并转换为CUDA数据类型。因此,您可以通过将输入张量移动到CPU设备来解决此问题。并移动模型(这不会有什么坏处)。
这可以像这样做:
>>> import torch
>>> device = torch.device("cpu")
>>> # move the model
>>> model = model.to(device)
>>> # move any input tensors
>>> test_data = test_data.to(device)
https://stackoverflow.com/questions/58994013
复制相似问题