Skip to content

Creating Parsnip Model Functions #832

Closed
@jonthegeek

Description

@jonthegeek

I'm trying to grok the addition of a parsnip-friendly model function to a package. I'm specifically working to make our parsnip-friendly implementation in {tidybert} work, but I'd like to move it from "change this argument and see what happens" like I'm tending to do right now toward "do the thing because that's clearly the thing to do."

I'm trying to understand how I can actually make our parameters tunable. I'm getting an error similar to epochs in #815, which was I think mostly fixed via this baguette PR. I'm not sure I grok what happened nor if my issue is similar.

The (most) important tidybert code is in parsnip.R.

# remotes::install_github("macmillancontentscience/tidybert")
library(tidybert)
library(tidymodels)
library(modeldata)
tidymodels_prefer()

data(tate_text)
tate_text <- tate_text %>% 
  # We'll just work with the top 6 artists.
  dplyr::filter(
    artist %in% c(
      "Schütte, Thomas", "Zaatari, Akram", "Beuys, Joseph", "Ferrari, León", 
      "Kossoff, Leon", "Tillmans, Wolfgang"
    )
  ) %>% 
  dplyr::mutate(
    artist = as.factor(as.character(artist))
  )
set.seed(4242)
tate_folds <- rsample::vfold_cv(tate_text, v = 2)

tidybert_spec <- bert(
  mode = "classification",
  epochs = 1
) %>% 
  parsnip::set_engine(
    "tidybert", 
    bert_type = tune(),
    n_tokens = tune()
  )
tidybert_workflow <- workflows::workflow(
  artist ~ title,
  tidybert_spec
)

grid <- tidybert_workflow %>% 
  workflows::extract_parameter_set_dials() %>% 
  update(
    bert_type = bert_type(
      c("bert_small_uncased", "bert_tiny_uncased")
    ),
    n_tokens = n_tokens(c(4, 5))
  ) %>% 
  dials::grid_latin_hypercube(size = 1)

torch::torch_manual_seed(4242)
tidybert_result <- tidybert_workflow %>% 
  tune::tune_grid(
    resamples = tate_folds,
    grid = grid
  )
#> ! Fold1: preprocessor 1/1, model 1/1: The following arguments cannot be manually modified and were removed: be...
#> ! Fold2: preprocessor 1/1, model 1/1: The following arguments cannot be manually modified and were removed: be...
tidybert_result$.notes[[1]]$note
#> [1] "The following arguments cannot be manually modified and were removed: bert_type, n_tokens."

Created on 2022-10-27 with reprex v2.0.2

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions