Training Neural Networks with mlr3torch

Question 1: Hello World!

In this exercise, you will train your first neural network with mlr3torch.

As a task, we will use the ‘Indian Liver Patient’ dataset where the goal is to predict whether a patient has liver disease or not.

library(mlr3verse)
library(mlr3torch)
ilpd <- tsk("ilpd")
ilpd
<TaskClassif:ilpd> (583 x 11): Indian Liver Patient Data
* Target: diseased
* Properties: twoclass
* Features (10):
  - dbl (5): albumin, albumin_globulin_ratio, direct_bilirubin, total_bilirubin, total_protein
  - int (4): age, alanine_transaminase, alkaline_phosphatase, aspartate_transaminase
  - fct (1): gender
autoplot(ilpd)

We remove the gender column from the task, so we need to only deal with numeric features for now.

ilpd_num = ilpd$clone(deep = TRUE)
ilpd_num$select(setdiff(ilpd_num$feature_names, "gender"))

Your task is to train a simple multi layer perceptron (lrn("classif.mlp")) with 2 hidden layers with 100 neurons each. Set the batch size to 32, the learning rate to 0.001 and the number of epochs to 20. Then, resample the learner on the task with a cross-validation with 5 folds and evaluate the results using classification error and false positive rate (FPR). Is the result good?

Hint The parameter for the learning rate is opt.lr

Question 2: Preprocessing

In the previous question, we have operated on the ilpd_num task where we excluded the categorical gender column. This was done because the MLP learner operates on numeric features only. You will now create a more complex GraphLearner that also incudes one-hot encoding of the gender column before applying the MLP. Resample this learner on the original ilpd task and evaluate the results using the same measures as before.

Hint Concatenate po("encode") with a lrn("classif.mlp") using %>>% to create the GraphLearner. For available options on the encoding, see po("encode")$help().

Question 3: Benchmarking

Instead of resampling a single learner, the goal is now to compare the performance of the MLP with a simple classification tree. Create a benchmark design and compare the performance of the two learners.

Hint Create a classification tree via lrn("classif.rpart"). A benchmark design can be created via benchmark_grid(). To run a benchmark, pass the design to benchmark().

Question 4: Iris as a Lazy Tensor

Create a version of the iris task where the 4 features are represented as a single lazy tensor column.

Question 5: Custom Architecture

Create a network with one hidden layer with 100 neurons and a sigmoid activation function by assempling PipeOps in a Graph. Convert the Graph to a Learner and train the network for 10 epochs using Adam with a learning rate of 0.001 and a batch size of 32.