[Python] Snowflake ML Registry for Model Explainability: `ValueError: Model type <class...

Discussão em 'Python' iniciado por Stack, Março 7, 2025.

  1. Stack

    Stack Membro Participativo

    I am training an XGBClassifier model using Snowflake ML and attempting to register it using Snowflake’s model registry. The training and evaluation steps complete successfully, but when I try to log the model using log_model(), I get the following error:

    Training Accuracy: 0.9610
    Evaluation Accuracy: 0.8730
    Traceback (most recent call last):
    File "C:\Mas\rla_projects\Claims-AI--lodgement\code\python\src\explain\train_model.py", line 56, in <module>
    model_version = native_registry.log_model(
    File "C:\Mas\py39_sf_ml\lib\site-packages\snowflake\ml\_internal\telemetry.py", line 542, in wrap
    return ctx.run(execute_func_with_statement_params)
    ...
    File "C:\Mas\py39_sf_ml\lib\site-packages\snowflake\ml\model\_packager\model_task\model_task_utils.py", line 149, in _get_model_task
    raise ValueError(f"Model type {type(model)} is not supported")
    ValueError: Model type <class 'NoneType'> is not supported


    Code Snippet:

    from snowflake.snowpark import Session
    from snowflake.ml.registry import registry
    from snowflake.ml.modeling.preprocessing import StandardScaler
    from snowflake.ml.modeling.impute import SimpleImputer
    from snowflake.ml.modeling.pipeline import Pipeline
    from snowflake.ml.modeling.xgboost import XGBClassifier

    # Snowflake connection parameters
    conn_params = {
    "user": "<...>",
    "account": "<...>",
    "warehouse": "<...>",
    "database": "<...>",
    "schema": "<...>",
    "role": "<...>",
    "authenticator": "externalbrowser",
    }

    # Create session
    session = Session.builder.configs(conn_params).create()

    # Load and prepare data
    all_data = session.sql("SELECT *, IFF(CLASS = 'g', 1.0, 0.0) AS LABEL FROM Gamma_Telescope_Data").drop("CLASS")
    train_data, test_data = all_data.random_split(weights=[0.9, 0.1], seed=0)

    # Define feature and label columns
    FEATURE_COLS = [c for c in train_data.columns if c != "LABEL"]
    LABEL_COLS = ["LABEL"]

    # Construct pipeline
    pipeline = Pipeline(steps=[
    ("impute", SimpleImputer(input_cols=FEATURE_COLS, output_cols=FEATURE_COLS)),
    ("scaler", StandardScaler(input_cols=FEATURE_COLS, output_cols=FEATURE_COLS)),
    ("model", XGBClassifier(input_cols=FEATURE_COLS, label_cols=LABEL_COLS))
    ])

    # Train the pipeline
    pipeline.fit(train_data)

    # Register model
    native_registry = registry.Registry(
    session=session,
    database_name=session.get_current_database(),
    schema_name=session.get_current_schema()
    )

    model_name = "Gamma_test"
    version = "V8"

    model_version = native_registry.log_model(
    model=pipeline, # <-- This line triggers the error
    model_name=model_name,
    version_name=version,
    sample_input_data=test_data,
    comment="Gamma test",
    conda_dependencies=["snowflake-ml-python==1.7.4", "snowflake-snowpark-python==1.28.0"],
    options={"enable_explainability": True}
    )

    Observations & Debugging Attempts:

    1. Pipeline Training Works – pipeline.fit(train_data) runs without errors.
    2. Pipeline Predictions Work – Predictions on training and test data succeed.
    3. Model Explanation Works Without Pipeline – If I train an XGBClassifier without a pipeline, I can successfully generate predictions and explanations.
    4. Session is Active – session.get_current_database() and session.get_current_schema() return valid values.
    5. Feature & Label Columns Look Correct – FEATURE_COLS and LABEL_COLS contain expected values.
    Additional Context:

    • Environment:
      • Win 10
      • Python 3.9
      • snowflake-connector-python 3.14.0
      • snowflake-ml-python 1.7.4
      • snowflake-snowpark-python 1.28.0
    • This example is based on Snowflake’s official documentation:
      Feature Preprocessing and Training on Non-Synthetic Data
    • The documentation suggests using a Pipeline, but it does not provide an example of registering a Pipeline-based model that has explainability.
    • The error message suggests that the model is somehow being treated as NoneType when passed to log_model().
    • As mentioned the whole pipeline can be registered and retrieved when I remove this options={"enable_explainability": True} from .log_model.
    • This options={"enable_explainability": True} works if I don't have a pipeline: Snowflake ML: `There is no method with name explain available in the model`
    Question:


    Is it possible to register a pipeline with options={"enable_explainability": True}?

    Continue reading...

Compartilhe esta Página