---
title: "classification: gallstone dataset"
output: rmarkdown::html_vignette
params:
  eval: true
vignette: >
  %\VignetteIndexEntry{classification: gallstone dataset}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r, include = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
library(LBBNN)
has_torch <- requireNamespace("torch", quietly = TRUE) &&
            torch::torch_is_installed()
```

## Prepare data

The gallstone dataset consists of a mix of demographic, bioimpedance and laboratory features. It is a mix of binary and continuous features, with 40 in total after pre-processing the dataset. It is relatively small, with only 319 total samples, where we use 223 of those for training and the remaining 96 for validation. This makes it potentially sensitive to initializations, or other hyperparameters such as the number of epochs to train.
```{r, eval = has_torch}
torch::torch_manual_seed(42)
loaders_gs <- get_dataloaders(gallstone_dataset, train_proportion = 0.70,
                              train_batch_size = 223, test_batch_size = 96,
                              standardize = TRUE, seed = 42)
train_loader_gs <- loaders_gs$train_loader
test_loader_gs <- loaders_gs$test_loader
```

## Define model

This 4 hidden layer model with normalizing flows has over 30000 parameters, which
can be seen when using the print function.
```{r, eval = has_torch}
problem <- "binary classification"
sizes <- c(40, 16, 16, 16, 16, 1)
inclusion_priors <- c(0.5, 0.5, 0.5, 0.5, 0.5) 
stds <- c(1, 1, 1, 1, 1) 
inclusion_inits <- 'polarized_dense'
device <- "cpu"
model_gs <- lbbnn_net(problem_type = problem, sizes = sizes,
                     prior = inclusion_priors, flow = TRUE,
                     dims = c(10, 10, 10, 10), 
                     inclusion_inits = inclusion_inits,
                     input_skip = TRUE, std = stds, device = device)
#print(model_gs)
```

## Train and validate

```{r, eval = FALSE}
train_lbbnn(epochs = 1000, LBBNN = model_gs,
            lr = 0.005, train_dl = train_loader_gs, device = device,
            verbose = FALSE)

validate_lbbnn(LBBNN = model_gs, num_samples = 10, test_dl = test_loader_gs,
              device = device)

```

The above results in a very sparse model, where accuracy around 73%. Instead, training can be 
terminated at a specified minimum density using the argument "min_density" as follows:

```{r,eval = FALSE}
torch::torch_manual_seed(42)
model_2 <- lbbnn_net(problem_type = problem, sizes = sizes,
                     prior = inclusion_priors, flow = TRUE,
                     dims = c(10, 10, 10, 10), 
                     inclusion_inits = inclusion_inits,
                     input_skip = TRUE, std = stds, device = device)

train_lbbnn(epochs = 1000, LBBNN = model_2,
            lr = 0.005, train_dl = train_loader_gs, device = device,
            verbose = FALSE, min_density = 0.1)

validate_lbbnn(LBBNN = model_2, num_samples = 10, test_dl = test_loader_gs,
              device = device)
```
Accuracy is now improved to around 79%.


