Skip to content

Snowflake Model Registry

In this tutorial we will show how you can create a model in a notebook, save that model to the model registry and deploy/use that model via SQL.

Video

Video still in development

Requirement

  • Snowflake account, you can use a free trial. We also assume no complex security needs.

Download

Setup

Lets start by adding some sample data and uploading our notebook.

If you don't have a database, schema or warehouse yet.
use role accountadmin;

-- Create a database to store our schemas.
create database if not exists raw;

-- Create the schema. The schema stores all our objectss.
create schema if not exists raw.science;

/*
    Warehouses are synonymous with the idea of compute
    resources in other systems. We will use this
    warehouse to call our user defined function.
*/
create warehouse if not exists development 
    warehouse_size = xsmall
    auto_suspend = 30
    initially_suspended = true;

use database raw;
use schema science;
use warehouse development;

Sample data

First lets start by creating some sample data for our model to train from.

use role accountadmin;

-- Sample Data
create or replace table student_test_scores as
    select
        $1 as hours_studied,
        $2 as test_score
    from values
        (1.0, 3.5),
        (2.0, 9.2),
        (3.0, 13.1),
        (4.0, 24.7),
        (5.0, 28.5),
        (6.0, 41.0),
        (7.0, 50.3),
        (8.0, 63.5),
        (9.0, 82.1),
        (10.0, 95.0);
1
2
3
| status                                          |
|-------------------------------------------------|
| Table STUDENT_TEST_SCORES successfully created. |

Upload and Setup Notebook

Lets upload our notebook we downloaded earlier. Start by navigating to notebooks and clicking import .ipynb file. Navigate to upload

Next we'll name it and select the location and that we want to run via a warehouse. selection on notebook

Our final step before running is to install the needed package snowflake-ml-python to build and save our model. The package comes with sklearn with it. package install

Run the notebook

To run the notebook we'll click run all. run all

Once the model is run we can refresh our schema to see a model object has been added. We'll want to open the details to get our metrics we saved in the notebook. object

We can click on the version we just created. version

We can see the metadata our model and also the functions we will use in the next section to make predictions via SQL. metadata

Use the model via SQL

Now that we have our model saved into our model registry, lets use the model via sql to make a prediction. Lets open a new worksheet.

1
2
3
4
5
6
7
-- Using the default version.
select
    8 as hours_studied,

    predict_test_score!predict(hours_studied) as result,

    result:output_feature_0::float as prediction;
1
2
3
| HOURS_STUDIED | RESULT                                          | PREDICTION  |
|---------------|-------------------------------------------------|-------------|
| 8             | {   "output_feature_0": 6.647030303030303e+01 } | 66.47030303 |

We can also select specific versions if we have multiple or want to retrieve the latest model version..

1
2
3
4
5
6
7
8
-- Selecting a specific model version.
with
    predict_test_score as model predict_test_score version last

select
    3.5 as hours_studied,
    predict_test_score!predict(hours_studied) as result,
    result:output_feature_0::float as prediction;
1
2
3
| HOURS_STUDIED | RESULT                                          | PREDICTION   |
|---------------|-------------------------------------------------|--------------|
| 3.5           | {   "output_feature_0": 2.586181818181818e+01 } | 25.861818182 |

Change default version

Updating the version can allow us to make sure downstream users are using the correct model.

1
2
3
4
alter model
    predict_test_score
set
    default_version = <Model version name>;
1
2
3
4
alter model
   predict_test_score
set
    default_version = V20241122_114535;
1
2
3
| status                           |
|----------------------------------|
| Statement executed successfully. |