【问题标题】:Pytorch giving runtimeerror can't be cast to the desired output type LongPytorch 给出 runtimeerror 无法转换为所需的输出类型 Long
【发布时间】:2022-08-08 23:28:02
【问题描述】:

以下代码给出运行时错误“结果类型 Float 无法转换为所需的输出类型 Long”。

我已经尝试执行以下操作:

从:torch.div(self.indices_buf, vocab_size, out=self.beams_buf)

至:torch.div(self.indices_buf, vocab_size, out=self.beams_buf).type_as(torch.LongTensor)

有问题的代码:

class BeamSearch(Search):

    def __init__(self, tgt_dict):
        super().__init__(tgt_dict)

    def step(self, step, lprobs, scores):
        super()._init_buffers(lprobs)
        bsz, beam_size, vocab_size = lprobs.size()

        if step == 0:
            # at the first step all hypotheses are equally likely, so use
            # only the first beam
            lprobs = lprobs[:, ::beam_size, :].contiguous()
        else:
            # make probs contain cumulative scores for each hypothesis
            lprobs.add_(scores[:, :, step - 1].unsqueeze(-1))

        torch.topk(
            lprobs.view(bsz, -1),
            k=min(
                # Take the best 2 x beam_size predictions. We\'ll choose the first
                # beam_size of these which don\'t predict eos to continue with.
                beam_size * 2,
                lprobs.view(bsz, -1).size(1) - 1,  # -1 so we never select pad
            ),
            out=(self.scores_buf, self.indices_buf),
        )
        torch.div(self.indices_buf, vocab_size, out=self.beams_buf).type_as(torch.LongTensor)
        self.indices_buf.fmod_(vocab_size)
        return self.scores_buf, self.indices_buf, self.beams_buf

此代码来自 fairseq。

    标签: python deep-learning pytorch tensor fairseq


    【解决方案1】:

    我有同样的问题。你得到解决方案了吗?

    【讨论】:

      猜你喜欢
      • 2022-01-03
      • 1970-01-01
      • 2021-04-01
      • 2020-10-07
      • 2020-05-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-01
      相关资源
      最近更新 更多