You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Azure AutoML的PipelineWithYTransformations模型转ONNX失败求助

解决Azure AutoML PipelineWithYTransformations转ONNX的RuntimeError问题

方案1:使用Azure AutoML内置的ONNX导出功能(推荐)

你已经在AutoML配置中设置了enable_onnx_compatible_models=True,AutoML会自动为兼容模型生成ONNX格式版本,无需手动用skl2onnx转换。按以下步骤获取自动生成的ONNX模型:

  1. 连接到Azure ML工作区:
from azure.ai.ml import MLClient
from azure.identity import DefaultAzureCredential

ml_client = MLClient(
    DefaultAzureCredential(),
    subscription_id="你的订阅ID",
    resource_group_name="你的资源组名",
    workspace_name="你的工作区名"
)
  1. 获取目标AutoML作业并筛选ONNX模型资产:
# 替换为你的AutoML作业ID
job = ml_client.jobs.get("your-automl-job-id")

# 查找作业关联的ONNX格式模型
best_onnx_model = None
for model in ml_client.models.list(name=job.name):
    if model.tags.get("model_format") == "onnx":
        best_onnx_model = model
        break

# 下载ONNX模型到本地
if best_onnx_model:
    ml_client.models.download(
        name=best_onnx_model.name,
        version=best_onnx_model.version,
        download_path="./onnx_model"
    )

方案2:手动拆分管道并转换(仅当内置导出不可用时)

问题根源是PipelineWithYTransformations是Azure自定义管道类型,skl2onnx没有内置的形状计算器。你可以拆分管道,提取推理所需的核心部分(跳过目标变量Y的转换步骤)再转换:

  1. 加载本地.pkl模型并查看管道结构:
import pickle
from sklearn.pipeline import Pipeline

with open("你的模型文件.pkl", "rb") as f:
    automl_model = pickle.load(f)

# 查看管道内的步骤组件名称
print(automl_model.named_steps)
  1. 提取推理用的子管道(通常包含预处理和模型核心步骤):
# 根据实际输出的步骤名称调整组件名
inference_pipeline = Pipeline(steps=[
    ("preprocessor", automl_model.named_steps["preprocessor"]),
    ("model", automl_model.named_steps["model"])
])
  1. 使用skl2onnx转换子管道:
from skl2onnx import convert_sklearn
from skl2onnx.common.data_types import FloatTensorType

# 定义输入张量类型,替换为你的特征数量(示例为10个特征)
initial_types = [("input_features", FloatTensorType([None, 10]))]
onnx_model = convert_sklearn(inference_pipeline, initial_types=initial_types)

# 保存转换后的ONNX模型
with open("converted_model.onnx", "wb") as f:
    f.write(onnx_model.SerializeToString())

注意事项

  • 如果预处理步骤包含Azure自定义转换器,skl2onnx可能仍无法转换,此时优先使用方案1。
  • 确保skl2onnx版本与AutoML使用的scikit-learn版本兼容,避免版本冲突导致转换失败。

内容的提问来源于stack exchange,提问作者elldora

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.09 01:40:18