HIT-SCIR / plm-nlp-code

Apache License 2.0
626 stars 195 forks source link

你好,第四章的lstm_sent_polarity.py无法运行 #5

Open Vstay97 opened 3 years ago

Vstay97 commented 3 years ago

如题,直接运行会报错。 RuntimeError: 'lengths' argument should be a 1D CPU int64 tensor, but got 1D cuda:0 Long tensor

需要把'lengths'放到cpu中去,才可以。

将第40行的 x_pack = pack_padded_sequence(embeddings, lengths, batch_first=True, enforce_sorted=False) 改为 x_pack = pack_padded_sequence(embeddings, lengths.cpu(), batch_first=True, enforce_sorted=False)

同样的,当电脑中同时有CUDA和CPU环境时,transformer_sent_polarity.py文件也会报错 RuntimeError: Expected all tensors to be on the same device, but found at least two devices, cuda:0 and cpu! 第四章文件utils.py的第25行 mask = torch.arange(max_len).expand(lengths.shape[0], max_len) < lengths.unsqueeze(1) 也应修改为: mask = torch.arange(max_len).expand(lengths.shape[0], max_len).cuda() < lengths.unsqueeze(1)