Version

Review of previous steps

Show the code from previous steps
library(tidymodels)
library(vetiver)

car_mod <-
    workflow(mpg ~ ., linear_reg()) %>%
    fit(mtcars)
v <- vetiver_model(car_mod, "cars_mpg")
Show the code from previous steps
from vetiver.data import mtcars
from vetiver import VetiverModel
from sklearn import linear_model

car_mod = linear_model.LinearRegression().fit(mtcars.drop(columns="mpg"), mtcars["mpg"])
                 
v = VetiverModel(car_mod, model_name = "cars_mpg", 
                 ptype_data = mtcars.drop(columns="mpg"))

Store and version your model

You can store and version your model by choosing a pins “board” for it. Your board for model pins can be set up to use a local folder, RStudio Connect, Amazon S3, and more. When we write the vetiver model to our board, the binary model object is stored on our board together with necessary metadata, like the packages needed to make a prediction and the model’s input data prototype for checking new data at prediction time.

Note

We’ll use a temporary board that will be automatically deleted for this demo, but for your real work, you will want to choose the best board for your particular infrastructure.

Most pins boards have versioning turned on by default, but we can turn it on explicitly for our temporary demo board.

library(pins)
model_board <- board_temp(versioned = TRUE)
model_board %>% vetiver_pin_write(v)

To read the vetiver model object from your board, use model_board %>% vetiver_pin_read("cars_mpg").

from pins import board_temp
from vetiver import vetiver_pin_write
model_board = board_temp(versioned = True, allow_pickle_read = True)
vetiver_pin_write(model_board, v)

To read the vetiver model object from your board, use VetiverModel.from_pin(model_board, "cars_mpg").

Let’s train a new kind of model for mtcars, a decision tree instead of our original linear model.

car_mod <-
    workflow(mpg ~ ., decision_tree(mode = "regression")) %>%
    fit(mtcars)

v <- vetiver_model(car_mod, "cars_mpg")

model_board %>% vetiver_pin_write(v)
from sklearn import tree
car_mod = tree.DecisionTreeRegressor().fit(mtcars.drop(columns="mpg"), mtcars["mpg"])

v = VetiverModel(car_mod, model_name = "cars_mpg", 
                 ptype_data = mtcars.drop(columns="mpg"))
vetiver_pin_write(model_board, v)

Both versions are stored, and we have access to both.

model_board %>% pin_versions("cars_mpg")
# A tibble: 2 × 3
  version                created             hash 
  <chr>                  <dttm>              <chr>
1 20230123T183906Z-c565d 2023-01-23 18:39:06 c565d
2 20230123T183909Z-cc1e9 2023-01-23 18:39:09 cc1e9
model_board.pin_versions("cars_mpg")
              created   hash                 version
0 2023-01-23 18:39:07  82cd6  20230123T183907Z-82cd6
1 2023-01-23 18:39:10  03f7f  20230123T183910Z-03f7f

The primary purpose of pins is to make it easy to share data artifacts, so depending on the board you choose, your pinned vetiver model can be shareable with your collaborators.