dzegpi
dzegpi

Reputation: 688

Is possible to build a MLP model with the tidymodels framework?

According to documentation, there are three engines for fitting MLP models in tidymodels, but all of them (as I understand) can define only one hidden layer. Am I missing some engine, incorporated in the tidymodels ecosystem, that makes available multi (hidden) layer feed forward networks? In sklearn.neural_network fashion.

Upvotes: 3

Views: 930

Answers (1)

Julia Silge
Julia Silge

Reputation: 11663

You can do this with the new-ish tidymodels package brulee, which uses torch. You specify the number of layers and hidden units together:

hidden_units: An integer for the number of hidden units, or a vector of integers. If a vector of integers, the model will have length(hidden_units) layers each with hidden_units[i] hidden units.

library(tidymodels)
library(brulee)

data("parabolic")

set.seed(1)
parabolic_split <- initial_split(parabolic)
parabolic_tr <- training(parabolic_split)
parabolic_te <- testing(parabolic_split)

set.seed(2)
cls_fit <- brulee_mlp(
  class ~ ., data = parabolic_tr, 
  ## two layers with 5 hidden units each:
  hidden_units = c(5, 5),
  epochs = 50L, learn_rate = 0.1, batch_size = 2^8
)

grid_points <- seq(-4, 4, length.out = 100)
grid <- crossing(X1 = grid_points, X2 = grid_points)

predict(cls_fit, grid, type = "prob") %>%
  bind_cols(grid) %>%
  ggplot(aes(X1, X2)) +
  geom_contour(aes(z = .pred_Class1), breaks = 1/2, col = "black") +
  geom_point(data = parabolic_te, aes(col = class))

Created on 2022-05-22 by the reprex package (v2.0.1)

Upvotes: 7

Related Questions