I am struggling with interoperability of R packages torch and targets. For example, if I define a target of class dataset
(from torch), then it is impossible to read it with tar_read
(from targets), and I cannot use it in other targets.
Here is my dataset
generator nn_dataset
:
library(torch)
library(targets)
library(dplyr)
library(tidymodels)
nn_dataset <-
dataset(
name = "nn_dataset",
initialize = function(df) {
data <- self$prepare_data(df)
self$tele <- data$x$tele
self$class <- data$x$class
self$y <- data$y
},
.getitem = function(i) {
list(
x = list(
tele = self$tele[i, ],
class = self$class[i, ]
),
y = self$y[i, ]
)
},
.length = function() {
self$y$size()[[1]]
},
prepare_data = function(df) {
target_col <-
df$claim_ind_cov_1_2_3_4_5_6 %>%
as.integer() %>%
`-`(1) %>%
as.matrix()
tele_cols <-
df %>%
select(starts_with(c("h_", "p_", "vmo", "vma"))) %>%
as.matrix()
class_df <- select(df, expo:years_licensed, distance)
rec_class <-
recipe(~ ., data = class_df) %>%
step_impute_median(commute_distance, years_claim_free) %>%
step_other(all_nominal(), threshold = 0.05) %>%
step_dummy(all_nominal()) %>%
prep()
class_cols <- juice(rec_class) %>% as.matrix()
list(
x = list(
tele = torch_tensor(tele_cols),
class = torch_tensor(class_cols)
),
y = torch_tensor(target_col)
)
}
)
If I define the following target:
tar_target(
name = target_name,
command = nn_dataset(valid_df)
)
where valid_df
is a tibble, and if I then try to read it:
tar_read(target_name)
then I get this error:
Error in cpp_tensor_dim(self$ptr) : external pointer is not valid
I have also tried this:
tar_target(
name = target_name,
command = nn_dataset(valid_df),
format = "torch"
)
and this:
tar_torch(
name = target_name,
command = nn_dataset(valid_df)
)
but neither worked.