nn_multi_embedding: Create multiple embeddings at once

nn_multi_embeddingR Documentation

Create multiple embeddings at once

Description

It is especially useful, for dealing with multiple categorical features.

Usage

nn_multi_embedding(
  num_embeddings,
  embedding_dim,
  padding_idx = NULL,
  max_norm = NULL,
  norm_type = 2,
  scale_grad_by_freq = FALSE,
  sparse = FALSE,
  .weight = NULL
)

Arguments

num_embeddings

(integer) Size of the dictionary of embeddings.

embedding_dim

(integer) The size of each embedding vector.

padding_idx

(integer, optional) If given, pads the output with the embedding vector at padding_idx (initialized to zeros) whenever it encounters the index.

max_norm

(numeric, optional) If given, each embedding vector with norm larger than max_norm is renormalized to have norm max_norm.

norm_type

(numeric, optional) The p of the p-norm to compute for the max_norm option. Default 2.

scale_grad_by_freq

(logical, optional) If given, this will scale gradients by the inverse of frequency of the words in the mini-batch. Default FALSE.

sparse

(logical, optional) If TRUE, gradient w.r.t. weight matrix will be a sparse tensor.

.weight

(torch_tensor or list of torch_tensor) Embeddings weights (in case you want to set it manually).

Examples

library(recipes)

data("gss_cat", package = "forcats")

gss_cat_transformed <-
  recipe(gss_cat) %>%
  step_integer(everything()) %>%
  prep() %>%
  juice()

gss_cat_transformed <- na.omit(gss_cat_transformed)

gss_cat_transformed <-
   gss_cat_transformed %>%
   mutate(across(where(is.numeric), as.integer))

glimpse(gss_cat_transformed)

gss_cat_tensor  <- as_tensor(gss_cat_transformed)
.dict_size      <- dict_size(gss_cat_transformed)
.dict_size

.embedding_size <- embedding_size_google(.dict_size)

embedding_module <-
  nn_multi_embedding(.dict_size, .embedding_size)

# Expected output size
sum(.embedding_size)

embedding_module(gss_cat_tensor)


krzjoa/torchts documentation built on June 24, 2022, 5:30 a.m.