Python – onnx导出模型出现RuntimeError: Exporting the operator pad_sequence to ONNX opset version 13 is not supported错误
本文作者:StubbornHuang
版权声明:本文为站长原创文章,如果转载请注明原文链接!
原文标题:Python – onnx导出模型出现RuntimeError: Exporting the operator pad_sequence to ONNX opset version 13 is not supported错误
原文链接:https://www.stubbornhuang.com/2430/
发布于:2022年12月01日 13:26:42
修改于:2022年12月01日 13:26:42

1 onnx导出模型出现RuntimeError: Exporting the operator pad_sequence to ONNX opset version 13 is not supported错误
今天在pytorch中导出模型为onnx时,由于使用了from torch.nn.utils.rnn import pad_sequence
,在执行torch.onnx.export
导出模型时出现了以下的错误
RuntimeError: Exporting the operator pad_sequence to ONNX opset version 13 is not supported. Please feel free to request support or submit a pull request on PyTorch GitHub.
意思就是这个算法pad_seqeunce
在onnx中不支持,我们可以自定义一下这个函数,就是将一个列表中不等长的Tensor自动补齐到最大长度,自定义的pad_seqeunce
代码如下
def pad_sequence(sequences, batch_first=False, max_len=None, padding_value=0):
"""
对一个List中的元素进行padding
sequences:
batch_first: 是否把batch_size放到第一个维度
padding_value:
max_len : 最大句子长度,默认为None,即在每个batch中以最长样本的长度对其它样本进行padding;
当指定max_len的长度小于一个batch中某个样本的长度,那么在这个batch中还是会以最长样本的长度对其它样本进行padding
建议指定max_len的值为整个数据集中最长样本的长度
Returns:
"""
max_size = sequences[0].size()
trailing_dims = max_size[1:]
length = max_len
max_len = max([s.size(0) for s in sequences])
if length is not None:
max_len = max(length, max_len)
if batch_first:
out_dims = (len(sequences), max_len) + trailing_dims
else:
out_dims = (max_len, len(sequences)) + trailing_dims
out_tensor = sequences[0].data.new(*out_dims).fill_(padding_value)
for i, tensor in enumerate(sequences):
length = tensor.size(0)
# use index notation to prevent duplicate references to the tensor
if batch_first:
out_tensor[i, :length, ...] = tensor
else:
out_tensor[:length, i, ...] = tensor
return out_tensor
使用自定义的pad_sequence
函数替换掉from torch.nn.utils.rnn import pad_sequence
函数,重新训练并导出为onnx即可。
参考连接
当前分类随机文章推荐
- TensorRT - 安装TensorRT工具Polygraphy 阅读3408次,点赞0次
- TensorRT - 解决INVALID_ARGUMENT: getPluginCreator could not find plugin ScatterND version 1,TensorRT找不到ScatterND插件的问题 阅读2824次,点赞0次
- TensorRT - 自带工具trtexec的参数使用说明 阅读3762次,点赞0次
- TensorRT - Using PreviewFeaturekFASTER_DYNAMIC_SHAPES_0805 can help improve performance and resolve potential functional issues 阅读148次,点赞0次
- TensorRT - Polygraphy工具的使用 阅读3827次,点赞0次
- Python - 使用onnxruntime加载和推理onnx模型 阅读131次,点赞0次
- TensorRT - workspace的作用 阅读157次,点赞0次
- TensorRT - 使用torch普通算子组合替代torch.einsum爱因斯坦求和约定算子的一般性方法 阅读2408次,点赞1次
- TensorRT - 扩展TensorRT C++API的模型输入维度,增加Dims5,Dims6,Dims7,Dims8 阅读1515次,点赞0次
- TensorRT - 喜大普奔,TensorRT8.2 EA起开始支持Einsum爱因斯坦求和算子 阅读1410次,点赞0次
全站随机文章推荐
- WordPress - robots.txt 阅读2384次,点赞0次
- 资源分享 - PHP与MySQL程序设计(第3版) 中文 PDF下载 阅读1871次,点赞0次
- C++ - single header跨平台高效开源日志库Easylogging++的配置和使用 阅读277次,点赞0次
- 一文带你从欧拉角的角度搞懂左手坐标系到右手坐标系的转换 阅读13211次,点赞18次
- WordPress - get_post_type():获取当前文章或者给定文章类型 阅读1884次,点赞0次
- 资源分享 - Handbook of Computer Aided Geometric Design 英文高清PDF下载 阅读1539次,点赞0次
- 资源分享 - Real-Time Volume Graphics 英文高清PDF下载 阅读1718次,点赞0次
- C++ - 拷贝构造函数与拷贝构造函数调用时机 阅读153次,点赞0次
- 资源分享 - Vulkan学习指南 , Learning Vulkan 中文版PDF下载 阅读1429次,点赞0次
- Pytorch - reshape和view的用法和区别 阅读85次,点赞0次
评论
164