Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add the mlverse:tabnet model familly #226

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ Suggests:
rsample,
RSpectra,
survival (>= 3.2-10),
tabnet,
testthat (>= 3.0.0),
TH.data,
usethis (>= 1.5.0),
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ S3method(axe_env,terms)
S3method(axe_env,train)
S3method(axe_env,train.recipe)
S3method(axe_env,xgb.Booster)
S3method(axe_fitted,"_tabnet_fit")
S3method(axe_fitted,C5.0)
S3method(axe_fitted,default)
S3method(axe_fitted,earth)
Expand Down
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# butcher (development version)

* Added butcher methods for `tabnet()` (@cregouby #226).

# butcher 0.3.0

* Julia Silge is now the maintainer (#230).
Expand Down
45 changes: 45 additions & 0 deletions R/tabnet_fit.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#' Axing a tabnet_fit.
#'
#' @inheritParams butcher
#'
#' @return Axed tabnet_fit object.
#'
#' @examplesIf rlang::is_installed("tabnet")
#'
#' # Load libraries
#' suppressWarnings(suppressMessages(library(parsnip)))
#' suppressWarnings(suppressMessages(library(rsample)))
#'
#' # Load data
#' split <- initial_split(mtcars, props = 9/10)
#' car_train <- training(split)
#'
#' # Create model and fit
#' mtcar_fit <- tabnet() %>%
#' set_mode("regression") %>%
#' set_engine("torch")
#' fit(mpg ~ ., data = car_train)
#'
#' out <- butcher(mtcar_fit, verbose = TRUE)
#'
#' @name axe-tabnet_fit
NULL

#' Remove fitted values.
#'
#' @rdname axe-tabnet_fit
#' @export
axe_fitted._tabnet_fit <- function(x, verbose = FALSE, ...) {
old <- x
x$fit$fit <- exchange(x$fit$fit, "checkpoints", list(NULL))
x$fit$fit$importances <- exchange(x$fit$fit$importances, "variables", list(NULL))
x$fit$fit$importances <- exchange(x$fit$fit$importances, "importance", list(NULL))

add_butcher_attributes(
x,
old,
disabled = NULL,
add_class = FALSE,
verbose = verbose
)
}
46 changes: 46 additions & 0 deletions man/axe-tabnet_fit.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

41 changes: 41 additions & 0 deletions tests/testthat/test-tabnet_fit.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
test_that("tabnet_fit + axe_fitted() works", {
skip_on_cran()
skip_if_not_installed("tabnet")
suppressPackageStartupMessages(library(parsnip))
# Create model and fit
tabnet_fit <- tabnet::tabnet(epochs = 10) %>%
set_mode("regression") %>%
set_engine("torch") %>%
fit(mpg ~ ., data = mtcars)

expect_error(axed_out <- axe_fitted(tabnet_fit, verbose = TRUE), NA)
expect_lt(lobstr::obj_size(axed_out),lobstr::obj_size(tabnet_fit))
})

test_that("tabnet_fit + butcher() works", {
skip_on_cran()
skip_if_not_installed("tabnet")
suppressPackageStartupMessages(library(parsnip))
# Create model and fit
tabnet_fit <- tabnet::tabnet(epochs = 10) %>%
set_mode("regression") %>%
set_engine("torch") %>%
fit(mpg ~ ., data = mtcars)

expect_error(tabnet_out <- butcher(tabnet_fit, verbose = TRUE), NA)
})

test_that("tabnet_fit + predict() works", {
skip_on_cran()
skip_if_not_installed("tabnet")
suppressPackageStartupMessages(library(parsnip))
# Create model and fit
tabnet_fit <- tabnet::tabnet(epochs = 10) %>%
set_mode("regression") %>%
set_engine("torch") %>%
fit(mpg ~ ., data = mtcars)

tabnet_out <- butcher(tabnet_fit, verbose = TRUE)
new_data <- as.matrix(mtcars[1:3, 2:11])
expect_equal(predict(tabnet_out,new_data), predict(tabnet_fit, new_data))
})