torch转onnx torch.jit.script模式 crf解码问题

    torch模型pth转onnx格式时,默认为trace方式,它不支持动态for循环。

    而在crf解码的过程中,需要传入seq_length,然后循环解码,这里就需要用到script模式。

    注意:

     1、转onnx不支持reversed,需要进行改写

     2、seq_length要在torch.jit.script里转为int类型

     3、最后返回的best_tags是list[tensor],要转换为tensor类型,因为onnx只支持tensor的输入和输出。

     4、下面这个例子,最后得到的best_tags还要reversed一下。

@torch.jit.script
def viterbi_decode(start_transitions, transitions, end_transitions, emissions, seq_length):
    seq_length = int(seq_length)
    score = start_transitions + emissions[0]
    history = []

    for i in range(1, seq_length):
        broadcast_score = score.unsqueeze(1)
        broadcast_emission = emissions[i].unsqueeze(0)
        next_score = broadcast_score + transitions + broadcast_emission
        next_score, indices = next_score.max(dim=0)
        score = next_score
        history.append(indices)

    score += end_transitions
    _, best_last_tag = score.max(dim=0)
    best_tags = [best_last_tag]
    for i in range(len(history) - 1, -1, -1):
        hist = history[i]
        best_last_tag = hist[best_tags[-1]]
        best_tags.append(best_last_tag)

    best_tags = torch.stack(best_tags, dim=0)
    return best_tags, score

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值