nn.utils.rnn.pack_padded_sequence: RuntimeError: ‘lengths‘ argument should be a 1D CPU int64 tensor

本文介绍了解决PyTorch中RNN模块使用时遇到的关于张量设备(CPU/GPU)不匹配的问题。当使用pack_padded_sequence函数时,如果长度参数为Tensor类型,则必须确保该Tensor位于CPU上。文章提供了具体的解决方案,并解释了背后的原因。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

解决方法:对length调用.cpu()即可

packed = rnn.pack_padded_sequence(x, x_len.cpu(), batch_first=True)

原因:

It’s because of this PR #41984, that preserves the device of as_tensor argument if it’s torch tensor. pack_padded_sequence calls as_tensor on the lengths tensor: https://github.com/pytorch/pytorch/blob/master/torch/nn/utils/rnn.py#L234. It caused implicit copy before, but does not now.
Given that implementation does not do anything smart with the lengths on the GPU, and only copies and synchronizes behind users back, @myleott do you think we should restore previous behavior, or can you call .cpu() on the lengths in your script before calling pack_padded_sequence?

输入到pack_padded_sequence如果是tensor形式,必须要保证其在CPU()上

参考自:
https://github.com/pytorch/pytorch/issues/43227

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值