-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathREADME.Rmd
184 lines (138 loc) · 4.11 KB
/
README.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
---
output: github_document
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
```
<img src="assets/austin_mos.png" width="400"/>
# torchtabular
<!-- badges: start -->
[![DOI](https://zenodo.org/badge/378582235.svg)](https://zenodo.org/badge/latestdoi/378582235)
<!-- badges: end -->
A package for training transformer models on tabular datasets, using SAINT and TabTransformer variant models in R using {torch}.
## Installation
You can install torchtabular from [GitHub](https://github.com/) with:
``` r
# install.packages("devtools")
devtools::install_github("cmcmaster1/torchtabular")
```
## Example
```{r setup, results='hide', message=FALSE, warning=FALSE}
library(torchtabular)
library(tidymodels)
library(tidyverse)
library(torch)
library(luz)
library(madgrad)
```
### Set seeds
```{r}
torch_manual_seed(seed = 154)
set.seed(154)
```
### Check for GPU and assign device
```{r}
device <- ifelse(cuda_is_available(), 'cuda', 'cpu')
```
### Load data
The blastchar dataset is included.
```{r}
data('blastchar')
glimpse(blastchar)
```
### Prepare data
First we will convert the target variable into an integer (0 and 1), and convert characters to factors so that our tabular dataset will identify them correctly.
```{r}
blastchar <- blastchar %>%
select(-customerID) %>%
mutate(across(c(where(is.character), SeniorCitizen), as_factor),
Churn = as.numeric(Churn) - 1)
glimpse(blastchar)
```
We can now split the data into train and test sets.
```{r}
split <- initial_split(blastchar)
train <- training(split)
valid <- testing(split)
```
By creating a recipe, the `tabular_dataset` function will automatically recognise categorical (must be factors) and continuous predictors.
```{r}
recipe <- recipe(blastchar, Churn ~ .) %>%
step_scale(all_numeric_predictors()) %>%
step_integer(all_nominal_predictors()) %>%
step_impute_linear(all_predictors())
```
We can then pass this recipe to `tabular_dataset` with the relevant split.
```{r}
train_dset <- tabular_dataset(recipe, train)
valid_dset <- tabular_dataset(recipe, valid)
```
Finally, we make a dataloader.
```{r}
train_dl <- dataloader(train_dset,
batch_size = 64,
shuffle = TRUE)
valid_dl <- dataloader(valid_dset,
batch_size = 1024,
shuffle = FALSE)
```
Now we define our model:
```{r}
n_epochs <- 5
model_setup <- tabtransformer %>%
setup(
loss = nn_bce_with_logits_loss(),
optimizer = madgrad::optim_madgrad,
metrics = list(
luz_metric_binary_auroc(from_logits = TRUE),
luz_metric_binary_accuracy_with_logits()
)
) %>%
set_hparams(categories = train_dset$categories,
num_continuous = train_dset$num_continuous,
dim_out = 1,
attention = "both",
attention_type = "signed",
is_first = TRUE,
dim = 32,
depth = 1,
heads_selfattn = 32,
heads_intersample = 32,
dim_heads_selfattn = 16,
dim_heads_intersample = 64,
attn_dropout = 0.1,
ff_dropout = 0.8,
embedding_dropout = 0.0,
mlp_dropout = 0.0,
mlp_hidden_mult = c(4, 2),
softmax_mod = 1.0,
is_softmax_mod = 1.0,
skip = FALSE,
device = device) %>%
set_opt_hparams(lr = 2e-3)
```
And train...
```{r}
fitted <- model_setup %>%
fit(train_dl,
epochs = n_epochs,
valid_data = valid_dl,
verbose = TRUE)
```
We test on a large batch to improve performance:
```{r}
full_dset <- tabular_dataset(recipe, bind_rows(valid, train))
predict_bs <- 4000
preds <- predict(fitted,
full_dset,
dataloader_options = list(batch_size = predict_bs))$squeeze(-1)
preds <- as_array(preds)[1:nrow(valid)]
truth <- as_factor(ifelse(valid$Churn == 1, "Yes", "No"))
roc_auc_vec(truth = truth, estimate = preds, event_level = "second")
```