Traceback (most recent call last):
File "d:/HY_22/02-code/23summervac/IntentBert-finetune - lstm/transfer-wuMLM.py", line 235, in <module>
main()
File "d:/HY_22/02-code/23summervac/IntentBert-finetune - lstm/transfer-wuMLM.py", line 198, in main
trainer.train(model, tok, args.mode)
File "d:\HY_22\02-code\23summervac\IntentBert-finetune - lstm\utils\Trainer.py", line 174, in train
logits = self.LSTM_forward(model,X,beforeBatchNorm=self.beforeBatchNorm)
File "d:\HY_22\02-code\23summervac\IntentBert-finetune - lstm\utils\Trainer.py", line 95, in LSTM_forward
output, (hn, cn) = self.lstm(inputs)
File "C:\Users\10918\miniconda3\envs\pytorch\lib\site-packages\torch\nn\modules\module.py", line 1130, in _call_impl
return forward_call(*input, **kwargs)
File "C:\Users\10918\miniconda3\envs\pytorch\lib\site-packages\torch\nn\modules\rnn.py", line 769, in forward
result = _VF.lstm(input, hx, self._flat_weights, self.bias, self.num_layers,
RuntimeError: Input and parameter tensors are not at the same device, found input tensor at cuda:0 and parameter tensor at cpu
这个是lstm的模型,想将其放在gpu上跑,最后我的修改为
class Trainer(TrainerBase):
def __init__(
self.lstm = nn.LSTM(768, 100).to('cuda')
self.dropout = nn.Dropout(0.1).to('cuda')
self.fc2 = nn.Linear(100, self.clsNum).to('cuda')
def LSTM_forward(self, model,X,beforeBatchNorm=False):
inputs = batchEmbedding.float().to(model.device)
output, (hn, cn) = self.lstm(inputs)
x = torch.mean(output, dim=1)
x = self.dropout(x)
outs = self.fc2(x)
return outs
训练阶段
model = model.to(model.device)
logits = self.LSTM_forward(model,X,beforeBatchNorm=self.beforeBatchNorm)