R/mlp-module.R
model_mlp.Rd
A configurable feed forward network (Multi-Layer Perceptron) with embedding
model_mlp(..., embedding = NULL, activation = nnf_relu)
net <- model_mlp(4, 2, 1)
x <- as_tensor(iris[, 1:4])
net(x)
#> torch_tensor
#> 1.0752
#> 1.0135
#> 0.9961
#> 0.9770
#> 1.0645
#> 1.1229
#> 0.9802
#> 1.0552
#> 0.9345
#> 1.0353
#> 1.1334
#> 1.0245
#> 1.0119
#> 0.9324
#> 1.1855
#> 1.1938
#> 1.1196
#> 1.0640
#> 1.1762
#> 1.0801
#> 1.1199
#> 1.0639
#> 0.9981
#> 1.0342
#> 1.0270
#> 1.0357
#> 1.0338
#> 1.0917
#> 1.0858
#> 0.9986
#> ... [the output was truncated (use n=-1 to disable)]
#> [ CPUFloatType{150,1} ][ grad_fn = <ReluBackward0> ]
# With categorical features
library(recipes)
#>
#> Attaching package: ‘recipes’
#> The following object is masked from ‘package:stats’:
#>
#> step
iris_prep <-
recipe(iris) %>%
step_integer(Species) %>%
prep() %>%
juice()
iris_prep <- mutate(iris_prep, Species = as.integer(Species))
x_num <- as_tensor(iris_prep[, 1:4])
x_cat <- as_tensor(dplyr::select(iris_prep, 5))
n_unique_values <- dict_size(iris_prep)
.init_layer_spec <-
init_layer_spec(
num_embeddings = n_unique_values,
embedding_dim = embedding_size_google(n_unique_values),
numeric_in = 4,
numeric_out = 2
)
#> Error in init_layer_spec(num_embeddings = n_unique_values, embedding_dim = embedding_size_google(n_unique_values), numeric_in = 4, numeric_out = 2): could not find function "init_layer_spec"
net <- model_mlp(.init_layer_spec, 2, 1)
#> Error in initialize(...): object '.init_layer_spec' not found
net(x_num, x_cat)
#> Error in mget(x = c("tensors", "dim")): attempt to apply non-function