是否可以使用 tidymodels 框架构建 MLP 模型?

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

根据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时尚。

是的,有来自 RStudio 的 keras package,可以从 R 调用 Python 库。

检查此 ressource, and it is designed to work well with the tidymodels framework, as extensively used in SMLTAR book

您可以使用 new-ish tidymodels 包 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))

reprex package (v2.0.1)

于 2022-05-22 创建