Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
Vstay97 opened this issue Aug 26, 2021 · 0 comments
Open

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

Vstay97 opened this issue Aug 26, 2021 · 0 comments

Comments

@Vstay97
Copy link

Vstay97 commented Aug 26, 2021

如题,直接运行会报错。
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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant